I'm trying to write web application using wicket + hibernate + spring. And like all guys I'd like to use @Autowired annotation. First of all, here is my applicationContext.xml:
<beans default-autowire="autodetect"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<bean id="placeholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="false"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="ignoreResourceNotFound" value="false"/>
<property name="locations">
<list>
<value>classpath*:/application.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
//connection...
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<!-- setup transaction manager -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
//some hibernate stuff
</bean>
//wicket app bean
<bean id="wicketApplication" class="com.team.wicket.ShopApp" scope="singleton"/>
//DAO beans
<bean id="userDAO" class="com.team.wicket.daoImpl.UserDAOImpl" scope="singleton"/>
<bean id="contactDAO" class="com.team.wicket.daoImpl.ContactDAOImpl" scope="singleton"/>
here is web.xml:
<display-name>wicket-spring-hibernate</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>opensessioninview</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter>
<filter-name>wicket-spring-hibernate</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationFactoryClassName</param-name>
<param-value>org.apache.wicket.spring.SpringWebApplicationFactory</param-value>
</init-param>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>com.mycompany.WicketApplication</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>opensessioninview</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>wicket-spring-hibernate</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Actually I need to use @Autowired only with userDAO and contactDAO beans. The code of UserDAOImpl and ContactDAOImpl starts with @Service annotation:
@Service
public class ContactDAOImpl implements ContactDAO {
....
public ContactDAOImpl() {
System.out.println("contactDAO created");
}
@Service
public class UserDAOImpl implements UserDAO {
...
public UserDAOImpl() {
System.out.println("UserDAO created");
}
As you can see I've added System.out to the default construstors of the classes because I wanted to be sure that spring creates beans. And it really creates beans every time I start application so the problem must be with autowiring. And finally here is the class where autowiring fails:
public class UserSession extends AuthenticatedWebSession {
@Autowired
@Qualifier("userDAO")
private UserDAO userDAO;
private User user;
public UserSession(Request request) {
super(request);
}
@Override
public boolean authenticate(String username, String password) {
User user = userDAO.getUser(username, password);
if (user != null) {
setUser(user);
return true;
}
return false;
}
@Override
public Roles getRoles() {
Roles roles = new Roles();
if (isSignedIn()) {
roles.add(Roles.USER);
}
return roles;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
when authenticate() tries to call getUser(...) it gives a NullPointerException. From debuging I know that UserDAO is not initialized (it is just null) and I also know that Spring creates UserDAO and ContactDAO beans. So what I'm doing wrong? Thanks.