Bean 未在 Spring 中初始化

Bean Not getting initialized in Spring

我有一个 class,它正在创建数据并将数据插入 table.Please 在下面找到我的代码:

Class 数据库连接:

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;
    }
}

当 运行 这个 class 我收到以下错误:

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

使用@autowired.

@autowired
private DBConnection dbConnection;

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