Spring 全注解配置 bean 和 调用 (3) @Conditonal 条件化的配置bean

94 篇文章 0 订阅
69 篇文章 0 订阅
package com.xiuye.config;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.Scope;

import com.xiuye.bean.Student;
import com.xiuye.component.ComponentForStudent2;
import com.xiuye.config.condition.StudentCondition;

@Configuration
@ComponentScan("com.xiuye.component")
@Profile("dev")
public class BeanConfiguration1 {

	@Bean
	@Conditional(StudentCondition.class)
	public Student student() {
		return new Student("xiuye", "man", 18, 99);
	}

	/**
	 * @param name
	 * @param sex
	 * @param age
	 * @param level
	 * @return
	 */
	@Bean
	@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) // 必须原生,否则no longer has
													// any effect
	public Student student(String name, String sex, int age, int level) {
		return new Student(name, sex, age, level);
	}

	@Bean
	public ComponentForStudent2 cfs2(Student s) {
		ComponentForStudent2 cfs2 = new ComponentForStudent2();
		cfs2.setStudent(s);
		return cfs2;
	}

}

package com.xiuye.config.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class StudentCondition implements Condition{

	@Override
	public boolean matches(ConditionContext paramConditionContext, AnnotatedTypeMetadata paramAnnotatedTypeMetadata) {
		Environment env = paramConditionContext.getEnvironment();
		for(String a : env.getActiveProfiles()){
			System.out.println("Active profile := "+a);
		}
		boolean b = Boolean.valueOf(env.getProperty("test"));
		System.out.println("test := " + b);
		return b;
	}

}

package com.xiuye.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.xiuye.bean.Student;
import com.xiuye.component.Component;
import com.xiuye.component.ComponentForStudent;
import com.xiuye.config.BeanConfiguration1;

public class Main {

	public static void main(String []args) {

		System.setProperty("test","false");//@Conditional注解对AnnotationConfigApplicationContext不起作用
		System.setProperty("spring.profiles.active","dev");//设置(开启)激活的开发环境   可以   不设置报错
		//System.setProperty("spring.profiles.default","dev");//设置默认的开发环境  可以

		ApplicationContext ac = new AnnotationConfigApplicationContext(BeanConfiguration1.class);
		Student s = ac.getBean(Student.class);
		System.out.println(s);
		s = ac.getBean(Student.class, "Linda","Woman",22,70);
		System.out.println(s);
		ComponentForStudent cf = ac.getBean(ComponentForStudent.class);
		cf.studentInfo();
		Component c = ac.getBean(Component.class, "100","200");
		System.out.println(c);
	}

}
十一月 12, 2016 12:30:55 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1af6ecc: startup date [Sat Nov 12 12:30:55 CST 2016]; root of context hierarchy
Active profile := dev
test := false
Student [name=xiuye, sex=man, age=18, level=99]
Student [name=Linda, sex=Woman, age=22, level=70]
Student [name=xiuye, sex=man, age=18, level=99]
Component [_1=100, _2=200]


package com.xiuye.test;

import javax.annotation.Resource;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.xiuye.component.ComponentForStudent;
import com.xiuye.component.ComponentForStudent2;
import com.xiuye.config.BeanConfiguration1;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=BeanConfiguration1.class)
@ActiveProfiles("dev")

public class TestMain {


	@Resource
	private ComponentForStudent cfs;
	@Resource
	private ComponentForStudent2 cfs2;

	@BeforeClass
	public static void envConfig(){
		System.setProperty("test","true");
	}

	@Test
	public void testCfs(){
		cfs.studentInfo();
	}
	@Test
	public void testCfs2(){
		cfs2.studentInfo();
	}


}
十一月 12, 2016 12:31:13 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
十一月 12, 2016 12:31:13 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners
信息: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
十一月 12, 2016 12:31:13 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@203ea1, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@7f674d, org.springframework.test.context.support.DirtiesContextTestExecutionListener@1251891, org.springframework.test.context.transaction.TransactionalTestExecutionListener@140564, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@18ceff1]
十一月 12, 2016 12:31:13 下午 org.springframework.context.support.GenericApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@137cf78: startup date [Sat Nov 12 12:31:13 CST 2016]; root of context hierarchy
Active profile := dev
test := true
Student [name=xiuye, sex=man, age=18, level=99]
Student [name=xiuye, sex=man, age=18, level=99]
十一月 12, 2016 12:31:13 下午 org.springframework.context.support.GenericApplicationContext doClose
信息: Closing org.springframework.context.support.GenericApplicationContext@137cf78: startup date [Sat Nov 12 12:31:13 CST 2016]; root of context hierarchy


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值