Spring 全注解配置 bean 和 调用 (4) @Primary 自动装配歧义性

来源:互联网 发布:js trim函数使用 编辑:程序博客网 时间:2024/04/29 20:13
package com.xiuye.bean;import org.springframework.beans.factory.annotation.Autowired;import com.xiuye.component.CarComponent;public class Car {private CarComponent wheels;private CarComponent steer;private CarComponent engine;@Autowiredpublic void setWheels(CarComponent wheels) {this.wheels = wheels;}@Autowiredpublic void setSteer(CarComponent steer) {this.steer = steer;}@Autowiredpublic void setEngine(CarComponent engine) {this.engine = engine;}public void configInfo(){this.steer.description();this.wheels.description();this.engine.description();}}

package com.xiuye.component;public interface CarComponent {public void description();}

package com.xiuye.component;import org.springframework.stereotype.Component;@Componentpublic class CarSteerComponent implements CarComponent {@Overridepublic void description() {System.out.println("I'm car's steer!");}}

package com.xiuye.component;import org.springframework.context.annotation.Primary;import org.springframework.stereotype.Component;@Component@Primarypublic class CarEngineComponent implements CarComponent {@Overridepublic void description() {System.out.println("I'm car's engine!");}}

package com.xiuye.component;import org.springframework.stereotype.Component;@Componentpublic class CarWheelComponent implements CarComponent {@Overridepublic 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.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 effectpublic Student student(String name, String sex, int age, int level) {return new Student(name, sex, age, level);}@Beanpublic ComponentForStudent2 cfs2(Student s) {ComponentForStudent2 cfs2 = new ComponentForStudent2();cfs2.setStudent(s);return cfs2;}@Beanpublic Car car(){return new Car();}}

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.component.ComponentForStudent;import com.xiuye.component.ComponentForStudent2;import com.xiuye.config.BeanConfiguration1;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes=BeanConfiguration1.class)@ActiveProfiles("dev")public class TestMain {@Resourceprivate ComponentForStudent cfs;@Resourceprivate ComponentForStudent2 cfs2;@Resourceprivate Car car;@BeforeClasspublic static void envConfig(){System.setProperty("test","true");}@Testpublic void testCfs(){cfs.studentInfo();}@Testpublic void testCfs2(){cfs2.studentInfo();}@Testpublic void testCar(){this.car.configInfo();}}

十一月 12, 2016 1:00:26 下午 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:00:26 下午 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:00:26 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@133c8f8, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1a25848, org.springframework.test.context.support.DirtiesContextTestExecutionListener@c3fd8b, org.springframework.test.context.transaction.TransactionalTestExecutionListener@194a1b5, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@5fc7f7]十一月 12, 2016 1:00:26 下午 org.springframework.context.support.GenericApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.GenericApplicationContext@25790e: startup date [Sat Nov 12 13:00:26 CST 2016]; root of context hierarchyActive profile := devtest := trueI'm car's engine!I'm car's engine!I'm car's engine!十一月 12, 2016 1:00:26 下午 org.springframework.context.support.GenericApplicationContext doClose信息: Closing org.springframework.context.support.GenericApplicationContext@25790e: startup date [Sat Nov 12 13:00:26 CST 2016]; root of context hierarchy
注:三个注入的CarComponent都是CarEngineComponent
0 0