一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - spring基礎系列之JavaConfig配置詳解

spring基礎系列之JavaConfig配置詳解

2020-12-02 14:24唯一浩哥 Java教程

本篇文章主要介紹了spring基礎系列之JavaConfig配置詳解,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

早以前,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
2
3
4
5
6
7
8
import org.springframework.stereotype.Service;
 
@Service
public class StudentService {
  public void study(){
    System.out.println("學生學習Java");
  }
}

2-TeacherService

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

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

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

針對第二種注入方式:

1-StudentService

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

2-TeacherService

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

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

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

4-測試類:TestMain

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

執行結果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
七月 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)。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/V1haoge/p/7171011.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产老太婆hd老头 | 成年性香蕉漫画在线观看 | 亚洲国产精品第一页 | ai换脸杨颖被啪在线观看 | 日韩在线一区二区 | 青青色综合| 被调教的校花 | 日本一区二区视频免费播放 | 国产成人福利色视频 | 农村妇女野外性生话免费视频 | 精品国产影院 | 精品久久香蕉国产线看观看亚洲 | 国产真实偷乱视频在线观看 | 日本亚洲欧洲高清有码在线播放 | 1024免费观看完整版在线播放 | avtt一区| 岛国a香蕉片不卡在线观看 荡女淫春2古装 | 外国xxx| 亚洲国产区男人本色在线观看欧美 | 四虎1515hhcom | 四虎成人永久地址 | 白丝尤物的下面被疯狂蹂躏 | 色婷婷婷丁香亚洲综合不卡 | 99re7在线精品免费视频 | 欧美综合亚洲图片综合区 | 精品videoss另类日本 | 国产亚洲精品久久yy5099 | 特级淫片大乳女子高清视频 | 欧美日韩在线一区 | 日本另类z0zx高清 | 女人张开腿 让男人桶个爽 免费观看 | 全彩调教侵犯h本子全彩妖气he | 久久久精品3d动漫一区二区三区 | 国产一区二区三区久久精品小说 | 国产精品性视频免费播放 | 女女性恋爱视频入口 | 嗯啊好大好粗 | 成人免费视频在 | 日本在线观看www免费 | 青青国产在线视频 | 免费的毛片视频 |