掌握這些springboot的配置方式,讓你工作效率翻個倍!
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的配置方式,會讓你在工作中處理事情來更輕鬆,覺得文章對你有幫助的話還請給我點個贊,你的支援,就是我創作最大的動力!