最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年07月15日 08:59:22   作者:唯一浩哥  
本篇文章主要介紹了spring基礎(chǔ)系列之JavaConfig配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

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

1、規(guī)則

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

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

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

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

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

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

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

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

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

規(guī)則驗證:

首先我們創(chuàng)建幾個測試類

針對第一種注入方式:

1-StudentService

import org.springframework.stereotype.Service;

@Service
public class StudentService {
  public void study(){
    System.out.println("學(xué)生學(xué)習(xí)Java");
  }
}

2-TeacherService

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

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

public class StudentService {
  
  public void study(){
    System.out.println("學(xué)生在學(xué)習(xí)Java");
  }
  
}

2-TeacherService

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方法

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

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é)果:

七月 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

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java可重入鎖reentrantLock解析

    Java可重入鎖reentrantLock解析

    這篇文章主要介紹了Java可重入鎖reentrantLock解析,reentrantLock跟synchronized代碼結(jié)構(gòu)差不多,只是多了一個lock和unlock的過程,需要的朋友可以參考下
    2023-12-12
  • 淺談springboot中tk.mapper代碼生成器的用法說明

    淺談springboot中tk.mapper代碼生成器的用法說明

    這篇文章主要介紹了淺談springboot中tk.mapper代碼生成器的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • SpringBoot整合MybatisPlus實現(xiàn)增刪改查功能

    SpringBoot整合MybatisPlus實現(xiàn)增刪改查功能

    MybatisPlus是國產(chǎn)的第三方插件,?它封裝了許多常用的CURDapi,免去了我們寫mapper.xml的重復(fù)勞動。本文將整合MybatisPlus實現(xiàn)增刪改查功能,感興趣的可以了解一下
    2022-05-05
  • MyBatis通用Mapper和PageHelper的過程詳解

    MyBatis通用Mapper和PageHelper的過程詳解

    這篇文章主要介紹了MyBatis通用Mapper和PageHelper的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • Spring Gateway動態(tài)路由的實現(xiàn)方案

    Spring Gateway動態(tài)路由的實現(xiàn)方案

    Spring Cloud Gateway (下文簡稱 Gateway)作為微服務(wù)網(wǎng)關(guān),動態(tài)配置路由是剛需,畢竟沒人想路由一改就要重啟網(wǎng)關(guān),本文結(jié)合源碼,提供一種動態(tài)路由配置的思路,需要的朋友可以參考下
    2025-09-09
  • 使用Java的Lucene搜索工具對檢索結(jié)果進(jìn)行分組和分頁

    使用Java的Lucene搜索工具對檢索結(jié)果進(jìn)行分組和分頁

    這篇文章主要介紹了使用Java的搜索工具Lucene對檢索結(jié)果進(jìn)行分組和分頁的方法,Luence是Java環(huán)境中的一個全文檢索引擎工具包,需要的朋友可以參考下
    2016-03-03
  • 詳解Spring工廠特性

    詳解Spring工廠特性

    今天帶大家學(xué)習(xí)Spring的特性-工廠特性,文中有非常詳細(xì)的介紹及代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • 一文詳解SpringBoot?Redis多數(shù)據(jù)源配置

    一文詳解SpringBoot?Redis多數(shù)據(jù)源配置

    Spring?Boot默認(rèn)只允許一種?Redis?連接池配置,且配置受限于?Lettuce?包,不夠靈活,所以本文將為大家介紹如何自定義Redis配置方案實現(xiàn)多數(shù)據(jù)源支持,需要的可以參考下
    2024-11-11
  • SpringBoot中的Bean注入問題

    SpringBoot中的Bean注入問題

    SpringBoot開發(fā)中,Bean注入是關(guān)鍵,涉及構(gòu)造函數(shù)注入、Setter注入和字段注入等方法,常見問題包括Bean未找到、循環(huán)依賴、多個實現(xiàn)注入等,推薦使用構(gòu)造函數(shù)注入以增強(qiáng)代碼測試性和維護(hù)性,并關(guān)注Bean的生命周期和作用域
    2024-09-09
  • 基于OpenID?Connect及Token?Relay實現(xiàn)Spring?Cloud?Gateway

    基于OpenID?Connect及Token?Relay實現(xiàn)Spring?Cloud?Gateway

    這篇文章主要介紹了基于OpenID?Connect及Token?Relay實現(xiàn)Spring?Cloud?Gateway,Spring?Cloud?Gateway旨在提供一種簡單而有效的方式來路由到API,并為API提供跨領(lǐng)域的關(guān)注點,如:安全性、監(jiān)控/指標(biāo)和彈性
    2022-06-06

最新評論

晴隆县| 和硕县| 丹阳市| 同德县| 滦平县| 巴里| 新巴尔虎左旗| 伊春市| 黔西县| 梅州市| 铜鼓县| 漠河县| 石屏县| 平果县| 龙游县| 扶沟县| 利辛县| 海丰县| 略阳县| 沾化县| 垣曲县| 泸定县| 和田市| 临沂市| 景宁| 株洲市| 富裕县| 酒泉市| 濮阳市| 酉阳| 大名县| 祁东县| 宜宾县| 峨山| 马鞍山市| 丰县| 南涧| 镇康县| 河东区| 台南县| 太仆寺旗|