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