繁体   English   中英

无法通过web.xml中的ContextLoaderListener初始化spring

[英]Unable to initialize spring through ContextLoaderListener in web.xml

请参考问题- 可以直接实例化休眠会话Factory,但不能通过Spring完成

在这个问题上,我根本无法发动春天。 在有人建议我应该使用ApplicationContext来启动spring之后,问题得到解决。 但是这种方法要求我每次必须获取实例时都使用ApplicationContext,然后使用bean。 我想要的是春天将所有的豆子注入它们的位置,然后我可以在任何需要的地方使用它们。 所以我把这个放到我的web.xml中

<listener> <!-- For struts2-spring to work -->
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>  

但这根本不适合我。 无论何时调用了bean注入的对象,它都会给我空指针异常。

标准的web.xml文件应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

将您的Web xml版本与此版本进行比较,看看包含这些bean的xml文件是否已正确初始化。 您可以分析应用程序启动时打印的堆栈跟踪。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM