Working with Spring

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

Hi !

Sorry for the delay in the response - and in general for my lack of presence on those boards, but things have been quite epic at work for the last weeks - and it is not over yet :expressionless:

Anyway !

I’m not sure to fully understand your problem here.
In my application, I define more or less everything as a spring bean - including the ITMill components I use in my user interface.

In fact, my whole object hierarchy is defined in Spring - mostly in applicationContext file & explicitly, true, but I don’t think that using annotations and / or autowiring should change much.

Hibernate factories (and a lot of others things) are defined in Spring, too, and wired in the bean hierarchy, ultimately to the toolit components.

The nice consequence of this is that I only use the SpringHelper to get access to the root node of my hierarchy - which is a bean extending the toolkit Application.

But then, I’m really not sure to understand what your problem is :expressionless:
I’m not, by any mean, a Spring expert - I only know how to use it to do what I want, but there is a lot of things about it I don’t know ^^

Sorry if I’m not much help here :s

Quentin.

The problem I am having is that my beans are not being instantiated. Its like the component-scan option to automatically discover your beans doesn’t work.

<code> <context:component-scan base-package=“org.waterford.onlineupdates” />
</code>

When I run my code in the example I posted above, none of the @Autowired objects are instantiated.

OK I finally found my problem. I’m kind of embarrassed, it has nothing to do with Spring. It was a silly mistake on some package names not agreeing. So spring couldn’t find my classes because it was scanning the wrong base package. I guess I didn’t see an error because I had some other project loaded in JBoss with the matching package name so I didn’t get exceptions while loading the WAR.

That’s a good news ! :slight_smile:

I apologize again for not being able to be very helpful on that one… Deadlines should be forbidden :frowning:

Good luck on your projects,
Quentin.

no apology necessary I didn’t post enough information for anyone to have spotted my error. None of my code samples included the package names. Don’t worry, I will be posting more questions you can help me with. :slight_smile:

Hi,

I updated the Spring Integration document. Just see the end of that. I think this is what you are looking for.

-Petri

Thanks for the examples!