配置注解:
@Component、@Configuration、@Bean、@Import、@Resource、@Qualifier、@Mapper、@Service、@autowired、@Value、@bean、@entity、@Table、@Transient
参数注解:
@param、@requestparam、@requestbody
代码:
Car
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.wange.anno; public interface Car { public void print(); }
Bmw
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.wange.anno; import org.springframework.stereotype.Component; @Component public class Bmw implements Car{ @Override public void print() { System.out.println("我是BMW"); } }
RangeRover
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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