Spring GenericFilterBean @AutoWired hibernate property is null -
a simple filter implementation:
public class sessionfilter extends genericfilterbean { @autowired private sessionfactory sessionfactory; @override public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws ioexception, servletexception { system.out.println("sessionfilter : dofilter called. opening session..."); if(sessionfactory == null) { system.out.println("its null"); } sessionfactory.getcurrentsession().begintransaction(); system.out.println("sessionfilter : dofilter calling filter chain.."); filterchain.dofilter(servletrequest, servletresponse); system.out.println("sessionfilter : dofilter closing session."); sessionfactory.getcurrentsession().gettransaction().commit(); system.out.println("sessionfilter : dofilter finished."); } public void setsessionfactory(sessionfactory sessionfactory) { this.sessionfactory =sessionfactory; }
}
web.xml:
<filter> <filter-name>sessionfilter</filter-name> <filter-class>lt.maze.controller.sessionfilter</filter-class> </filter> <filter-mapping> <filter-name>sessionfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
and part of application context xml file:
<beans:bean id="hibernate5annotatedsessionfactory" class="org.springframework.orm.hibernate5.localsessionfactorybean"> <beans:property name="datasource" ref="datasource" /> <beans:property name="hibernateproperties"> <beans:props> <beans:prop key="hibernate.dialect">org.hibernate.dialect.mysqldialect </beans:prop> <beans:prop key="hibernate.show_sql">true</beans:prop> </beans:props> </beans:property> </beans:bean> <beans:bean id="sessionfilter" class="lt.maze.controller.sessionfilter"> <beans:property name="sessionfactory" ref="hibernate5annotatedsessionfactory" /> </beans:bean>
for reason variable sessionfactory null, null point exception(and print prints "its null"). full message 16-aug-2015 23:51:38.383 severe [http-apr-8080-exec-2] org.apache.catalina.core.standardwrappervalve.invoke servlet.service() servlet [maze] in context path [] threw exception java.lang.nullpointerexception
edit:i added more prints sessionfilter
s setsessionfactory
method:
public void setsessionfactory(sessionfactory sessionfactory) { system.out.println("sesionfilter : setsessionfactory called"); if(sessionfactory == null) { system.out.println("sessionfilter : setsessionfactory. sessionfactory null"); } else { system.out.println("sessionfilter : setsessionfactory. sessionfactory isnt null"); } this.sessionfactory = sessionfactory; system.out.println("after assignment this.sessionfactory null? " + (this.sessionfactory == null)); }
which print out
sesionfilter : setsessionfactory called sessionfilter : setsessionfactory. sessionfactory isnt null after assignment this.sessionfactory null? false
so how can sessionfactory null afterwards if value isn't changed?
solved(i think) changing filter class in filter attribute:
<filter> <filter-name>sessionfilter</filter-name> <filter-class>org.springframework.web.filter.delegatingfilterproxy</filter-class> </filter> <filter-mapping> <filter-name>sessionfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Comments
Post a Comment