【问题标题】:Spring datasource bean giving null pointer exception when it is used in controllerSpring数据源bean在控制器中使用时给出空指针异常
【发布时间】:2012-12-26 21:13:58
【问题描述】:

我在 applicationContext.xml 中使用 spring 的 BasicDataSource 如下:

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

    <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/school" />
        <property name="username" value="root" />
        <property name="password" value="" />
        <property name="initialSize" value="5" />
        <property name="maxActive" value="10" />
    </bean>
</beans>

当我在控制器中使用这个 bean 时:

package admin.controller;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.inject.*;

@Controller
public class Welcome {

@Inject
BasicDataSource datasource;

private static final String WELCOME_PAGE = "welcome";

@RequestMapping("/")
public String welcome(ModelMap model){

    Connection con=null;
    PreparedStatement stmt =null;
    String testQuery = "INSERT INTO ADMIN(ID,USER_NAME,PASSWORD,FIRST_NAME,LAST_NAME) VALUES(?,?,?,?,?)";
    try {
        con = datasource.getConnection();
        stmt = con.prepareStatement(testQuery);

        stmt.setInt(1, 4);
        stmt.setString(2, "ij");
        stmt.setString(3, "kl");
        stmt.setString(4, "mn");
        stmt.setString(5, "op");

        stmt.execute();

        //con.commit();
    } catch (SQLException e) {
        e.printStackTrace();
    } 
    finally{
            try {
                if(con!=null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    }
    return WELCOME_PAGE;
}   
}

然后它在以下行给了我一个空指针异常:datasource.getConnection();

连接详细信息是 100% 正确的,因为当我在控制器内创建 BasicDatasource 的新实例并使用其设置方法(例如:datasource.setDriverClassName 等)添加详细信息时,它会连接到数据库并毫无问题地执行查询.但是当我想从应用程序上下文中使用 bean 时,它给了我一个空指针异常。

【问题讨论】:

    标签: mysql spring nullpointerexception datasource


    【解决方案1】:

    我遇到了另一个有趣的事实,当我用@Autowired 替换@Inject 时,它工作正常,我不必编写setter 方法。但是如果我使用@Inject,那么我必须编写一个setter方法来让它工作。这听起来很奇怪,但在我的情况下似乎是真的。

    【讨论】:

      【解决方案2】:

      我没有得到空指针异常的原因。但我确信我在绑定的某个地方犯了错误。因为当我删除 @Inject 并为数据源实现 setter 方法时,我没有得到任何其他异常。并且交易成功完成。如果您能指出问题,请告诉我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-31
        • 1970-01-01
        • 2018-07-05
        • 1970-01-01
        • 1970-01-01
        • 2015-04-27
        相关资源
        最近更新 更多