【Spring註解】@Condition條件註冊
[email protected]條件註冊
@Condition來指定一定條件下注冊元件對像
All Conditions that must match in order for the component to be registered.
所有的條件必須實現Condition介面,重寫matches方法,來決定元件是否註冊
配置類
@Configuration
public class ConditionConfig {
@Conditional(WindowsCondition.class)
@Bean("bill" )
public Person bill(){
return new Person("bill",10);
}
@Conditional(LinuxCondition.class)
@Bean("Linus")
public Person person(){
return new Person("Linus",10);
}
}
WindowsCondition 判斷windows環境
public class WindowsCondition implements Condition {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//獲取當前執行環境資訊
Environment environment = context.getEnvironment();
//獲取當前環境名稱
String osName = environment.getProperty("os.name");
return osName.contains("Windows");
}
}
LinuxCondition 判斷Linux環境
public class LinuxCondition implements Condition {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//獲取當前執行環境資訊
Environment environment = context.getEnvironment();
//獲取當前環境名稱
String osName = environment.getProperty("os.name");
return osName.contains("Linux");
}
}
測試(系統為Window 7)
@Test
public void person() {
Map<String, Person> beansOfType = annotationConfigApplicationContext.getBeansOfType(Person.class);
System.out.println("beansOfType = " + beansOfType);
}
設定vm的變數來模擬Linux環境,Run Configurations->VM options
新增 Dos.name=Linux
執行結果
相關推薦
【Spring註解】@Condition條件註冊
[email protected]條件註冊 @Condition來指定一定條件下注冊元件對像 All Conditions that must match in order
【Spring註解】1、Spring元件註冊
1、@Configuration: 指定該註解的類是個配置類,對應之前的配置檔案 2、@Bean: 指定該方法作為一個Bean元件,它會往容器中註冊一個Bean元件; 其中型別為返回值,id預設為方法名;
【Spring註解】FactoryBean註冊
5.FactoryBean 自定義Cat的工廠物件 CatFactoryBean /** * @author GaoYuzhe * @date 2018/3/13. */ publ
【Spring註解】3、屬性賦值
1、@Value 配置檔案 person.properties: person.nickName=張三 person.age=20 實體類 Person.java: @Data @AllArgsConstructor @NoArgsConstructor public class
【Spring註解】2、Bean生命週期
1、初始化和銷燬 通過@Bean註解的initMethod和destroyMethod屬性 InitializingBean介面、DisposableBean介面 可以使用JSR250: @PostConstruct:在Bean建立完成並且屬性
【Spring註解】@Configuration和@ComponentScan註解
Spring註解 一、組建註冊 包結構: [email protected]和@ComponentScan @ComponentScan的includeFilters
【Spring註解】——生命週期以及自動裝配
本文主要講解一下配合spring註解講解spring中bean的生命週期和自動裝配涉及到一些註解,如有理解偏頗之處,懇請各位大神指正。容器管理bean的生命週期bean的生命週期 bea
【Spring註解】四、自動裝配@[email protec
四、自動裝配 [email protected]&@Qualifier和@Primary @Autowried是Spring2.5定義的自動裝配的註解,它可以對類成員變數、方法及建構函式進行標註,完成自動裝配的工作。 使用 @Au
【Spring註解】@Scope 作用域和@Lazy懶載入
[email protected] 作用域和@Lazy懶載入 @Scope來表示註冊物件的作用域。 @Scope的四種類型 //多例模式,只有在使用類時才註冊物件
【Spring註解開發】元件註冊-使用@Configuration和@Bean給容器中註冊元件
## 寫在前面 > 在之前的Spring版本中,我們只能通過寫XML配置檔案來定義我們的Bean,XML配置不僅繁瑣,而且很容易出錯,稍有不慎就會導致編寫的應用程式各種報錯,排查半天,發現是XML檔案配置不對!另外,每個專案編寫大量的XML檔案來配置Spring,也大大增加了專案維護的複雜度,往往很多
【Spring註解驅動開發】元件註冊-@ComponentScan-自動掃描元件&指定掃描規則
## 寫在前面 > 在實際專案中,我們更多的是使用Spring的包掃描功能對專案中的包進行掃描,凡是在指定的包或子包中的類上標註了@Repository、@Service、@Controller、@Component註解的類都會被掃描到,並將這個類注入到Spring容器中。Spring包掃描功能可以使
【Spring註解驅動開發】在@Import註解中使用ImportBeanDefinitionRegistrar向容器中註冊bean
## 寫在前面 > 在前面的文章中,我們學習瞭如何使用@Import註解向Spring容器中匯入bean,可以使用@Import註解快速向容器中匯入bean,小夥伴們可以參見《[【Spring註解驅動開發】使用@Import註解給容器中快速匯入一個元件](https://mp.weixin.qq.co
【Spring MVC】註解和配置檔案的程式碼比較
當我們在類檔案裡寫了方法,怎麼被程式知道並呼叫呢?一般有兩種方法: 配置檔案 註解 下面小編就以親自敲的例子“SpringMVC_Test”為例來簡單說說。 配置檔案篇 在springmvc.xml中這樣寫: 在controller包下的類中這樣寫: 註
【Spring學習】spring註解自動注入bean
Spring mvc註解用到的配置: <!-- 啟用spring mvc 註解 --> <context:annotation-config /> <context:component-scan base-package
【Spring三】使用註解配置bean
原理: 1、當啟動spring容器的時候,給spring容器的bean建立物件 2、當spring容器解析到<context:annotation-config></context:annotation-config>的時候, spring容器掃描
@Autowired註解【Spring入門】
前一篇文章寫了在使用Spring框架過程中,通過在頭標籤中加入預設default-autowire來自動裝配,但是該方法有一定侷限性,就是需要被裝配類中有setter方法,且setter方法有一定要求
【Spring Boot】(33)、SpringBoot事務管理@Transactional註解原理
1、依賴包 1.1、 SpringBoot中的依賴包 眾所周知,在SpringBoot中凡是需要跟資料庫打交道的,基本上都要顯式或者隱式新增jdbc的依賴: <dependency> <groupId>org.springfram
【Spring實戰】Spring註解配置工作原理原始碼解析
一、背景知識在【Spring實戰】Spring容器初始化完成後執行初始化資料方法一文中說要分析其實現原理,於是就從原始碼中尋找答案,看原始碼容易跑偏,因此應當有個主線,或者帶著問題、目標去看,這樣才能最大限度的提升自身程式碼水平。由於上文中大部分都基於註解進行設定的(Spri
【Spring Cloud】原始碼-Eureka客戶端如何載入Eureka服務註冊中心列表
這部分原始碼涉及到兩個類: 1. com.netflix.discovery.endpoint.EndpointUtils 2. org.springframework.cloud.netflix.eureka.EurekaClientConfigBean 我
【spring springmvc】springmvc使用註解宣告控制器與請求對映
# 概述 **註解:** 在Spring中儘管使用XML配置檔案可以實現Bean的裝配工作,但如果應用中Bean的數量較多,會導致XML配置檔案過於臃腫,從而給維護和升級帶來一定的困難。 從JDK 5開始提供了名為Annotation(註解)的功能,Spring正是利用這一特性,Spring逐步完善對Anno