配置注解:
@Component、@Configuration、@Bean、@Import、@Resource、@Qualifier、@Mapper、@Service、@autowired、@Value、@bean、@entity、@Table、@Transient
参数注解:
@param、@requestparam、@requestbody
代码:
Car
package com.wange.anno; public interface Car { public void print(); }
Bmw
package com.wange.anno; import org.springframework.stereotype.Component; @Component public class Bmw implements Car{ @Override public void print() { System.out.println("我是BMW"); } }
RangeRover
package com.wange.anno; import org.springframework.stereotype.Component; @Component public class RangeRover implements Car{ @Override public void print() { System.out.println("我是range rover"); } }
ConfigurationA
package com.wange.anno; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ConfigurationA { @Bean("rangerover") public Car getRangerover(){ return new RangeRover(); } }
ConfigurationB
package com.wange.anno; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ConfigurationB { @Bean("bmw") public Car getBmw(){ return new Bmw(); } }
Config
package com.wange.anno; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import({ConfigurationA.class, ConfigurationB.class}) public class Config { }
Test
package com.wange.anno; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class ContextLoaderTest { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); Car bmw = (Car)context.getBean("bmw"); bmw.print(); RangeRover rangeRover = (RangeRover)context.getBean("rangerover"); rangeRover.print(); } }
结果:
我是BMW
我是range rover