欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

bean属性注入的4种方式

程序员文章站 2022-04-07 18:24:02
以数据库连接为例,测试bean注入的几种方式JdbcProperties文件:jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://127.0.0.1:3306/xxxjdbc.username=rootjdbc.password=xxx@Configuration//声明一个类是一个java配置类, 相当于xml配置文件@EnableConfigurationProperties(JdbcProperties.clas...

以数据库连接为例,测试bean属性注入的几种方式
1.@Autowired注入
2.构造方法注入
3.@Bean方法形参注入
4.直接在@Bean方法上使用@ConfigurationProperties(prefix=“jdbc”)

JdbcProperties文件:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/xxx
jdbc.username=root
jdbc.password=xxx

@Configuration//声明一个类是一个java配置类, 相当于xml配置文件
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
    @Autowired
    private JdbcProperties jdbcProperties;
    @Value("${jdbc.driverClassName}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
       @Bean//把方法的返回值注入到spring 容器
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(this.driverClassName);
        dataSource.setUrl(this.url);
        dataSource.setUsername(this.username);
        dataSource.setPassword(this.password);
        return dataSource;
    }

application.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/leyou
jdbc.username=root
jdbc.password=123

构造方法注入

@Configuration//声明一个类是一个java配置类, 相当于一个xml配置文件
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
    //  @Autowired
    private JdbcProperties jdbcProperties;

    public JdbcConfiguration(JdbcProperties jdbcProperties) {
        this.jdbcProperties = jdbcProperties;
    }

    @Bean//把方法的返回值注入到spring 容器
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(this.jdbcProperties.getDriverClassName());
        dataSource.setUrl(this.jdbcProperties.getUrl());
        dataSource.setUsername(this.jdbcProperties.getUsername());
        dataSource.setPassword(this.jdbcProperties.getPassword());
        return dataSource;
    }
}

set方式注入

@ConfigurationProperties(prefix = "jdbc" )
public class JdbcProperties {

    private String driverClassName;
    private String url;
    private String username;
    private String password;

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
@Configuration//声明一个类是一个java配置类, 相当于一个xml配置文件
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
    @Autowired
    private JdbcProperties jdbcProperties;

    

    @Bean//把方法的返回值注入到spring 容器
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(this.jdbcProperties.getDriverClassName());
        dataSource.setUrl(this.jdbcProperties.getUrl());
        dataSource.setUsername(this.jdbcProperties.getUsername());
        dataSource.setPassword(this.jdbcProperties.getPassword());
        return dataSource;
    }
}

传参注入

@Configuration//声明一个类是一个java配置类, 相当于一个xml配置文件
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
//    @Autowired
//    private JdbcProperties jdbcProperties;
//
//    public JdbcConfiguration(JdbcProperties jdbcProperties) {
//        this.jdbcProperties = jdbcProperties;
//    }

    @Bean//把方法的返回值注入到spring 容器
    public DataSource dataSource(JdbcProperties jdbcProperties) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName( jdbcProperties.getDriverClassName());
        dataSource.setUrl( jdbcProperties.getUrl());
        dataSource.setUsername( jdbcProperties.getUsername());
        dataSource.setPassword( jdbcProperties.getPassword());
        return dataSource;
    }
}

最简介的方式:要保证返回值有相应的set和get方法,并且配置文件的名称要和返回值属性名称相同

@Configuration//声明一个类是一个java配置类, 相当于一个xml配置文件
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
//    @Autowired
//    private JdbcProperties jdbcProperties;
//
//    public JdbcConfiguration(JdbcProperties jdbcProperties) {
//        this.jdbcProperties = jdbcProperties;
//    }

    @Bean//把方法的返回值注入到spring 容器
    @ConfigurationProperties(prefix = "jdbc")
    public DataSource dataSource(JdbcProperties jdbcProperties) {
        DruidDataSource dataSource = new DruidDataSource();
        return dataSource;
    }
}


本文地址:https://blog.csdn.net/qq_40971668/article/details/110260733