内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

使用spring怎么對(duì)JavaConfig進(jìn)行配置-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān)使用spring怎么對(duì)JavaConfig進(jìn)行配置,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)專注于企業(yè)營(yíng)銷型網(wǎng)站、網(wǎng)站重做改版、絳縣網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)商城建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為絳縣等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。

1、規(guī)則

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

我們?cè)诙xJavaConfig類時(shí),都會(huì)在其上加注@Configuration注解,來(lái)表明這是一個(gè)配置類,@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)注的類統(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-方法帶返回值,且返回類型為你要加載的第三方類類型

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

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

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

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

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

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:這是針對(duì)第一種注入方式而設(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);
  }
  
}

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

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-測(cè)試類: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

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

看完上述內(nèi)容,你們對(duì)使用spring怎么對(duì)JavaConfig進(jìn)行配置有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

分享標(biāo)題:使用spring怎么對(duì)JavaConfig進(jìn)行配置-創(chuàng)新互聯(lián)
本文來(lái)源:http://www.rwnh.cn/article22/ccsejc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、企業(yè)建站App設(shè)計(jì)、做網(wǎng)站、品牌網(wǎng)站制作、用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)
开化县| 阿拉善盟| 上思县| 镇原县| 旺苍县| 呼玛县| 南昌市| 常宁市| 大余县| 鄱阳县| 寻乌县| 洞头县| 庄河市| 翁牛特旗| 濮阳县| 丘北县| 克什克腾旗| 陕西省| 唐河县| 宣城市| 那坡县| 富民县| 湖口县| 黎平县| 东海县| 湟源县| 辽源市| 大宁县| 玉龙| 当阳市| 冀州市| 遂昌县| 游戏| 宝丰县| 宿州市| 山阴县| 海兴县| 栾城县| 犍为县| 修武县| 双牌县|