package com.example.demo_3_5;
/**
* Created by WangBin on 2017/4/21.
*
*/
@WiselyConfiguration("com.example.demo_3_5")//使用WiselyConfiguration 代替Configuration ComponentScan
public class DemoConfig {
}
package com.example.demo_3_5;
import org.springframework.stereotype.Service;
/**
* Created by WangBin on 2017/4/21.
*
*/
@Service
public class DemoService {
public void outputResult(){
System.err.println("从组合注解配置照样活得的bean");
}
}
package com.example.demo_3_5;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by WangBin on 2017/4/21.
*
*/
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DemoConfig.class);
DemoService demoService = context.getBean(DemoService.class);
demoService.outputResult();
context.close();
}
}
package com.example.demo_3_5;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.lang.annotation.*;
/**
* Created by WangBin on 2017/4/21.
*
*/
/* @Target:注解的作用目标
@Target(ElementType.TYPE) //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)//表示jvm运行时,此注解可被读出
@Documented//d注释的作用及其javadoc文档生成工具的使用代码放在MyDocumentedtAnnotationDemo.java文件中
@Configuration// 组合Configuration元注解
@ComponentScan// 组合ComponentScan元注解
public @interface WiselyConfiguration {
String[] value() default {};//覆盖value参数
}
package com.example.demo_3_7;
import org.junit.Assert;
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.SpringJUnit4ClassRunner;
/**
* Created by WangBin on 2017/4/24.
*
*/
@RunWith(SpringJUnit4ClassRunner.class)// SpringJUnit4ClassRunner 在 Junit 环境下 提供Spring TestCOntext Framework功能
@ContextConfiguration(classes = {TestConfig.class})//ContextConfiguration用来加载配置类
@ActiveProfiles("pro")//ActiveProfiles 用来声明活动的profile
public class DemoBeanIntegrationTest {
@Autowired//可使用普通的Autowired注入Bean
TestBean testBean;
@Test//测试代码通过junit 的 Assert 来校验结果是否和预期的一致
public void proBeanShouldInject(){
String expected = "pro";
String actual = testBean.getContent();
Assert.assertEquals(expected,actual);//断言
}
}
package com.example.demo_3_7;
/**
* Created by WangBin on 2017/4/24.
*
*/
public class TestBean {
private String content;
public TestBean(String content) {
this.content = content;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
package com.example.demo_3_7;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
/**
* Created by WangBin on 2017/4/24.
*
*/
@Configuration
public class TestConfig {
@Bean
@Profile("dev")
public TestBean devTestBean(){
return new TestBean("dev");
}
@Bean
@Profile("pro")
public TestBean proTestBean(){
return new TestBean("pro");
}
}