今天就跟大家聊聊有關使用spring怎么對JavaConfig進行配置,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
創新互聯專注于企業營銷型網站、網站重做改版、絳縣網站定制設計、自適應品牌網站建設、成都h5網站建設、商城建設、集團公司官網建設、外貿營銷網站建設、高端網站制作、響應式網頁設計等建站業務,價格優惠性價比高,為絳縣等各大城市提供網站開發制作服務。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
import org.springframework.stereotype.Service; @Service public class StudentService { public void study(){ System.out.println("學生學習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:這是針對第一種注入方式而設,需要在TeacherService 中定義帶參數的構造器
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("學生在學習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(); } }
執行結果:
七月 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)。
看完上述內容,你們對使用spring怎么對JavaConfig進行配置有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創新互聯行業資訊頻道,感謝大家的支持。
分享標題:使用spring怎么對JavaConfig進行配置-創新互聯
本文來源:http://vcdvsql.cn/article22/ccsejc.html
成都網站建設公司_創新互聯,為您提供ChatGPT、企業建站、App設計、做網站、品牌網站制作、用戶體驗
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯