2014-01-06 1 views
0

방금 ​​봄 Mvc 프로젝트를 만들었으니 web.xml, spring-servlet.xmlListnerClass.java을 제외하고는 아무 것도 없습니다.
서버를 시작하는 동안 시스템 속성을 초기화하고 싶습니다. 이를 위해 I 서블릿 컨텍스트autowired bean에서 데이터 소스를 가져 오는 동안 널 포인터 예외가 발생했습니다.

public class ListnerClass implements ServletContextListener{ 

    @Autowired 
    private DataSource dataSource; 

    @Override 
    public void contextDestroyed(ServletContextEvent arg0) { 

    } 

    @Override 
    public void contextInitialized(ServletContextEvent arg0) { 

     /* code for initializing system properties */ 
    Connection conn=dataSource.getConnection(); 

} 

I이 줄 NullPointerExceptin 얻을 생성 Connection conn=dataSource.getConnection();

web.xml의

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

    <servlet> 
     <servlet-name>spring</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>spring</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/spring-servlet.xml 
     </param-value> 
    </context-param> 
    <listener> 
     <listener-class>com.infocentercache.manager.ListnerClass</listener-class> 
    </listener> 

</webapp> 

ServletContextListener 객체 등록

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

<context:annotation-config/> 

    <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"> 

     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/infocenter" /> 
     <property name="username" value="root" /> 
     <property name="password" value="gaurav" /> 
    </bean> 
</beans> 
+0

확실하지,하지만 봄 런타임이 생활에 온다도 전에 컨텍스트 리스너는 초기화됩니다. 따라서 자동 배선이 작동하지 않을 수 있습니다. 데이터 소스 인스턴스는 자동으로 초기화되지 않습니다. 당신은 JNDI api가 직접 문맥 초기화 메소드에서 데이터 소스를 검색해야만한다. –

답변

3

스프링 servlet.xml에 Spring에 의해 관리되지 않는 배치 서술자에서는 Servlet Con에 의해 관리된다. tainer. 따라서 Spring은 bean을 주입하지 않습니다.

어림짐작은 @Autowired 필드 타겟이 있고 null 일 경우 스프링이 관련되지 않는다는 것입니다. @Autowired 대상을 해결할 수없는 경우 Spring에서 모든 종류의 예외를 throw합니다.

는 해결 방법이 있습니다

+0

yeap! ServletContextListener가 "컴퍼넌트" – venergiac

관련 문제