Spring 全注解配置 bean 和 调用 (6) 自定义注解 自动装配歧义性消除

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

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.annotation.Qualifier;

@Target({ElementType.CONSTRUCTOR,ElementType.FIELD,ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Engine {

}

package com.xiuye.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.annotation.Qualifier;

@Target({ElementType.CONSTRUCTOR,ElementType.FIELD,ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Steer {

}

package com.xiuye.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.annotation.Qualifier;

@Target({ElementType.CONSTRUCTOR,ElementType.FIELD,ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Wheel {

}

package com.xiuye.component;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import com.xiuye.annotation.Engine;

@Component
@Primary
@Engine
public class CarEngineComponent implements CarComponent {

	@Override
	public void description() {

		System.out.println("I'm car's engine!");

	}

}

package com.xiuye.component;

import org.springframework.stereotype.Component;

import com.xiuye.annotation.Steer;

@Component
@Steer
public class CarSteerComponent implements CarComponent {

	@Override
	public void description() {
		System.out.println("I'm car's steer!");
	}

}

package com.xiuye.component;

import org.springframework.stereotype.Component;

import com.xiuye.annotation.Wheel;

@Component
@Wheel
public class CarWheelComponent implements CarComponent {

	@Override
	public void description() {
		System.out.println("I'm car's wheels!");
	}

}

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.Car;
import com.xiuye.bean.Student;
import com.xiuye.bean.WhiteCar;
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;
	}

	@Bean
	public Car car(){
		return new Car();
	}
	@Bean
	public WhiteCar wCar(){
		return new WhiteCar();
	}

}

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.bean.Car;
import com.xiuye.bean.WhiteCar;
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;

	@Resource
	private Car car;

	@Resource
	private WhiteCar wcar;

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

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


	@Test
	public void testCar(){
		this.car.configInfo();
	}
	@Test
	public void testWhiteCar(){
		this.wcar.configInfo();
	}

}

十一月 12, 2016 1:18:15 下午 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 1:18:15 下午 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 1:18:15 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1a25848, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@c3fd8b, org.springframework.test.context.support.DirtiesContextTestExecutionListener@194a1b5, org.springframework.test.context.transaction.TransactionalTestExecutionListener@5fc7f7, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@16eb2ec]
十一月 12, 2016 1:18:15 下午 org.springframework.context.support.GenericApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@a097cc: startup date [Sat Nov 12 13:18:15 CST 2016]; root of context hierarchy
Active profile := dev
test := true
I'm car's steer!
I'm car's wheels!
I'm car's engine!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值