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 | 使用 Add External JARs 选项添加所需的 Spring 库,如 Spring Hello World 示例 一章中所述。 |
| 3 | 因为您使用的是基于 Java 的注解,所以您还需要添加 Java 安装目录中的 CGLIB.jar 和可从 asm.ow2.org 下载的 ASM.jar 库。 |
| 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方法调用另一个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 | 使用 Add External JARs 选项添加所需的 Spring 库,如 Spring Hello World 示例 一章中所述。 |
| 3 | 因为您使用的是基于 Java 的注解,所以您还需要添加 Java 安装目录中的 CGLIB.jar 和可从 asm.ow2.org 下载的 ASM.jar 库。 |
| 4 | 在 com.tutorialspoint 包下创建 Java 类 TextEditorConfig、TextEditor、SpellChecker 和 MainApp。 |
| 5 | 最后一步是创建所有 Java 文件和 Bean 配置文件的内容,然后运行应用程序,如下所述。 |
Here is the content of TextEditorConfig.java file
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 作用域
默认作用域是 singleton (单例),但您可以使用 @Scope 注解覆盖它,如下所示 −
@Configuration
public class AppConfig {
@Bean
@Scope("prototype")
public Foo foo() {
return new Foo();
}
}

