国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - spring基礎(chǔ)系列之JavaConfig配置詳解

spring基礎(chǔ)系列之JavaConfig配置詳解

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

本篇文章主要介紹了spring基礎(chǔ)系列之JavaConfig配置詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

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

使用JavaConfig來(lái)裝配Bean擁有其自己的一套規(guī)則,我們?cè)谶@里來(lái)看一看:

1、規(guī)則

規(guī)則一:@Configuration注解

我們?cè)诙xJavaConfig類(lèi)時(shí),都會(huì)在其上加注@Configuration注解,來(lái)表明這是一個(gè)配置類(lèi),@Configuration注解底層是@Component注解,而且這個(gè)注解會(huì)被AnnotationConfigApplicationContext來(lái)進(jìn)行加載,AnnotationConfigApplicationContext是ApplicationContext的一個(gè)具體實(shí)現(xiàn),代表依據(jù)配置注解啟動(dòng)應(yīng)用上下文。

規(guī)則二:@ComponentScan注解

我們使用JavaConfig的目的是為了實(shí)現(xiàn)以前XML配置實(shí)現(xiàn)的功能,首先就是組件掃描功能,將我們使用特定注解標(biāo)注的類(lèi)統(tǒng)一掃描加載到Spring容器,這一功能就是依靠@ComponentScan注解來(lái)實(shí)現(xiàn)的,我們可以為其指定位置參數(shù)來(lái)指定要掃描的包。

規(guī)則三:@Bean注解

使用@Bean注解我們可以實(shí)現(xiàn)XML配置中手動(dòng)配置第三方Bean的功能,這里我們使用方法來(lái)定義Bean,并在方法前面加注@Bean注解,表示要將該方法返回的對(duì)象加載到Spring容器中,這樣就對(duì)我們的方法定義帶來(lái)了一些限制,這些限制包括方法的大概格式:

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

2-方法的名稱(chēng)為默認(rèn)的Bean的name,如果要自定義Bean的name,可以使用@Bean注解的name屬性。

3-要實(shí)現(xiàn)注入只需要將要注入的Bean的類(lèi)型作為參數(shù),調(diào)用該類(lèi)的帶參數(shù)的構(gòu)造器構(gòu)建這個(gè)Bean,或者采用第二種方式:先創(chuàng)建這個(gè)類(lèi)的對(duì)象,然后調(diào)用該對(duì)象的set方法進(jìn)行注入,以被注入的Bean的方法為參數(shù)

規(guī)則驗(yàn)證:

首先我們創(chuàng)建幾個(gè)測(cè)試類(lèi)

針對(duì)第一種注入方式:

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("學(xué)生學(xué)習(xí)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:這是針對(duì)第一種注入方式而設(shè),需要在TeacherService 中定義帶參數(shù)的構(gòu)造器

?
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);
  }
  
}

針對(duì)第二種注入方式:

1-StudentService

?
1
2
3
4
5
6
7
public class StudentService {
  
  public void study(){
    System.out.println("學(xué)生在學(xué)習(xí)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-測(cè)試類(lèi):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();
  }
 
}

執(zhí)行結(jié)果:

?
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
學(xué)生學(xué)習(xí)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

該測(cè)試結(jié)果中打印出的是Spring上下文中所有加載的Bean的名稱(chēng)(name)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 亚洲精品久久久久中文字幕欢迎你 | 亚洲电影在线观看 | 日韩一区二区三区四区 | 精品久久久久久久 | 久久青草国产 | 国产精品入口在线观看 | 色狠狠一区 | 91精品国产综合久久久久久丝袜 | 少妇看av一二三区 | 91精品国产欧美一区二区 | 国产电影一区二区三区 | 欧美日韩一区二区电影 | 欧美精品一区二区三区在线 | 99久久精品免费看国产一区二区三区 | 色综合天天综合网国产成人综合天 | 久久伊人精品 | 色一色视频 | 在线精品国产 | 亚洲激情中文字幕 | t66y最新地址一地址二69 | 国产精品久久久久无码av | 久久久久久久久久国产 | 国产一区二区三区四区在线观看 | 免费观看一级视频 | 久久久精品网站 | 成人欧美一区二区三区白人 | 欧美中文一区二区三区 | 91久久夜色精品国产网站 | 亚洲一区中文字幕在线观看 | 亚洲卡一 | 亚洲福利在线观看 | 亚洲电影在线播放 | 91在线看 | 中文视频在线 | 成人毛片在线观看 | 亚洲免费不卡视频 | 最近中文字幕免费观看 | 性做久久久久久久免费看 | 午夜精品视频在线观看 | 欧美视频精品 | 国产在线拍揄自揄拍视频 |