gpt4 book ai didi

java - 过滤器 - Servlets 和 Spring beans 集成 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 08:29:06 24 4
gpt4 key购买 nike

我在 servlet 之前执行过滤器:

    public class UserFilter implements Filter {


List<String> sessionIdsList;

WebContentDAOIF webContentDAOIF;

public void setWebContentDAOIF(WebContentDAOIF webContentDAOIF) {
this.webContentDAOIF = webContentDAOIF;
}


@Override
public void init(FilterConfig arg0) throws ServletException {

sessionIdsList = new ArrayList<String>();
sessionIdsList = webContentDAOIF.fetchAllSessionIds();

}


@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {

System.out.println("List size: " + sessionIdsList.size());

HttpServletRequest httpRequest = (HttpServletRequest)request;

HttpSession session = httpRequest.getSession();
String userSessionId = session.getId();
System.out.println("Filter: " + userSessionId);

if(sessionIdsList.size() !=0 && sessionIdsList.contains(userSessionId)) {

System.out.println("There is same sessionID");

} else {

System.out.println("Hello anonim");

}

RequestDispatcher dispatcher = request.getRequestDispatcher("main");
dispatcher.forward(request, response);


}


@Override
public void destroy() {


}


}

我有 spring 上下文 xml 文件,我在其中声明了我的所有 bean:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="url"><value>jdbc:mysql://localhost:3306/contentDB</value></property>
<property name="username"><value>root</value></property>
<property name="password" ><value>25051988</value></property>

</bean>


<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>user.hbm.xml</value>
<value>userEvents.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>

<prop key="hibernate.hbm2ddl.auto">update</prop>

<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">15</prop>
</props>

</property>

</bean>


<bean id="webContentDAOImpl" class="demidov.pkg.persistence.WebContentDAOImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>


<tx:annotation-driven/>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>


<bean id="userFilter" class="demidov.pkg.web.UserFilter">
<property name="webContentDAOIF" ref="webContentDAOImpl"/>
</bean>

我还在我的 web.xml 中声明了过滤器:

 <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/*-context.xml</param-value>
</context-param>

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

<filter>
<filter-name>userFilter</filter-name>
<filter-class>demidov.pkg.web.UserFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>userFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>initialContentRendering</servlet-name>
<servlet-class>demidov.pkg.web.InitialContentRendering</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>initialContentRendering</servlet-name>
<url-pattern>/main</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>orderEventServlet</servlet-name>
<servlet-class>demidov.pkg.web.OrderEventServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>orderEventServlet</servlet-name>
<url-pattern>/event</url-pattern>
</servlet-mapping>

但是当我加载应用程序时它说:

   SEVERE: Exception starting filter userFilter
java.lang.NullPointerException
at demidov.pkg.web.UserFilter.init(UserFilter.java:35)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:273)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:254)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:372)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:98)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4562)
at org.apache.catalina.core.StandardContext$2.call(StandardContext.java:5240)
at org.apache.catalina.core.StandardContext$2.call(StandardContext.java:5235)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)

请帮我看看我做错了什么。谢谢大家。

最佳答案

您将获得 UserFilter 的 2 个不同实例,一个由 Servlet 容器管理,另一个由 Spring 管理 - 由 Servlet 容器创建的 UserFilter 当然会有一个空的 WebContentDAOIF。

解决方案是让 Spring 管理您的 UserFilter(并正确注入(inject) WebContentDAOIF)。然后使用Spring的DelegatingFilterProxy , 来处理最终委托(delegate)给您的 UserFilter 的过滤。

在您的 web.xml 中,删除您的 UserFilter 声明并改为声明 DelegatingFilterProxy。

<filter>
<filter-name>userFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>userFilter</param-value>
</init-param>
</filter>

关于java - 过滤器 - Servlets 和 Spring beans 集成 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19712397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com