简体   繁体   English

Spring JDBC将DataSource注入另一个bean

[英]Spring JDBC Injecting DataSource into another bean

I am new to Spring. 我是Spring的新手。 While learning it, I decided to play around with JDBC. 在学习它的同时,我决定使用JDBC。

Cutting short, I have two classes: 简而言之,我有两节课:

Class 1 contains this: 第1类包含以下内容:

import javax.sql.DataSource;
public class class1 extends class2 {

    private Connection con;
    private DataSource dataSource;

    public void setDataSource(DataSource ds) {
        dataSource = ds;
    }

public void getConnection(){
login(username,password,url);
con = dataSource.getConnection();
}
}

Class 1 is basically where I ask user to enter the username/password/ur and then call the method from class2 passing these details. 基本上,在类1中,我要求用户输入用户名/密码/ ur,然后从类2中调用方法,并传递这些详细信息。 I want to inject value of DataSource from this class to class1. 我想将此类的DataSource值注入class1。 So far this is my code for class2: 到目前为止,这是我对class2的代码:

import javax.sql.DataSource;

import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

abstract class class2{
public void login(String username, String password, String url){
GenericApplicationContext context = new GenericApplicationContext();
            DefaultListableBeanFactory factory = (DefaultListableBeanFactory) context.getBeanFactory();

            BeanDefinitionBuilder bean1 = BeanDefinitionBuilder
                    .rootBeanDefinition("org.springframework.jdbc.datasource.DriverManagerDataSource");
            bean1.addPropertyReference("driverClassName", "dataSource");
            bean1.addPropertyValue("driverClassName", "com.mysql.jdbc.Driver");
            bean1.addPropertyReference("url", "dataSource");
            bean1.addPropertyValue("url", "url");
            bean1.addPropertyReference("username", "dataSource");
            bean1.addPropertyValue("username", username);
            bean1.addPropertyReference("password", "dataSource");
            bean1.addPropertyValue("password", password);
            bean1.registerBeanDefinition("dataSource", bean1.getBeanDefinition());

            BeanDefinitionBuilder bean2 = BeanDefinitionBuilder.rootBeanDefinition("class2");
            bean2.addPropertyValue("dataSource", "getCon");
            context.refresh();
            bean2.addPropertyValue("dataSource", new RuntimeBeanReference("dataSource"));

            factory.registerBeanDefinition("getCon", bean2.getBeanDefinition());

}
}

However, when I try to execute the method getConnection() from class1, it gives NPE. 但是,当我尝试从class1执行方法getConnection()时,它将得到NPE。 Can anyone tell me correct way to do this? 谁能告诉我正确的方法?

Also, I want to do this task programmically (not by using xml). 另外,我想以编程方式执行此任务(而不是使用xml)。

If you want to do it in Spring then you need to declare a bean definition in application context xml for creating data source and inject to you class. 如果要在Spring中执行此操作,则需要在应用程序上下文xml中声明一个bean定义以创建数据源并注入您的类。

Alternatively, you can create data source using spring jdbc. 另外,您可以使用spring jdbc创建数据源。 Check out this link: 查看此链接:

http://examples.javacodegeeks.com/enterprise-java/spring/jdbc/create-data-source-for-jdbctemplate/ http://examples.javacodegeeks.com/enterprise-java/spring/jdbc/create-data-source-for-jdbctemplate/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM