2013-03-06 30 views
0

初始化春季請參閱問題 - Can instantiate hibernate session Factory directly but cannot do it through spring無法通過的ContextLoaderListener在web.xml

在這個問題我無法啓動春季可言。有人建議我應該使用ApplicationContext來啓動spring,這個問題已經解決了。但是這種方法需要我使用ApplicationContext,然後每次我必須獲得一個實例時使用這個bean。我想要的是,春天將所有豆子注入他們的地方,我可以隨時隨地使用它們。所以我把它放到我的web.xml中

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

但是這根本不適用於我。無論注入bean的對象在哪裏調用,它都會給我一個空指針異常。

回答

0

一個標準的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> 

比較你的網站的XML版本的這一個,看看是否含有這些豆你的XML文件得到正確初始化。您可以分析應用程序啓動時打印的堆棧跟蹤。

+0

這是一個正確的答案。但我的問題在於我無法找到的其他地方。我在另一個IDE中從頭開始了一個新項目,並且工作正常。 – ishan 2013-03-06 12:26:46