springboot的@Configuration


作用:替代以前的applicationContext.xml文件,完成spring容器的初始化。

 

 

例子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 {
}

 

 

细节

参考博客:https://blog.csdn.net/BinshaoNo_1/article/details/85005935

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM