【Spring】 (11)组合注解和元注解

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");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值