目錄
一、@PropertySource
二、@ImportResource
①SpringConfig?(Spring框架全注解)
②@ImportResource注解實現
三、@Bean
四、多配置文件
多Profile文件的使用
文件命名約定:
激活Profile:
YAML文件支持多文檔塊:
五、配置加載位置優先級
對于SpringBoot項目來說,有3種常用來綁定指定配置文件的注解:?????@PropertySource,@ImportResource和@Bean。
想看默認配置綁定看這篇:SpringBoot 第一課(Ⅱ)配置文件注入-CSDN博客文章瀏覽閱讀500次,點贊7次,收藏14次。需要為每個屬性單獨指定,適用于簡單的配置或單個屬性的注入。屬性上,Spring Boot 會處理這種命名風格的差異。使用@ConfigurationProperties注入。),而松散綁定支持這三種名字互通,就比如配置文件里的。允許一次性注入整個對象,適用于復雜的配置結構。Java中常見命名規則有駝峰命名法(實現上面操作后,就可以通過訪問“使用注解時,在處理復雜情況時,
https://blog.csdn.net/m0_74977981/article/details/146282864?spm=1001.2014.3001.5501
一、@PropertySource
配置文件
在里面編寫:
student1.name=admin
student1.age=20
student1.id=1
Java實體類
package com.qcby.entity1;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;@Component
@PropertySource(value="classpath:student1.properties")
@ConfigurationProperties(prefix="student1")
public class Student1 {private String name;private int age;private int id;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int getId() {return id;}public void setId(int id) {this.id = id;}@Overridepublic String toString() {return "Student1{" +"name='" + name + '\'' +", age=" + age +", id=" + id +'}';}
}
現在我們需要測試這個類:
在test路徑下測試:
注意這里SpringBoot項目在創建時會自動生成這么一個test類(如下):
為了方便查看,我們再在它旁邊創建一個StudentTest類:
package com.qcby.sbdemotest01;import com.qcby.entity1.Student1;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;/*** 從Spring Boot 2.2版本開始,@SpringBootTest 已經包含了 SpringRunner 的功能,* 因此在大多數情況下,工程師不再需要顯式地使用 @RunWith(SpringRunner.class)。*/
//@RunWith(SpringRunner.class)
//我用的是2.7.0,所以不用顯式這一行【我在這里顯式加上它的話,日志會報錯警告(雖然能用吧)】(但是要知道有這么一個注釋)
@SpringBootTest
public class StudentTest {@AutowiredStudent1 student1;@Testvoid contextLoads() {System.out.println(student1);}}
得到結果:
注意: 在Spring中,
classpath
是一個特殊的路徑前綴,在SpringBoot項目中,默認代表的根目錄一般是src/main/resources。
①規范情況
即假設一個項目的目錄結構是:
src/ ├── main/ │ ├── java/ │ │ └── com/example/Student1.java │ └── resources/ │ ├── student1.properties │ └── beans.xml
那么會在實體類中這么書寫:
@PropertySource("classpath:config.properties")
②嵌套子目錄
src/ ├── main/ │ ├── java/ │ │ └── com/example/Student1.java │ └── resources/ │ └── config/ │ ├── student1.properties │ └── beans.xml
此時在實體類中定義時,就要加上目錄路徑前綴:
@PropertySource("classpath:config/config.properties")
不只是@PropertySource,其他用到
classpath
的地方同樣遵循這個準則。
二、@ImportResource
在講@ImportResource這里時,結合Spring框架全注解實現配置一起。
詳文參照:Spring(二)---基于注解的方式實現Bean管理和注入屬性_有沒有注解可以實現bean動態注入,配置項打開注入,關閉則剔除-CSDN博客文章瀏覽閱讀488次,點贊9次,收藏6次。①:注解是代碼特殊標記,格式:@注解名稱(屬性名稱=屬性值,屬性名稱=屬性值...)②:使用注解,注解作用在類上面,方法上面,屬性上邊③:使用注解的目的:簡化XML配置。_有沒有注解可以實現bean動態注入,配置項打開注入,關閉則剔除
https://blog.csdn.net/m0_74977981/article/details/144626708?spm=1001.2014.3001.5501
①SpringConfig
?(Spring框架全注解)
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ComponentScan;@Configuration
@ComponentScan(value = "com.qcby")
public class SpringConfig {
}
-
@Configuration
注解:標識這是一個配置類,Spring容器會掃描這個類中定義的Bean。 -
@ComponentScan
注解:指定Spring容器掃描的包。在這個例子中,Spring容器會掃描com.qcby
包及其子包中的所有Spring組件(如@Component
,@Service
,@Repository
等),并將它們注冊為Spring容器中的Bean。
這種方式是基于Java的配置,它允許項目通過Java代碼來定義Spring容器的配置(并且是范圍性的)。
②@ImportResource注解實現
配置類
@Configuration
@ImportResource("classpath:applicationContext.xml")
public class AppConfig {// 配置類中的其他配置
}
resources路徑下的:applicationContext.xml
<!-- applicationContext.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- <context:component-scan base-package="com.qcby" /> --><!-- 掃描可以選擇范圍掃描或是逐一注入 --><!-- 逐一聲明Bean --><bean id="myBean" class="com.qcby.MyBean" />
</beans>
@Configuration
注解:同樣標識這是一個配置類。-
@ImportResource
注解:用于導入一個XML配置文件。在這個例子中,它導入了類路徑下的applicationContext.xml
文件。這種方式允許你將配置信息放在XML文件中,而不是Java代碼中。
三、@Bean
在Spring Boot中,推薦使用全注解的方式來配置和添加組件到Spring容器中。這種方式不僅代碼更簡潔,而且易于維護和理解。下面我將詳細解釋如何使用 @Configuration
和 @Bean
注解來實現這一過程。
接口
package com.qcby.service;public interface HelloService {void sayHello();
}
Java實現類
package com.qcby.service;import org.springframework.stereotype.Service;@Service
public class SimpleHelloService implements HelloService {@Overridepublic void sayHello() {System.out.println("Hello, Spring Boot!");}
}
在這個例子中,SimpleHelloService
類被標記為 @Service
,這表示它是一個服務層組件,Spring會自動將其注冊為一個Bean。
MyAppConfig類:
我們創建一個配置類 MyAppConfig
,并在其中使用 @Bean
注解來聲明一個Bean。
package com.qcby.service;import com.qcby.service.HelloService;
import com.qcby.service.SimpleHelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;@Configuration
public class MyAppConfig {@Bean@Primary // 指定為首選beanpublic HelloService helloService02() {System.out.println("配置類@Bean給容器中添加組件了...");return new SimpleHelloService();}
}
在這個配置類中,我們使用 @Bean
注解聲明了一個 HelloService
類型的Bean。這意味著Spring容器將創建一個 SimpleHelloService
實例,并將其注冊為 HelloService
類型的Bean。
?創建Test類確認是否調用正確:
package com.qcby.sbdemotest01;import com.qcby.service.HelloService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class HelloServiceTest {@Autowiredprivate HelloService helloService;@Testvoid contextLoads() {helloService.sayHello();}}
得到結果:
四、多配置文件
Spring Boot 支持多環境配置,允許為不同的環境(如開發、測試、生產等)創建不同的配置文件。這是通過使用不同的配置文件來實現的,這些文件可以是 .properties
格式或 .yml。
多Profile文件的使用
文件命名約定:
對于 .properties
文件,可以使用 application-{profile}.properties
來命名不同的配置文件。例如:
-
application-dev.properties
:開發環境的配置文件。 -
application-test.properties
:測試環境的配置文件。 -
application-prod.properties
:生產環境的配置文件。
默認情況下,如果沒有指定環境,Spring Boot 會使用 application.properties
文件。
激活Profile:
可以通過多種方式激活特定的Profile:
-
在啟動應用時,通過命令行參數指定:
java -jar your-application.jar --spring.profiles.active=dev 【終端】
-
在
application.properties
或application.yml
文件中指定:spring.profiles.active=dev【配置文件中】
-
在環境變量中設置:
export SPRING_PROFILES_ACTIVE=dev
(Linux/Mac)或set SPRING_PROFILES_ACTIVE=dev
(Windows)【操作系統級別】
YAML文件支持多文檔塊:
YAML文件支持多文檔塊方式,這意味著可以在一個YAML文件中定義多個配置塊,每個塊對應一個Profile。例如:
spring:profiles: devapplication:name: MyApplication---
spring:profiles: testapplication:name: MyTestApplication---
spring:profiles: prodapplication:name: MyProductionApplication
在這個例子中,我們定義了三個配置塊,每個塊對應一個不同的Profile(開發、測試、生產)。Spring Boot會根據激活的Profile加載相應的配置塊。
五、配置加載位置優先級
Spring Boot在啟動時會按照一定的順序掃描并加載配置文件,這些配置文件的位置和優先級如下:
-
file:./config/
:項目根目錄下的config
文件夾中。 -
file:./
:項目根目錄中。 -
classpath:/config/
:類路徑下的config
文件夾中。 -
classpath:/
:類路徑根目錄中。
Spring Boot會從這四個位置加載application.properties
或application.yml
文件作為默認配置文件,優先級由高到低,高優先級的配置會覆蓋低優先級的配置。這意味著如果同一配置項在多個文件中出現,那么優先級高的文件中的配置將被使用。
/myproject
? ? /src
? ? ? ? /main
? ? ? ? ? ? /java
? ? ? ? ? ? ? ? /com
? ? ? ? ? ? ? ? ? ? /yourcompany
? ? ? ? ? ? ? ? ? ? ? ? /YourApplication.java
? ? ? ? ? ? /resources
? ? ? ? ? ? ? ? application.properties
? ? /config
? ? ? ? application.properties
? ? application.properties
即: