javascript
Spring系列(十一):@Profile 注解用法介绍
今天給大家分享Spring屬性注入的注解@Profile 介紹,希望對大家能有所幫助!
? ? ? ? ? ? ? ?
一、@Profile 注解的作用
在Spring容器中如果存在同一類型的多個組件,可以使用@Profile注解標(biāo)識實(shí)際要獲取的是哪一個bean,這在不同的環(huán)境使用不同的變量的場景下非常有用。
最典型的例子:開發(fā)環(huán)境、測試環(huán)境、生產(chǎn)環(huán)境會配置不同的數(shù)據(jù)源,在盡量不修改代碼的情況下,可以使用這個注解來動態(tài)指定要連接的數(shù)據(jù)源。
二、@Profile 指定環(huán)境的方式
2.1 JVM啟動參數(shù)
運(yùn)行的時候給虛擬機(jī)參數(shù)位置增加 -Dspring.profiles.active=dev
2.2 通過代碼方式控制:
首先創(chuàng)建一個AnnotationConfigApplicationContext
設(shè)置環(huán)境變量,指定要激活的環(huán)境
注冊配置類
啟動的時候刷新容器
三、@Profile 實(shí)現(xiàn)切換數(shù)據(jù)源示例
3.1 導(dǎo)入依賴
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.44</version> </dependency>3.2 新建數(shù)據(jù)源配置文件dataSource.properties
dataSource.user=root dataSource.password=123 dataDriveClassName=com.mysql.jdbc.Drive3.3 新建TestProfileConfig.java 配置類
package com.spring.config;import com.mchange.v2.c3p0.ComboPooledDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.EmbeddedValueResolverAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.PropertySource; import org.springframework.util.StringValueResolver;import javax.sql.DataSource; import java.beans.PropertyVetoException;// 加載配置文件 @PropertySource("classpath:/dataSource.properties") @Configuration public class TestProfileConfig implements EmbeddedValueResolverAware {// 通過@Value注解獲取配置文件dataSource.user的值@Value("${dataSource.user}")private String user;private StringValueResolver resolver;private String dirveClassName;/*** 開發(fā)環(huán)境**/@Profile("dev")@Beanpublic DataSource dataSourceDev(@Value("${dataSource.password}") String pwd) throws PropertyVetoException {ComboPooledDataSource dataSource = new ComboPooledDataSource();dataSource.setUser(user);dataSource.setPassword(pwd);dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/dev_db");dataSource.setDriverClass(dirveClassName);return dataSource;}/*** 測試環(huán)境**/@Profile("test")@Beanpublic DataSource dataSourceTest(@Value("${dataSource.password}") String pwd) throws PropertyVetoException{ComboPooledDataSource dataSource = new ComboPooledDataSource();dataSource.setUser(user);dataSource.setPassword(pwd);dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test_db");dataSource.setDriverClass(dirveClassName);return dataSource;}/*** 生產(chǎn)環(huán)境**/@Profile("onLine")@Beanpublic DataSource dataSourceOnLine(@Value("${dataSource.password}") String pwd) throws PropertyVetoException{ComboPooledDataSource dataSource = new ComboPooledDataSource();dataSource.setUser(user);dataSource.setPassword(pwd);dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/online_db");dataSource.setDriverClass(dirveClassName);return dataSource;}/*** 通過StringValueResolver解析dataDriveClassName的值**/public void setEmbeddedValueResolver(StringValueResolver resolver) {dirveClassName=resolver.resolveStringValue("${dataSource.dataDriveClassName}");} }3.4 新建測試類TestProfile.java
package com.spring.test;import com.spring.config.TestProfileConfig; import org.springframework.context.annotation.AnnotationConfigApplicationContext;import javax.sql.DataSource;public class TestProfile {public static void main(String[] args) {/* 命令行動態(tài)參數(shù):運(yùn)行的時候給虛擬機(jī)參數(shù)位置增加 -Dspring.profiles.active=dev通過代碼方式控制:1首先創(chuàng)建一個AnnotationConfigApplicationContext2 設(shè)置環(huán)境變量,指定要激活的環(huán)境3 注冊配置類4啟動的時候刷新容器*/// 01 首先創(chuàng)建一個AnnotationConfigApplicationContextAnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();//02 設(shè)置環(huán)境變量,指定要激活的環(huán)境 可以指定一個或者多個context.getEnvironment().setActiveProfiles("dev","onLine");//03 注冊配置類context.register(TestProfileConfig.class);//04 啟動刷新容器context.refresh();String[] names = context.getBeanNamesForType(DataSource.class);for (String name : names) {System.out.println(name);}} }輸出結(jié)果:
dataSourceDev
dataSourceOnLine
四、使用總結(jié)
1、針對標(biāo)注了環(huán)境標(biāo)識的bean,只有在這個環(huán)境被激活的時候,才會注入到容器當(dāng)中。默認(rèn)是default環(huán)境。
2、如果@Profile 注解的位置在類上,相當(dāng)于只有在指定該環(huán)境的情況下,整個配置類里面的配置才有機(jī)會生效。
3、針對沒有標(biāo)注環(huán)境表示的bean,在任何環(huán)境下都可以被正常加載。
IT技術(shù)分享社區(qū)
個人博客網(wǎng)站:https://programmerblog.xyz
文章推薦程序員效率:畫流程圖常用的工具程序員效率:整理常用的在線筆記軟件遠(yuǎn)程辦公:常用的遠(yuǎn)程協(xié)助軟件,你都知道嗎?51單片機(jī)程序下載、ISP及串口基礎(chǔ)知識硬件:斷路器、接觸器、繼電器基礎(chǔ)知識
總結(jié)
以上是生活随笔為你收集整理的Spring系列(十一):@Profile 注解用法介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue-cli部署ngixs_Vue-c
- 下一篇: Spring系列(十三):AOP相关知识