### @Profile
@Profile注解應用在了類級別上。它會告訴Spring這個配置類中的bean只有在dev profile激活時才會創建。如果dev profile沒有激活的話,那么帶有@Bean注解的方法都會被忽略掉
* ### java config配置profile
```
@Configuration
public class DatasourceConfig {
@Bean
@Profile("dev")
public DataSource devDataSource(){
return new DataSource("dev datasource");
}
@Bean
@Profile("prod")
public DataSource proDataSource(){
return new DataSource("prod datasource");
}
}
```
### XML配置profile
```
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 定義開發的profile -->
<beans profile="development">
</beans>
<!-- 定義生產使用的profile -->
<beans profile="produce">
</beans>
</beans>
```
### 激活Profile方式
* spring.profiles.active=prod
如果當spring.profiles.active屬性被設置時,那么Spring會優先使用該屬性對應值來激活Profile。當spring.profiles.active沒有被設置時,那么Spring會根據spring.profiles.default屬性的對應值來進行Profile進行激活。如果上面的兩個屬性都沒有被設置,那么就不會有任務Profile被激活,只有定義在Profile之外的Bean才會被創建
* 單元測試
```
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = DatasourceConfig.class)
@ActiveProfiles("prod")
public class DataSourceConfigTest {
@Autowired
private DataSource dataSource;
@Test
public void testProfile(){
System.err.println("dataSource==>" + dataSource.getSource());
}
}
```
- 概述
- Spring的使命
- 環境變量
- spring架構
- Spring各版本特性
- Spring3.1新特性
- spring頂級項目
- spring基礎
- spring環境變量
- 依賴注入
- spring獲取bean方法
- BeanFactory vs FactoryBean
- JavaBean裝配
- XML顯式配置
- 基于JAVA配置
- 自動化裝配bean
- SpringBean的作用域
- Spring應用上下文實現
- springbean的生命周期
- 自定義Bean的創建與銷毀
- Spring容器啟動過程
- spring加載xsd文件的流程
- spring擴展接口
- Spring主要類功能說明
- spring事務管理
- 事務特性
- 數據庫事務隔離級別
- 事務隔離性問題
- spring事務隔離級別
- 事務傳播行為
- @Transactional
- 循環依賴
- 構造器注入
- 循環依賴原理
- spring循環依賴原理
- spring三級緩存
- Spring注解
- @Component
- @ComponentScan
- @Autowired
- @Import
- @ImportResource
- @Profile
- @Conditional
- @Qualifier
- @Scope
- @PropertySource
- @Value
- @EnableScheduling
- SpEL-Spring表達式
- Spring-AOP
- SpringAOP五種通知類型
- AOP術語
- SpringMVC
- MVC原理圖
- SpringMVC工作原理
- springboot
- @SpringCloudApplication
- springboot tomcat配置
- Spring Boot Starter POMs
- Spring Boot technical starters
- spring boot事件類型
- Springboot日志
- SpringCloud
- springcloud微服務解決方案
- 服務組件
- 注冊中心
- Eureka
- Spring Cloud Zookeeper
- nacos
- Hystrix熔斷原理
- Hystrix應用
- Spring Cloud Config
- 服務網關
- Zuul
- Spring Cloud Gateway
- 服務調用及負載
- Ribbon
- Feign&OpenFeign
- Turbine
- actuator
- springboot & springcloud
- springcloud vs dubbo
- 常見面試題
- BeanFactory和FactoryBean
- @Autowired/@Resource和@Inject的區別
- Singleton bean注入prototype bean
- 附錄