Note that there are some explanatory texts on larger screens.

plurals
  1. POAutowiring doesn't work in Spring
    primarykey
    data
    text
    <p>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:</p> <pre><code>&lt;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"&gt; &lt;context:annotation-config/&gt; &lt;bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="ignoreUnresolvablePlaceholders" value="false"/&gt; &lt;property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/&gt; &lt;property name="ignoreResourceNotFound" value="false"/&gt; &lt;property name="locations"&gt; &lt;list&gt; &lt;value&gt;classpath*:/application.properties&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt; //connection... &lt;/bean&gt; &lt;tx:annotation-driven transaction-manager="txManager"/&gt; &lt;!-- setup transaction manager --&gt; &lt;bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt; &lt;property name="sessionFactory"&gt; &lt;ref bean="sessionFactory"/&gt; &lt;/property&gt; &lt;/bean&gt; &lt;!-- hibernate session factory --&gt; &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; //some hibernate stuff &lt;/bean&gt; //wicket app bean &lt;bean id="wicketApplication" class="com.team.wicket.ShopApp" scope="singleton"/&gt; //DAO beans &lt;bean id="userDAO" class="com.team.wicket.daoImpl.UserDAOImpl" scope="singleton"/&gt; &lt;bean id="contactDAO" class="com.team.wicket.daoImpl.ContactDAOImpl" scope="singleton"/&gt; </code></pre> <p></p> <p>here is web.xml:</p> <pre><code> &lt;display-name&gt;wicket-spring-hibernate&lt;/display-name&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;classpath:applicationContext.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;filter&gt; &lt;filter-name&gt;opensessioninview&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter&gt; &lt;filter-name&gt;wicket-spring-hibernate&lt;/filter-name&gt; &lt;filter-class&gt;org.apache.wicket.protocol.http.WicketFilter&lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;applicationFactoryClassName&lt;/param-name&gt; &lt;param-value&gt;org.apache.wicket.spring.SpringWebApplicationFactory&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;applicationClassName&lt;/param-name&gt; &lt;param-value&gt;com.mycompany.WicketApplication&lt;/param-value&gt; &lt;/init-param&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;opensessioninview&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;wicket-spring-hibernate&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;/web-app&gt; </code></pre> <p>Actually I need to use @Autowired only with userDAO and contactDAO beans. The code of UserDAOImpl and ContactDAOImpl starts with @Service annotation:</p> <pre><code>@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"); } </code></pre> <p>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:</p> <pre><code>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; } } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload