Spring GenericFilterBean @AutoWired hibernate属性为null

时间:2015-08-16 20:57:51

标签: spring hibernate autowired

一个简单的过滤器实现:

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>

我的应用程序上下文xml文件的一部分:

<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>

由于某种原因,变量sessionFactory始终为null,我得到一个空点异常(并且该print打印“its null”)。完整的消息是2015年8月16日23:51:38.383 SEVERE [http-apr-8080-exec-2] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service()for servlet [maze] in context with path []抛出异常  显示java.lang.NullPointerException

编辑:我向SessionFilter setSessionFactory方法添加了更多内容:

public void setSessionFactory(SessionFactory sessionFactory) {
    System.out.println("SesionFilter : setSessionFactory called");
    if(sessionFactory == null) {
        System.out.println("SessionFilter : setSessionFactory. sessionFactory is null");
    } else {
        System.out.println("SessionFilter : setSessionFactory. sessionFactory isnt null");
    }
    this.sessionFactory = sessionFactory;
    System.out.println("After assignment this.sessionFactory null? " + (this.sessionFactory == null));
}

打印出来

SesionFilter : setSessionFactory called
SessionFilter : setSessionFactory. sessionFactory isnt null
After assignment this.sessionFactory null? false

那么如果sessionFactory的值没有改变,那么它怎么能在之后变为空?

2 个答案:

答案 0 :(得分:1)

通过更改过滤器属性中的过滤器类来解决(我认为):

    <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>

答案 1 :(得分:0)

您必须使 SessionFilter 成为 spring 上下文的一部分。

@组件 公共类 SessionFilter 扩展了 GenericFilterBean {

}

而不是通过使用手动创建它对象来注入它的依赖

@Autowired private SessionFilter sessionFilter;