Spring(2)_多环境配置@Profile

Spring(2)_多环境配置@Profile

	@Profile()//用于标记该Bean在什么环境下能够被激活
	ioc容器配置启动环境
		_1.控制台启动命令 -Dspring.profiles.active=pros   //pros需要激活的环境名称
		_2.类配置环境:
			__1.获取上下文环境ApplicationContext
			__2.配置上下文需要激活的环境  getEnvironment().setActiveProfiles("环境名称");
			__3.上下文环境注册配置类         register("配置类");
			__4.启动上下文环境                    refresh();
	 

配置类 MainConfigProfile

package springSource.valueSet.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import springSource.valueSet.bean.dev;
import springSource.valueSet.bean.pro;
import springSource.valueSet.bean.test;

@Configuration
public class mainConfigProfile {

	
	@Bean
	@Profile("tests")
	public test test() {
		return new test();
	}
	
	@Bean
	@Profile("pros")
	public pro pro() {
		return new pro();
	}
	@Bean
	@Profile("devs")
	public dev dev() {
		return new dev();
	}
	
}

测试类

package springSource;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import springSource.valueSet.bean.pro;
import springSource.valueSet.config.mainConfigProfile;

public class profileTest {

	
	@Test
	public void test01() {
		AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(mainConfigProfile.class);
		String[] beans = annotationConfigApplicationContext.getBeanDefinitionNames();
		for (String bean : beans) {
			System.out.println(bean);
		}
	}
	@Test
	public void test02() {
//		//获取上下文实例
		AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
		//配置上下文需要激活的环境配置
		annotationConfigApplicationContext.getEnvironment().setActiveProfiles("pros");
		//上下文注册配置类
		annotationConfigApplicationContext.register(mainConfigProfile.class);
		//启动上下文环境
		annotationConfigApplicationContext.refresh();
		//获取上下文环境ioc容器扫描到的组件名
		String[] beans = annotationConfigApplicationContext.getBeanDefinitionNames();
		//遍历组件名
		for (String bean : beans) {
			System.out.println(bean);
		}
		//获取spring字符串解析器
		pro proIns = annotationConfigApplicationContext.getBean(pro.class);
		//调用字符串解析器
		System.out.println(proIns.resolveValue("#{1+2}"));
	}
}

 运行结果

七月 14, 2020 12:44:40 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7c53a9eb: startup date [Tue Jul 14 00:44:40 CST 2020]; root of context hierarchy
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfigProfile
pro
3

 

 

 

你可能感兴趣的:(Spring,spring,bean,ioc,java)