springboot 的多种配置形式
- java 配置次要靠 java 类和一些注解,比拟罕用的注解有:
- @Configuration:申明一个类作为配置类,代替 xml 文件
- @Bean:申明在办法上,将办法的返回值退出 Bean 容器,代替 标签
- @Value:根本类型或 String属性注入
- @PropertySource:指定内部属性文件
- 前面以 Druid 连接池配置为例,数据库名称为 springboot_test
形式一
<!--pom.xml -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
# src/resources/jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/bos
jdbc.username=root
jdbc.password=123456
//src\main\java\com\itheima\config\DruidConfig.java
@Configuration
@PropertySource("classpath:jdbc.properties")
public class DruidConfig {@Value("${jdbc.url}")
String url;
@Value("${jdbc.driverClassName}")
String driverClassName;
@Value("${jdbc.username}")
String username;
@Value("${jdbc.password}")
String password;
@Bean
public DataSource dataSource() {DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
解读:
- @Configuration:申明咱们 DruidConfig 是一个配置类
- @PropertySource:指定属性文件的门路是: classpath:jdbc.properties
- @Value 为属性注入值(只能是根本类型或 String)
- @Bean 将 dataSource() 办法申明为一个注册 Bean 的办法,Spring 会主动调用该办法,将办法的返回值退出 Spring 容器中。
形式二
<!--pom.xml -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- ============ 不增加在 IDEA 会报红,但并不影响性能 ================= -->
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--============================================================== -->
# src/resources/application.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/bos
jdbc.username=root
jdbc.password=123456
//src\main\java\com\itheima\config\DruidConfig.java
@ConfigurationProperties(prefix = "jdbc")
public class DruidProperties {
private String url;
private String driverClassName;
private String username;
private String password;
public String getUrl() {return url;}
public void setUrl(String url) {this.url = url;}
public String getDriverClassName() {return driverClassName;}
public void setDriverClassName(String driverClassName) {this.driverClassName = driverClassName;}
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;}
}
//src\main\java\com\itheima\config\DruidConfig.java
@Configuration
@EnableConfigurationProperties(DruidProperties.class)
public class DruidConfig {
@Bean
public DataSource dataSource(DruidProperties dp) {DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(dp.getDriverClassName());
dataSource.setUrl(dp.getUrl());
dataSource.setUsername(dp.getUsername());
dataSource.setPassword(dp.getPassword());
return dataSource;
}
}
解读:
- @ConfifigurationProperties 注解申明以后类为属性读取类,在类上定义各个属性,名称必须与属性文件中 jdbc. 前面局部统一。
- @EnableConfigurationProperties()申明要应用的属性读取类,应用该类有三种注入形式
1. @Autowired 注入
//src\main\java\com\itheima\config\DruidConfig.java
@Configuration
@EnableConfigurationProperties(DruidProperties.class)
public class DruidConfig {
@Autowired
private DruidProperties dp;
@Bean
public DataSource dataSource() {DruidDataSource dataSource = new DruidDataSource();
//setter
return dataSource;
}
}
2. 构造函数注入
//src\main\java\com\itheima\config\DruidConfig.java
@Configuration
@EnableConfigurationProperties(DruidProperties.class)
public class DruidConfig {
private DruidProperties dp;
public DruidConfig(DruidProperties dp){this.dp = dp;}
@Bean
public DataSource dataSource() {DruidDataSource dataSource = new DruidDataSource();
//setter
return dataSource;
}
}
3. 作为 @Bean 的办法参数注入(本例应用)
//src\main\java\com\itheima\config\DruidConfig.java
@Configuration
@EnableConfigurationPerProperties(DruidProperties.class)
public class DruidConfig {
@Bean
public DataSource dataSource(DruidProperties dp) {DruidDataSource dataSource = new DruidDataSource();
//setter
return dataSource;
}
}
形式二通过属性读取类解决了 @Value 不能读取对象属性 (如 user.friend.name) 的问题,但仿佛就更加麻烦了
形式三(举荐应用)
事实上,如果一段属性只有一个 Bean 须要应用,咱们无需将其注入到一个类。
<!--pom.xml -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- ============ 不增加在 IDEA 会报红,但并不影响性能 ================= -->
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--============================================================== -->
# src/resources/application.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/bos
jdbc.username=root
jdbc.password=123456
@Configuration
public class DruidConfig {
@Bean
@ConfigurationProperties(prefix = "jdbc")
public DataSource dataSource() {return new DruidDataSource();
}
}
形式四
<!--pom.xml -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
# src/resources/application.properties
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/bos
spring.datasource.username=root
spring.datasource.password=123456
思考:为什么这种形式不须要配置类能够读取配置信息?
启动类跑 main 办法时候,查看 SpringApplication 构造方法,如下追踪
容易发现,它是从 META-INF/spring.factories 中获取类名信息,存储在一键多值的 Map 中,关上 spring.factories,debug 比照
发现键是文件蓝色局部,值是绿色局部,往回看不难发现它将这些获取的类都生成了实例,注入到 IOC 容器中。
关上 DataSourceProperties 发现这不是形式二吗?
点进 DataSourceProperties.class
总结:
当咱们增加依赖后,执行启动类时主动加载 DataSourceAutoConfiguration,读取 DataSourceProperties 类,依据默认的前缀 spring.datasource 在 application.xml 中读取信息
最初
把握这些 springboot 的配置形式,会让你在工作中解决事件来更轻松,感觉文章对你有帮忙的话还请给我点个赞,你的反对,就是我创作最大的能源!