到目前为止,您已经了解了如何配置使用XML配置文件的Spring bean。如果习惯使用XML配置,那么会说,这不是真的需要学习如何进行基于Java的配置,因为你要使用的配置或者可达到相同的结果。
基于Java配置选项,可以编写大多数的Spring不用配置XML,但有几个基于Java的注释的帮助下解释。
注释类与@Configuration表示这个类可以使用Spring IoC容器为bean定义的来源。在@Bean 注解告诉Spring的注解为@Bean的一个方法将返回应注册为在Spring应用程序上下文中的bean对象。最简单可行的@Configuration类将如下所示:
package com.yiibai; import org.springframework.context.annotation.*; @Configuration public class HelloWorldConfig { @Bean public HelloWorld helloWorld(){ return new HelloWorld(); } }
上面的代码将等同于下面的XML配置:
<beans> <bean id="helloWorld" class="com.yiibai.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 | Create a project with a name SpringExample and create a package com.yiibai under the src folder in the created project. |
2 | Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 | Because you are using Java-based annotations, so you also need to add CGLIB.jar from your Java installation directory and ASM.jar library which can be downloaded from asm.ow2.org. |
4 | Create Java classes HelloWorldConfig, HelloWorld and MainApp under the com.yiibaipackage. |
5 | The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
这里是HelloWorldConfig.java文件的内容:
package com.yiibai; import org.springframework.context.annotation.*; @Configuration public class HelloWorldConfig { @Bean public HelloWorld helloWorld(){ return new HelloWorld(); } }
这里是HelloWorld.java的文件的内容:
package com.yiibai; 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.yiibai; 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对彼此的依赖,表达这种依赖很简单,只要有一个Bean的方法调用另一个如下:
package com.yiibai; import org.springframework.context.annotation.*; @Configuration public class AppConfig { @Bean public Foo foo() { return new Foo(bar()); } @Bean public Bar bar() { return new Bar(); } }
在这里,Bean接收基准通过构造函数注入。现在,让我们看到一个正常工作的例子:
让我们使用Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:
步骤 | 描述 |
---|---|
1 | Create a project with a name SpringExample and create a package com.yiibai under the src folder in the created project. |
2 | Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 | Because you are using Java-based annotations, so you also need to add CGLIB.jar from your Java installation directory and ASM.jar library which can be downloaded from asm.ow2.org. |
4 | Create Java classes TextEditorConfig, TextEditor, SpellChecker and MainApp under thecom.yiibai package. |
5 | The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
这里是TextEditorConfig.java文件的内容:
package com.yiibai; 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.yiibai; 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.yiibai; public class SpellChecker { public SpellChecker(){ System.out.println("Inside SpellChecker constructor." ); } public void checkSpelling(){ System.out.println("Inside checkSpelling." ); } }
以下是MainApp.java文件的内容:
package com.yiibai; 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的注解允许加载@Bean从另一个配置类定义。考虑一个配置类,如下所示:
@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(); } }
现在,不需要实例化的前提下,当同时指定配置A.class和配置B.class,只有Config B类需要如下提供:
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元素销毁方法的属性:
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(); } }
默认范围是单例,但可以使用@Scope注解来覆盖此如下:
@Configuration public class AppConfig { @Bean @Scope("prototype") public Foo foo() { return new Foo(); } }