到目前为止,你已经看到如何使用 xml 配置文件来配置 spring bean。如果你熟悉使用 xml 配置,那么我会说,不需要再学习如何进行基于 java 的配置是,因为你要达到相同的结果,可以使用其他可用的配置。
基于 java 的配置选项,可以使你在不用配置 xml 的情况下编写大多数的 spring,但是一些有帮助的基于 java 的注解,解释如下:
带有 @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 example 章节。 |
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!
当 @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 通过构造函数注入来接收参考基准。现在,让我们看到一个正在执行的例子:
让我们在恰当的位置使用 eclipse ide,然后按照下面的步骤来创建一个 spring 应用程序:
步骤 | 描述 |
---|---|
1 | 创建一个名称为 springexample 的项目,并且在创建项目的 src 文件夹中创建一个包 com.tutorialspoint。 |
2 | 使用 add external jars 选项,添加所需的 spring 库,解释见 spring hello world example 章节。 |
3 | 因为你是使用基于 java 的注解,所以你还需要添加来自 java 安装目录的 cglib.jar 和可以从 asm.ow2.org 中下载的 asm.jar 库。 |
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 注解允许从另一个配置类中加载 @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 注解支持指定任意的初始化和销毁的回调方法,就像在 bean 元素中 spring 的 xml 的初始化方法和销毁方法的属性:
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();
}
}