0

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.

Andrey Yaskulsky
  • 2,458
  • 9
  • 39
  • 81

2 Answers2

1

Try context:component-scan base-package="x.y.z"

Make sure you add xmlns:context="http://www.springframework.org/schema/context"

Sujoy
  • 802
  • 11
  • 22
  • If OP uses this solution, they need to remove the bean declaration for the `@Service` annotated classes. Otherwise, they will have 2 beans of the same type in the context. – Sotirios Delimanolis Aug 19 '13 at 14:03
  • Component scan with @service is the correct answer and also the answer from Evgeniy, the UserService needs to be a bean too. Sotirios: the xml config will not add a second bean but rather superseed the annotation config if the bean name is the same.. – Martin Frey Aug 19 '13 at 17:44
  • Ok, but I don't need to define UserSession as a Spring Bean because wicket creates UserSession object when new user appears (I don't need to get those objects from Spring). – Andrey Yaskulsky Aug 20 '13 at 07:35
  • I dont have repo of 50 points so I could not add comment to the next answer. I don't know how Wicket works, but am pretty much sure you can integrate that with Spring. Hope this link helps. http://vozis.blogspot.com/2012/02/integrating-wicket-with-spring.html – Sujoy Aug 21 '13 at 03:27
0

Spring can inject dependencies only into its own beans. UserSession is not in context.xml which means it is not a Spring bean and cannot be autowired by Spring

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Thanks. But I have another question - UserSession is provided by wicket and each instance is created by wicket itself. So I cant just define UserSession like a spring bean (because I don't want to get UserSession instances from Spring). What should I do in this situation? – Andrey Yaskulsky Aug 20 '13 at 07:33