spring基礎系列--JavaConfig配置


原創作品,可以轉載,但是請標注出處地址:http://www.cnblogs.com/V1haoge/p/7171011.html

  早以前,Spring推薦使用XML的方式來定義Bean及Bean之間的裝配規則,但是在Spring3之后,Spring提出的強大的JavaConfig這種類型安全的Bean裝配方式,它基於Java代碼的靈活性,使得裝配的過程也變得及其靈活。

  使用JavaConfig來裝配Bean擁有其自己的一套規則,我們在這里來看一看:

1、規則

規則一:@Configuration注解

  我們在定義JavaConfig類時,都會在其上加注@Configuration注解,來表明這是一個配置類,@Configuration注解底層是@Component注解,而且這個注解會被AnnotationConfigApplicationContext來進行加載,AnnotationConfigApplicationContext是ApplicationContext的一個具體實現,代表依據配置注解啟動應用上下文。

規則二:@ComponentScan注解

  我們使用JavaConfig的目的是為了實現以前XML配置實現的功能,首先就是組件掃描功能,將我們使用特定注解標注的類統一掃描加載到Spring容器,這一功能就是依靠@ComponentScan注解來實現的,我們可以為其指定位置參數來指定要掃描的包。

規則三:@Bean注解

  使用@Bean注解我們可以實現XML配置中手動配置第三方Bean的功能,這里我們使用方法來定義Bean,並在方法前面加注@Bean注解,表示要將該方法返回的對象加載到Spring容器中,這樣就對我們的方法定義帶來了一些限制,這些限制包括方法的大概格式:

    1-方法帶返回值,且返回類型為你要加載的第三方類類型

    2-方法的名稱為默認的Bean的name,如果要自定義Bean的name,可以使用@Bean注解的name屬性。

    3-要實現注入只需要將要注入的Bean的類型作為參數,調用該類的帶參數的構造器構建這個Bean,或者采用第二種方式:先創建這個類的對象,然后調用該對象的set方法進行注入,以被注入的Bean的方法為參數

規則驗證:

首先我們創建幾個測試類

針對第一種注入方式:

1-StudentService

1 import org.springframework.stereotype.Service;
2 
3 @Service
4 public class StudentService {
5     public void study(){
6         System.out.println("學生學習Java");
7     }
8 }

2-TeacherService 

 1 import org.springframework.stereotype.Service;
 2 
 3 @Service
 4 public class TeacherService {
 5     
 6     private StudentService studentService;
 7     
 8     public TeacherService(StudentService studentService){
 9         this.studentService=studentService;
10     }
11     
12     public void teach(){
13         studentService.study();
14     }
15 }

3-Config:這是針對第一種注入方式而設,需要在TeacherService 中定義帶參數的構造器

 1 import org.springframework.context.annotation.Bean;
 2 //import org.springframework.context.annotation.ComponentScan;
 3 import org.springframework.context.annotation.Configuration;
 4 
 5 @Configuration
 6 //@ComponentScan
 7 public class Config {
 8     
 9     @Bean(name="student")
10     public StudentService studentService(){
11         return new StudentService();
12     }
13     
14     @Bean(name="teacher")
15     public TeacherService teacherService(StudentService studentService){
16         return new TeacherService(studentService);
17     }
18     
19 }

針對第二種注入方式:

1-StudentService

1 public class StudentService {
2     
3     public void study(){
4         System.out.println("學生在學習Java");
5     }
6     
7 }

2-TeacherService

 1 public class TeacherService {
 2         
 3     private StudentService studentService;
 4 
 5     public StudentService getStudentService() {
 6         return studentService;
 7     }
 8 
 9     public void setStudentService(StudentService studentService) {
10         this.studentService = studentService;
11     }
12     
13     public void teach(){
14         studentService.study();
15     }
16     
17 }

3-Config:這是采用第二種注入方式:需要在TeacherService中提供set方法

 1 import org.springframework.context.annotation.Bean;
 2 import org.springframework.context.annotation.Configuration;
 3 
 4 @Configuration
 5 public class Config {
 6     
 7     @Bean
 8     public StudentService student(){
 9         return new StudentService();
10     }
11     
12     @Bean
13     public TeacherService teacher(){
14         TeacherService teacherService = new TeacherService();
15         teacherService.setStudentService(student());
16         return teacherService;
17     }
18     
19 }

4-測試類:TestMain

 1 import java.util.Iterator;
 2 
 3 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 4 
 5 public class TestMain {
 6 
 7     public static void main(String[] args) {
 8         AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(Config.class);
 9         TeacherService teacher = acac.getBean(TeacherService.class);
10         teacher.teach();
11         Iterator<String> i = acac.getBeanFactory().getBeanNamesIterator();
12         while(i.hasNext()){
13             System.out.println(i.next());
14         }
15         acac.close();
16     }
17 
18 }

執行結果:

七月 14, 2017 4:10:56 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Fri Jul 14 16:10:56 CST 2017]; root of context hierarchy
學生學習Java
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
config
student
teacher
environment
systemProperties
systemEnvironment
org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry
messageSource
applicationEventMulticaster
lifecycleProcessor
七月 14, 2017 4:10:59 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Fri Jul 14 16:10:56 CST 2017]; root of context hierarchy

  該測試結果中打印出的是Spring上下文中所有加載的Bean的名稱(name)。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM