- Spring核心基础
- Spring - 首页
- Spring - 概述
- Spring - 架构
- Spring - 环境设置
- Spring - Hello World示例
- Spring - IoC容器
- Spring - Bean定义
- Spring - Bean作用域
- Spring - Bean生命周期
- Spring - Bean后处理器
- Spring - Bean定义继承
- Spring - 依赖注入
- Spring - 注入内部Bean
- Spring - 注入集合
- Spring - Bean自动装配
- 基于注解的配置
- Spring - 基于Java的配置
- Spring - Spring中的事件处理
- Spring - Spring中的自定义事件
- Spring - Spring框架中的AOP
- Spring - JDBC框架
- Spring - 事务管理
- Spring - Web MVC框架
- Spring - 使用Log4J进行日志记录
- Spring问答
- Spring - 问答
- Spring有用资源
- Spring - 快速指南
- Spring - 有用资源
- Spring - 讨论
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 | 使用添加外部JAR选项添加所需的Spring库,如Spring Hello World示例章节中所述。 |
| 3 | 因为您使用的是基于Java的注解,所以您还需要添加Java安装目录中的CGLIB.jar和ASM.jar库,后者可以从asm.ow2.org下载。 |
| 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方法调用另一个方法一样简单,如下所示:
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 | 使用添加外部JAR选项添加所需的Spring库,如Spring Hello World示例章节中所述。 |
| 3 | 因为您使用的是基于Java的注解,所以您还需要添加Java安装目录中的CGLIB.jar和ASM.jar库,后者可以从asm.ow2.org下载。 |
| 4 | 在com.tutorialspoint包下创建Java类TextEditorConfig、TextEditor、SpellChecker和MainApp。 |
| 5 | 最后一步是创建所有Java文件和Bean配置文件的内容,并按照以下说明运行应用程序。 |
这是TextEditorConfig.java文件的内容
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作用域
默认作用域是单例,但您可以使用@Scope注解覆盖它,如下所示:
@Configuration
public class AppConfig {
@Bean
@Scope("prototype")
public Foo foo() {
return new Foo();
}
}