Heim > Java > javaLernprogramm > Hauptteil

Detaillierte Erläuterung der @Profile-Annotation

(*-*)浩
Freigeben: 2019-08-12 16:15:11
nach vorne
3399 Leute haben es durchsucht

@Profile: Spring bietet uns die Funktion, eine Reihe von Komponenten entsprechend der aktuellen Umgebung dynamisch zu aktivieren und zu wechseln.

Detaillierte Erläuterung der @Profile-Annotation

Entwicklungsumgebung entwickeln, Testumgebungstest, Produktionsumgebungsmaster

Datenquelle: (/dev) (/test) (/master)

@Profil: Geben Sie die Umgebung an, in der die Komponente registriert werden kann Der Container, wenn nicht angegeben, kann diese Komponente in jeder Umgebung registriert werden

1) Beans mit Umgebungsidentifikation können nur im Container registriert werden, wenn die Umgebung aktiviert ist. Der Standardwert ist die Standardumgebung

2) In der Konfigurationsklasse geschrieben, nur wenn es sich um die angegebene Umgebung handelt, können alle Konfigurationen in der gesamten Konfigurationsklasse wirksam werden

package com.spring.config;
 
import java.beans.PropertyVetoException;
 
import javax.sql.DataSource;
 
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 com.mchange.v2.c3p0.ComboPooledDataSource;
 
/**
 * Profile:
 * 		Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;
 * 
 * 开发环境develop、测试环境test、生产环境master
 * 数据源:(/dev) (/test) (/master)
 *
 * @Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件
 * 
 * 1) 加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境
 * 2) 写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效
 * 
 */
@PropertySource("classpath:/dbconfig.properties")
@Configuration
public class MainConfigOfProfile implements EmbeddedValueResolverAware{
	
	@Value("${db.user}")
	private String user;
	
	private String driverClass;
	
	@Profile("default")
	@Bean("test")
	public DataSource testDataSource(@Value("${db.password}")String password) throws PropertyVetoException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setUser(user);
		dataSource.setPassword(password);
		dataSource.setDriverClass(driverClass);
		return dataSource;
	}
	
	@Profile("dev")
	@Bean("dev")
	public DataSource devDataSource(@Value("${db.password}")String password) throws PropertyVetoException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setUser(user);
		dataSource.setPassword(password);
		dataSource.setDriverClass(driverClass);
		return dataSource;
	}
	
	@Profile("master")
	@Bean("master")
	public DataSource masterDataSource(@Value("${db.password}")String password) throws PropertyVetoException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setUser(user);
		dataSource.setPassword(password);
		dataSource.setDriverClass(driverClass);
		return dataSource;
	}
 
	public void setEmbeddedValueResolver(StringValueResolver resolver) {
		String driverClass = resolver.resolveStringValue("${db.driverClass}");
		this.driverClass = driverClass;
	}
 
}
Nach dem Login kopieren
package com.spring.test;
 
import java.util.Arrays;
 
import javax.sql.DataSource;
 
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
import com.spring.config.MainConfigOfProfile;
 
 
public class IOCTestProfile {
	//1. 使用命令行动态参数:在虚拟机参数位置加载 -Dspring.profiles.active=test
	//2. 使用代码的方式激活某种环境;
	@Test
	public void test01() {
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);
		//1. 创建一个applicationContext
		//2. 设置需要激活的环境
		applicationContext.getEnvironment().setActiveProfiles("dev","master");
		//3. 注册主配置类
		applicationContext.register(MainConfigOfProfile.class);
		//4. 启动刷新容器
		applicationContext.refresh();
		
		String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
		System.out.println(Arrays.toString(beanNamesForType));
		
		applicationContext.close();
	}
 
 
        @Test
	public void test02() {
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);
		
		String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
		System.out.println(Arrays.toString(beanNamesForType));
		
		applicationContext.close();
	}
}
Nach dem Login kopieren

Das obige ist der detaillierte Inhalt vonDetaillierte Erläuterung der @Profile-Annotation. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:csdn.net
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!