簡體   English   中英

從自動裝配的Bean獲取數據源時出現空指針異常

[英]Null pointer exception while getting datasource from autowired bean

我剛剛創建了一個spring mvc項目,除了web.xmlspring-servlet.xmlListnerClass.java之外,里面什么都沒有。
我想在服務器啟動期間初始化系統屬性。 為此,我創建了一個servlet上下文

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();

}

我在這一行得到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>

spring-servlet.xml

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

在部署描述符中注冊的ServletContextListener對象不是由Spring管理的,而是由Servlet容器管理的。 因此,Spring無需向其注入任何bean。

經驗法則是,如果您有一個@Autowired字段目標,並且為null ,則不涉及Spring。 如果Spring無法解析@Autowired目標,它將拋出各種異常。

有解決方法:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM