0

i have this exception

java.lang.NullPointerException
cz.xkadle21.dip.dao.ADiHibernateGenericDAO.findByCriteria(ADiHibernateGenericDAO.java:116)
cz.xkadle21.dip.dao.impl.DiUserDAO.findUserByUsername(DiUserDAO.java:86)
cz.xkadle21.dip.service.impl.DiUserContextSecurityService.loadUserByUsername(DiUserContextSecurityService.java:47)
cz.xkadle21.dip.service.impl.DiUserContextSecurityService.loadUserByUsername(DiUserContextSecurityService.java:1)

I was following this tutorial Spring Security 3 database authentication with Hibernate

and got "No bean named ... is defined" error. So i moved beans from dispatcher-servlet.xml to applicationContext-common-business.xml and change loading in web.xml

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext-common-business.xml 
        /WEB-INF/applicationContext-security.xml
    </param-value>
</context-param>
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Beans in despatcher-servlet.xml are loading with component-scan and are injecting sessionFactory automaticaly and properly. But bean in applicationContext-common-business.xml not.

applicationContext-common-business.xml

<bean name="userDetailsService"
    class="cz.xkadle21.dip.service.impl.DiUserContextSecurityService" >
     <constructor-arg ref="userDAO" /> 
     <constructor-arg ref="securityUserFactory" />      
</bean>

<bean id="securityUserFactory" class="cz.xkadle21.dip.factory.impl.DiSecurityUserFactory" />
<bean id="userDAO" class="cz.xkadle21.dip.dao.impl.DiUserDAO" />



<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="${hibernate.connection.driver_class}"
    p:url="${hibernate.connection.url}" p:username="${hibernate.connection.username}"
    p:password="${hibernate.connection.password}" />


<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>


    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
        </props>
    </property>
</bean>

UserDetailsService is injected via constructor, but how to inject sessionFactory to userDAO? SessionFactory is defined in ADiHibernateGenericDAO and all DAOs extend the abstract ADiHibernateGenericDAO.The exception above is thrown on SessionFactory, which is not injected.

Thanks for any response.

Community
  • 1
  • 1
rizler
  • 5
  • 1
  • 3

2 Answers2

1

You haven't shown us your DiUserDAO class, but assuming you have a setter in it for setSessionFactory(), you could simply change your XML mapping to be like:

<bean id="userDAO" class="cz.xkadle21.dip.dao.impl.DiUserDAO">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

Alternatively you could modify your DiUserDAO class to mark the SessionFactory field as @Autowired.

The same solution applies to any other beans that need to access this bean.

matt b
  • 138,234
  • 66
  • 282
  • 345
  • Yes, you right. I was trying before inject directly abstract class. With annotations it is not working for me. Only via constructor or setter (I was thinking, that annotations are working only in dispatcher-servlet.xml config.) Now i have "No Hibernate Session bound to thread" , but perhaps that's somehow solve itself. Thanks again for response – rizler Mar 13 '11 at 15:27
1

Mate, I don't see any Transaction Manager or

< tx:annotation-driven />

written anywhere in your bean configuration file. You should put it there if you already haven't. That might be the problem.

Satadru Biswas
  • 1,555
  • 2
  • 13
  • 23
  • Hi, I already figured it out. I thought that this definition should only be in one configuration file. So I put this to both and now it is working corectlly. – rizler Mar 14 '11 at 19:23
  • put it in the -servlet.xml – Satadru Biswas Mar 14 '11 at 19:53
  • well, when it is only in -servlet.xml, than are not working transactions in beans, that are loading via applicationContext-common-business.xml config file – rizler Mar 14 '11 at 23:00