SpringBoot项目练习

SpringBoot项目dy01

  • SpringBoot四种属性的注入
    写一个SpringBoot启动时自动读取的资源文件文件名为:application.properties 就不需要jdbc.properties这个资源文件了,从此SpringBoot的资源都放入这个文件里,以前缀区分 如:jdbc.
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/leyou
jdbc.username=root
jdbc.password=123456

创建一个属性读取类:
@ConfigurationProperties(prefix = “jdbc”)申明当前类为属性读取类,prefix = "jdbc”读取属性文件里前缀为jdbc的值

@ConfigurationProperties(prefix = "jdbc")
public class JdbcProperties {
    private String driverClassName;
    private String url;
    private String username;
    private String password;
get和set方法 注入   
 }

@Configuration//申明这是一个java配置类
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfig {
@Autowired//第一种属性注入方法
private JdbcProperties jdbcProperties;

/**public JdbcConfig(JdbcProperties jdbcProperties) {
    this.jdbcProperties = jdbcProperties;
}第二种方法构造方法注入*/

@Bean//申明为Bean将返回值注入容器
//@ConfigurationProperties(prefix = "jdbc")//第四种方法 直接在方法上读取资源文件
public DataSource dataSource(JdbcProperties jdbcProperties){//第三种方法形参注入
    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;
}

注:四种注入方法:
第一种:@Autowired

@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {

    @Autowired
    private JdbcProperties jdbcProperties;

    @Bean
    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
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {

    private JdbcProperties jdbcProperties;

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

    @Bean
    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
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {

    @Bean
    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方法

@Configuration
public class JdbcConfiguration {
    
    @Bean
    // 声明要注入的属性前缀,SpringBoot会自动把相关属性通过set方法注入到DataSource中
    @ConfigurationProperties(prefix = "jdbc")
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        return dataSource;
    }
}
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页