目录

Spring - Java Based Configuration

到目前为止,您已经了解了我们如何使用XML配置文件配置Spring bean。 如果您对XML配置感到满意,那么实际上不需要了解如何继续使用基于Java的配置,因为您将使用任一可用配置获得相同的结果。

基于Java的配置选项使您可以在没有XML的情况下编写大部分Spring配置,但是在本章中介绍的几个基于Java的注释的帮助下。

@Configuration&@Bean Annotations

使用@Configuration注释类表示Spring IoC容器可以将该类用作bean定义的源。 @Bean注释告诉Spring,使用@Bean注释的方法将返回一个应该在Spring应用程序上下文中注册为bean的对象。 最简单的@Configuration类如下 -

package com.iowiki;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
   @Bean 
   public HelloWorld helloWorld(){
      return new HelloWorld();
   }
}

上面的代码将等同于以下XML配置 -

<beans>
   <bean id = "helloWorld" class = "com.iowiki.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)

让我们有一个可用的Eclipse IDE,并按照以下步骤创建一个Spring应用程序 -

脚步 描述
1 创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个包com.iowiki
2 使用Add External JARs选项添加所需的Spring库,如Spring Hello World Example章节中所述。
3 因为您使用的是基于Java的注释,所以您还需要从Java安装目录和ASM.jar库中添加CGLIB.jarASM.jar库可以从asm.ow2.org下载。
4com.iowiki包下创建Java类HelloWorldConfigHelloWorldMainApp
5 最后一步是创建所有Java文件和Bean配置文件的内容并运行应用程序,如下所述。

这是HelloWorldConfig.java文件的内容

package com.iowiki;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
   @Bean 
   public HelloWorld helloWorld(){
      return new HelloWorld();
   }
}

这是HelloWorld.java文件的内容

package com.iowiki;
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.iowiki;
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方法调用另一个bean方法一样简单,如下所示 -

package com.iowiki;
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)

让我们有一个可用的Eclipse IDE,并按照以下步骤创建一个Spring应用程序 -

脚步 描述
1 创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个包com.iowiki
2 使用Add External JARs选项添加所需的Spring库,如Spring Hello World Example章节中所述。
3 因为您使用的是基于Java的注释,所以您还需要从Java安装目录和ASM.jar库中添加CGLIB.jarASM.jar库可以从asm.ow2.org下载。
4com.iowiki包下创建Java类TextEditorConfigTextEditorMainAppMainApp
5 最后一步是创建所有Java文件和Bean配置文件的内容并运行应用程序,如下所述。

这是TextEditorConfig.java文件的内容

package com.iowiki;
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.iowiki;
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.iowiki;
public class SpellChecker {
   public SpellChecker(){
      System.out.println("Inside SpellChecker constructor." );
   }
   public void checkSpelling(){
      System.out.println("Inside checkSpelling." );
   }
}

以下是MainApp.java文件的内容

package com.iowiki;
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 a() {
      return new A(); 
   }
}

现在,在实例化上下文时,不需要同时指定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的init-method和bean元素上的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();
   }
}
↑回到顶部↑
WIKI教程 @2018