目前你已经明白如何通过XML配置Springbeaan。如果你用XML配置很舒适,然而我会说你确实没必要学习如何处理基于Java的配置,因为你将获得和使用其中一种配置相同的结果。
基于Java的配置选择可以使你不使用XML配置完成大部分的Spring配置,但是需要几个基于Java的注解,解释如下。
@Configuration & @Bean注解
带有@Configuration注解的类表明这个类可以用于SpringIoC容器作为bean定义的元数据。@Bean注解告诉Spring一个方法带有@Bean注解并会返回个一个bean对象注册到Spring应用上下文中。最简单的@Configuration类应该是这样:
package com.tutorialspoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@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。一旦你的配置类定义了,你可以装载并提供它们给Spring容器通过AnnotationConfigApplicationContext 如下:
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的注解,因此你需要增加CGLIB.jar和ASM.jar从你Java的安装目录,可以从asm.ow2.org下载。 |
4 |
在com.tutorialspoint包下创建HelloWorldConfig、 HelloWorld 、 MainApp 类。 |
5 |
最后一步在Java类中和Bean配置文件中添加内容,并运行应用。 |
如下是HelloWorldConfig的源代码:
package com.tutorialspoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld() {
return new HelloWorld();
}
}
如下是HelloWorld的源代码:
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的源代码:
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainApp16 {
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依赖
当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的引用通过构造注入,现在让我们看一个工作的例子:
示例
让我们使用EclipseIDE按如下的步骤创建一个Spring应用:
步骤 |
描述 |
1 |
创建一个SpringExample的项目并在src目录下创建com.tutorialspoint包。 |
2 |
在Add External JARs选项卡中添加Spring库如在Spring Hello World章节中所讲。 |
3 |
因为你使用基于Java的注解,因此你需要增加CGLIB.jar和ASM.jar从你Java的安装目录,可以从asm.ow2.org下载。 |
4 |
在com.tutorialspoint包下创建TextEditorConfig、 TextEditor、SpellChecker、 MainApp 类。 |
5 |
最后一步在Java类中和Bean配置文件中添加内容,并运行应用。 |
如下是TextEditorConfig源代码:
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源代码:
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源代码:
package com.tutorialspoint;
public class SpellChecker {
public SpellChecker() {
System.out.println("Inside SpellChecker constructor.");
}
public void checkSpelling() {
System.out.println("Inside checkSpelling.");
}
}
如下是MainApp源代码:
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainApp16 {
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类如下:
package com.tutorialspoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@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);
}
声明周期回调(Lifecycle Callbacks)
@Bean注解支持指明任意的初始化和销毁回调方法,很像Spring XML配置中bean的属性的init-method和destroy-method。(译者备注:初始化方法和销毁方法都是在Bean对象本身中声明的)
package com.tutorialspoint;
public class Foo {
public void init() {
// initialization logic
}
public void cleanup() {
// destruction logic
}
}
@Configuration
public classAppConfig {
@Bean(initMethod = "init", destroyMethod ="cleanup")
publicFoo foo() {
return new Foo ();
}
}
指定Bean的范围
默认的范围是singleton,但你可以覆盖这个范围使用@Scope注解,如下:
@Configuration
public class AppConfig {
@Bean
@Scope("prototype")
public Foo foo() {
return new Foo ();
}
}