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

类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。

最佳答案

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

return new SQLOperationJavaConfig(dbConnection());


Spring将查看已经为DBConnection创建的bean

10-06 15:48