Hello,
I am trying to use IT Mill with Spring and Hibernate. I have read the
forum posts here
and I have implemented the Spring Context Helper described by
Quentin Astegiano here
.
Using the SpringContextHelper does work. However, it is very cumbersome from what I am used to. I have been using the annotation method in my projects to instantiate my beans. Using the helper class means I have to add this boilerplate code all through my project and have a reference to the Application class handy any time I want to access the database or any of my other beans.
The way I have been (and would like to) work with my data and other beans is by defining the data source and hibernate mappings in my context-servlet.xml as in the example below:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:/OnlineUpdatesDB"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>org/waterford/updates/db/Market.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
</props>
</property>
</bean>
Then in my source files I simple auto-wire my session object using the annotation @Autowired. I am also accustom to using the annotations to identify my bean components and then auto-wire them in classes where I want to use them as in the example below:
@Repository
@SuppressWarnings("unchecked")
public class MarketDAO {
@Autowired
private SessionFactory sessionFactory;
public Market getMarket(Integer intId) {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("from Market m where m.marketId=:id");
query.setInteger("id",intId);
return (Market)query.uniqueResult();
}
}
I would access my MarketDAO class through the code below:
@Component
public class MarketService {
@Autowired
private MarketDAO marketDAO;
public Market getMarket(int marketId) {
return marketDAO.getMarket(marketId);
}
}
So forgive me if this sounds like whining… but, is there a way I can still do this. I really don’t want to replace all my @Autowired variables with the whole helper look-up approach.
Thanks in advance for any advice/help.
Gregg