Bean没有在Spring中初始化

时间:2017-06-18 11:43:14

标签: java spring spring-java-config

我有一个创建数据并将数据插入表中的类。请在下面找到我的代码:

类DBConnection:

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnection {
    public static Connection getDBConnection() {
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "expenseCalc";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "*********";
        String password = "*******";
        Connection conn = null;
        try {
            Class.forName(driver).newInstance();
            conn = (Connection) DriverManager.getConnection(url + dbName, userName, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
}

SpringConfig.class

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;

    @Configuration
    public class SpringConfig {
        @Bean("sqloperations")
        public SQLOperationJavaConfig sqLoperationJavaConfig() {
            DBConnection dbConnection=new AppJavaConfig<DBConnection>().getbean("dbconnection"); 
            return new SQLOperationJavaConfig(dbConnection);
        }
        @Bean("dbconnection")
        public DBConnection dbConnection() {
            return new DBConnection();
        }
    }

SQLOperationJavaConfig.class

public class SQLOperationJavaConfig {
    private DBConnection dbConnection;
    public SQLOperationJavaConfig(DBConnection dbConnection) {
        this.dbConnection=dbConnection;
    }
    private Connection conn;
    public void createTable() throws SQLException {
        conn = dbConnection.getDBConnection();
        String sql="drop table if exists dummy";
        String sql1 = "create table dummy (id int,name varchar(20))";
        Statement statement = conn.createStatement();
        statement.execute(sql);
        statement.execute(sql1);
        System.out.println("Table Created!!");
    }

    public void insertData() throws SQLException {
        conn = dbConnection.getDBConnection();
        String sql = "insert into dummy values(12,'Sunny')";
        Statement statement = conn.createStatement();
        statement.execute(sql);
        System.out.println("Record Inserted!!");
    }
}

AppJavaConfig.java

public class AppJavaConfig<T> {
    static ApplicationContext context;
    public static void main(String[] args) throws SQLException {
        context = new AnnotationConfigApplicationContext(SpringConfig.class);
        SQLOperationJavaConfig sqLoperationJavaConfig = (SQLOperationJavaConfig) context.getBean("sqloperations");
        sqLoperationJavaConfig.createTable();
        sqLoperationJavaConfig.insertData();
    }
    public  T getbean(String beanName) {
        T t=(T) context.getBean(beanName);
        return t;
    }
}

运行此课程时,我收到以下错误:

Caused by: java.lang.NullPointerException
    at com.src.springproj.AppJavaConfig.getbean(AppJavaConfig.java:17)
    at com.src.springproj.SpringConfig.sqLoperationJavaConfig(SpringConfig.java:10)

getBean方法没有返回“dbconnection”Bean。

2 个答案:

答案 0 :(得分:0)

您不需要此getBean方法。只需将SQLOperationsJavaConfig的初始化程序更改为以下内容:

return new SQLOperationJavaConfig(dbConnection());

Spring将查看已为DBConnection创建的bean

答案 1 :(得分:0)

使用 @autowired

@autowired
private DBConnection dbConnection;

因为 @Autowired 注释可用于在setter方法上自动装配bean,就像 @Required 一样注释,构造函数,具有任意名称和/或多个参数的属性或方法。