作用:替代以前的applicationContext.xml文件,完成spring容器的初始化。
转入:https://www.cnblogs.com/dream-flying/articles/12933519.html
例子1:@Configuration+@ComponentScan
作用:功能类似于在applicationContext.xml文件中配置组件扫描器。在定义各级bean时,使用@Controoler,@Service,@Component等注释,就可以自动完成spring容器对Bean的装载。
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /* 配置器 */ @Configuration @ComponentScan("com.yrc.test4") public class MyConfig { }
例子2::@Configuration+@Bean
作用::功能类似于在applicationContext.xml文件手动注册Bean。此时在各级Bean中需要添加setter方法,
@Configuration public class MyConfig { @Bean public FunctionService functionService() { return new FunctionService(); } @Bean public UseFunctionService useFunctionService(FunctionService functionService) { UseFunctionService useFunctionService = new UseFunctionService(); useFunctionService.setFunctionService(functionService); return useFunctionService; } }
例子3:@Configuration+@ComponentScan+@EnableAspectJAutoProxy
作用:实现AOP配置,@EnableAspectJAutoProxy开启自动代理
@Configuration @ComponentScan("com.yrc.test6") @EnableAspectJAutoProxy public class MyConfig { }