Spring - 基於Java配置的註解

如果你已經很好使用XML的配置文件的方式實現依賴注入,那麼沒有那麼必要再去學習如果使用基於Java配置的方式實現依賴注入,因爲兩者是相同的作用。

使用Java配置的方式可以讓你在沒有XML配置文件的方式下實現依賴注入,這章我們將簡單介紹。

@Configuration & @Bean Annotations

使用@Configuration的類表明這個類可以被IoC容器使用,並指明相關bean的定義。@Bean註解的方法是告訴Spring容器,這個方法將返回一個對象,並且這個對象應該在Spring application context中被註冊成一bean。

簡單的@Configuration類如下所示:

package com.soygrow;
import org.springframework.context.annotation.*;

@Configuration
public class HelloWorldConfig {
   @Bean 
   public HelloWorld helloWorld(){
      return new HelloWorld();
   }
}

以上是一個Java的配置類,相當於XML中下面配置:

<beans>
    <bean id = "helloWorld" class = "com.soygrow.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();
}

Example

HelloWorld.java

package com.soygrow.JavaConfig;

public class HelloWorld {
    private String message;

    public void getMessage() {
        System.out.println("Your Message : " + message);
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

HelloWorldConfig.java

package com.soygrow.JavaConfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HelloWorldConfig {
    @Bean
    public HelloWorld helloWorld() {
        return new HelloWorld();
    }
}

MainApp.java

package com.soygrow.JavaConfig;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

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 Java Config!");
        helloWorld.getMessage();
    }
}

如果一切正常,那麼運行結果是:

Your Message : Hello World Java Config!

代碼說明
- @Bean聲明一個被Spring 容器使用的bean
- 使用對應的方式加載Java配置的方式

Injecting Bean Dependencies

當@Bean註解的bean依賴另外一個註解的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實例的引用的構造注入,下面讓我們看另外一個例子。

Example

TextEditor.java

package com.soygrow.JavaConfig;

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.soygrow.JavaConfig;

public class SpellChecker {
    public SpellChecker(){
        System.out.println("Inside SpellChecker constructor." );
    }
    public void checkSpelling(){
        System.out.println("Inside checkSpelling." );
    }
}

TextEditorConfig.java

package com.soygrow.JavaConfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TextEditorConfig {
    @Bean
    public TextEditor textEditor() {
        return new TextEditor(spellChecker());
    }

    @Bean
    public SpellChecker spellChecker() {
        return new SpellChecker();
    }
}

MainApp.java

package com.soygrow.JavaConfig;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        test2();
    }

    public static void test2() {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(TextEditorConfig.class);
        TextEditor te = ctx.getBean(TextEditor.class);
        te.spellCheck();
    }
}

The @Import Annotation

@Import註解可以允許從其他配置類中加載@Bean的定義。如下所示:

@Configuration
public class ConfigA {
   @Bean
   public A a() {
      return new A(); 
   }
}
@Configuration
@Import(ConfigA.class)
public class ConfigB {
   @Bean
   public B a() {
      return new A(); 
   }
}

現在你可以使用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);
}

Lifecycle Callbacks

使用@Bean支持指定任意的初始化和銷燬的回調方法,和XML中的init-methoddestroy-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();
   }
}

Specifying Bean Scope

和XML一樣默認的scope是單例(Singleton),但是你可以像下面這樣使用@Scope註解:

@Configuration
public class AppConfig {
   @Bean
   @Scope("prototype")
   public Foo foo() {
      return new Foo();
   }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章