- Spring核心基礎
- Spring - 首頁
- Spring - 概述
- Spring - 架構
- Spring - 環境設定
- Spring - Hello World示例
- Spring - IoC容器
- Spring - Bean定義
- Spring - Bean作用域
- Spring - Bean生命週期
- Spring - Bean後處理器
- Spring - Bean定義繼承
- Spring - 依賴注入
- Spring - 注入內部Bean
- Spring - 注入集合
- Spring - Bean自動裝配
- 基於註解的配置
- Spring - 基於Java的配置
- Spring - Spring中的事件處理
- Spring - Spring中的自定義事件
- Spring - Spring框架中的AOP
- Spring - JDBC框架
- Spring - 事務管理
- Spring - Web MVC框架
- Spring - 使用Log4J進行日誌記錄
- Spring問答
- Spring - 問答
- Spring有用資源
- Spring - 快速指南
- Spring - 有用資源
- Spring - 討論
Spring - 基於Java的配置
到目前為止,您已經瞭解瞭如何使用XML配置檔案配置Spring Bean。如果您熟悉XML配置,那麼實際上不需要學習如何使用基於Java的配置,因為您可以使用任何一種可用的配置來獲得相同的結果。
基於Java的配置選項使您能夠在沒有XML的情況下編寫大部分Spring配置,而是藉助本章介紹的一些基於Java的註解。
@Configuration & @Bean 註解
用@Configuration註解一個類表示該類可以被Spring IoC容器用作Bean定義的來源。@Bean註解告訴Spring,用@Bean註解的方法將返回一個物件,該物件應在Spring應用程式上下文中註冊為Bean。最簡單的@Configuration類如下所示:
package com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}
上面的程式碼等效於以下XML配置:
<beans> <bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld" /> </beans>
這裡,用@Bean註解的方法名稱用作Bean ID,它建立並返回實際的Bean。您的配置類可以包含多個@Bean的宣告。定義配置類後,您可以使用AnnotationConfigApplicationContext載入並將其提供給Spring容器,如下所示:
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.setMessage("Hello World!");
helloWorld.getMessage();
}
您可以載入各種配置類,如下所示:
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class, OtherConfig.class);
ctx.register(AdditionalConfig.class);
ctx.refresh();
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
示例
讓我們準備好一個正在執行的Eclipse IDE,並按照以下步驟建立一個Spring應用程式:
| 步驟 | 描述 |
|---|---|
| 1 | 建立一個名為SpringExample的專案,並在建立的專案中的src資料夾下建立一個包com.tutorialspoint。 |
| 2 | 使用新增外部JAR選項新增所需的Spring庫,如Spring Hello World示例章節中所述。 |
| 3 | 因為您使用的是基於Java的註解,所以您還需要新增Java安裝目錄中的CGLIB.jar和ASM.jar庫,後者可以從asm.ow2.org下載。 |
| 4 | 在com.tutorialspoint包下建立Java類HelloWorldConfig、HelloWorld和MainApp。 |
| 5 | 最後一步是建立所有Java檔案和Bean配置檔案的內容,並按照以下說明執行應用程式。 |
這是HelloWorldConfig.java檔案的內容
package com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}
這是HelloWorld.java檔案的內容
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
以下是MainApp.java檔案的內容
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
public class MainApp {
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(HelloWorldConfig.class);
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.setMessage("Hello World!");
helloWorld.getMessage();
}
}
建立完所有原始檔並添加了所需的額外庫後,讓我們執行該應用程式。您應該注意,不需要配置檔案。如果您的應用程式一切正常,它將列印以下訊息:
Your Message : Hello World!
注入Bean依賴項
當@Beans相互依賴時,表達這種依賴就像讓一個Bean方法呼叫另一個方法一樣簡單,如下所示:
package com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class AppConfig {
@Bean
public Foo foo() {
return new Foo(bar());
}
@Bean
public Bar bar() {
return new Bar();
}
}
這裡,foo Bean透過建構函式注入接收對bar的引用。現在讓我們看看另一個工作示例。
示例
讓我們準備好一個正在執行的Eclipse IDE,並按照以下步驟建立一個Spring應用程式:
| 步驟 | 描述 |
|---|---|
| 1 | 建立一個名為SpringExample的專案,並在建立的專案中的src資料夾下建立一個包com.tutorialspoint。 |
| 2 | 使用新增外部JAR選項新增所需的Spring庫,如Spring Hello World示例章節中所述。 |
| 3 | 因為您使用的是基於Java的註解,所以您還需要新增Java安裝目錄中的CGLIB.jar和ASM.jar庫,後者可以從asm.ow2.org下載。 |
| 4 | 在com.tutorialspoint包下建立Java類TextEditorConfig、TextEditor、SpellChecker和MainApp。 |
| 5 | 最後一步是建立所有Java檔案和Bean配置檔案的內容,並按照以下說明執行應用程式。 |
這是TextEditorConfig.java檔案的內容
package com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class TextEditorConfig {
@Bean
public TextEditor textEditor(){
return new TextEditor( spellChecker() );
}
@Bean
public SpellChecker spellChecker(){
return new SpellChecker( );
}
}
這是TextEditor.java檔案的內容
package com.tutorialspoint;
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker){
System.out.println("Inside TextEditor constructor." );
this.spellChecker = spellChecker;
}
public void spellCheck(){
spellChecker.checkSpelling();
}
}
以下是另一個依賴類檔案SpellChecker.java的內容
package com.tutorialspoint;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling(){
System.out.println("Inside checkSpelling." );
}
}
以下是MainApp.java檔案的內容
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
public class MainApp {
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(TextEditorConfig.class);
TextEditor te = ctx.getBean(TextEditor.class);
te.spellCheck();
}
}
建立完所有原始檔並添加了所需的額外庫後,讓我們執行該應用程式。您應該注意,不需要配置檔案。如果您的應用程式一切正常,它將列印以下訊息:
Inside SpellChecker constructor. Inside TextEditor constructor. Inside checkSpelling.
@Import 註解
@Import註解允許從另一個配置類載入@Bean定義。考慮以下ConfigA類:
@Configuration
public class ConfigA {
@Bean
public A a() {
return new A();
}
}
您可以如下所示在另一個Bean宣告中匯入上述Bean宣告:
@Configuration
@Import(ConfigA.class)
public class ConfigB {
@Bean
public B b() {
return new B();
}
}
現在,無需在例項化上下文時同時指定ConfigA.class和ConfigB.class,只需提供ConfigB即可,如下所示:
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigB.class);
// now both beans A and B will be available...
A a = ctx.getBean(A.class);
B b = ctx.getBean(B.class);
}
生命週期回撥
@Bean註解支援指定任意初始化和銷燬回撥方法,就像Spring XML在Bean元素上的init-method和destroy-method屬性一樣:
public class Foo {
public void init() {
// initialization logic
}
public void cleanup() {
// destruction logic
}
}
@Configuration
public class AppConfig {
@Bean(initMethod = "init", destroyMethod = "cleanup" )
public Foo foo() {
return new Foo();
}
}
指定Bean作用域
預設作用域是單例,但您可以使用@Scope註解覆蓋它,如下所示:
@Configuration
public class AppConfig {
@Bean
@Scope("prototype")
public Foo foo() {
return new Foo();
}
}