首页 > 编程语言 > 详细

springboot的@Configuration

时间:2021-01-05 18:51:58      阅读:21      评论:0      收藏:0      [点我收藏+]

作用:替代以前的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 {
}

springboot的@Configuration

原文:https://www.cnblogs.com/james641/p/14236811.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!