Spring框架: 基于Java的配置 - Break易站
到目前为止,您已经看到了如何使用XML配置文件来配置Spring bean。如果您对XML配置感到满意,那么实际上并不需要学习如何继续使用基于Java的配置,因为您将使用任何可用配置获得相同结果。
基于Java的配置选项使您可以在不使用XML的情况下编写大部分Spring配置,但需要借助本章中介绍的少数基于Java的注释。
@Configuration和@Bean注释
使用@Configuration注解一个类表明该类可以被Spring IoC容器用作bean定义的来源。该@Bean注解告诉Spring与@Bean注释的方法将返回应注册为Spring应用程序上下文的bean的对象。最简单的@Configuration类可能如下 -
package com.breakyizhan; import org.springframework.context.annotation.*; @Configuration public class HelloWorldConfig { @Bean public HelloWorld helloWorld(){ return new HelloWorld(); } }
以上代码将等同于以下XML配置 -
<beans> <bean id = "helloWorld" class = "com.breakyizhan.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.breakyizhan包。 |
2 | 按照Spring Hello World Example章节的介绍,使用Add External JARs选项添加所需的Spring库。 |
3 | 因为您使用的是基于Java的批注,所以您还需要从Java安装目录和ASM.jar库中添加CGLIB.jar,该库可以从asm.ow2.org下载。 |
4 | 在com.breakyizhan包下创建Java类HelloWorldConfig,HelloWorld和MainApp。 |
5 | 最后一步是创建所有Java文件和Bean配置文件的内容并按照下面的说明运行应用程序。 |
这里是HelloWorldConfig.java文件的内容
package com.breakyizhan; import org.springframework.context.annotation.*; @Configuration public class HelloWorldConfig { @Bean public HelloWorld helloWorld(){ return new HelloWorld(); } }
这里是HelloWorld.java文件的内容
package com.breakyizhan; 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.breakyizhan; 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.breakyizhan; 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.breakyizhan包。 |
2 | 按照Spring Hello World Example章节的介绍,使用Add External JARs选项添加所需的Spring库。 |
3 | 因为您使用的是基于Java的批注,所以您还需要从Java安装目录和ASM.jar库中添加CGLIB.jar,该库可以从asm.ow2.org下载。 |
4 | 在com.breakyizhan包下创建Java类TextEditor, SpellChecker和MainApp。 |
5 | 最后一步是创建所有Java文件和Bean配置文件的内容并按照下面的说明运行应用程序。 |
这里是TextEditorConfig.java文件的内容
package com.breakyizhan; 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.breakyizhan; 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.breakyizhan; public class SpellChecker { public SpellChecker(){ System.out.println("Inside SpellChecker constructor." ); } public void checkSpelling(){ System.out.println("Inside checkSpelling." ); } }
以下是MainApp.java文件的内容
package com.breakyizhan; 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的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(); } }
本文来自:Spring框架: 基于Java的配置