spring4.0基于Java配置_Spring基于Java的配置

基于Java的配置选项,可以使你在不用配置XML的情况下编写大多数的Spring配置。

@Configuration和@Bean注解

带有@Configuration的注解类表示这个类可以使用Spring IoC容器作为bean定义的来源。@Bean注解告诉Spring,一个带有@Bean的注解方法将返回一个对象,该对象应该被注册为在Spring应用程序上下文中的bean。最简单可行的@Configuration类如下所示:

@Configurationpublic classHelloWorldConfig {

@BeanpublicHelloWorld helloWorld(){return newHelloWorld();

}

}

对应XML配置如下:

在这里,带有@Bean注解的方法名称作为bean的ID,它创建并返回实际的bean。你的配置类可以声明多个@Bean。一旦定义了配置类,你就可以使用AnnotationConfigApplicationContext来加载并把他们提供给Spring容器,如下所示:

public static voidmain( String[] args )

{

ApplicationContext applicationContext= new AnnotationConfigApplicationContext(HelloWorldConfig.class);

HelloWorld helloWorld = applicationContext.getBean(HelloWorld.class);

helloWorld.setMessage("Hello World!");

helloWorld.getMessage();

}

当然,你可以加载各种配置类,如下所示:

public static voidmain(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();

}

例子:

pom.xml:

4.0.0

com.jsoft.testspring

testjavaconfiguration

0.0.1-SNAPSHOT

jar

testjavaconfiguration

http://maven.apache.org

UTF-8

junit

junit

3.8.1

test

org.springframework

spring-core

4.1.4.RELEASE

org.springframework

spring-context

4.1.4.RELEASE

HelloWorld.java:

packagecom.jsoft.testspring.testjavaconfiguration;public classHelloWorld {privateString message;public voidsetMessage(String message){this.message =message;

}public voidgetMessage(){

System.out.println(this.message);

}

}

HelloWorldConfig.java:

packagecom.jsoft.testspring.testjavaconfiguration;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;

@Configurationpublic classHelloWorldConfig {

@BeanpublicHelloWorld helloWorld(){return newHelloWorld();

}

}

App.java:

packagecom.jsoft.testspring.testjavaconfiguration;importorg.springframework.context.ApplicationContext;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;/*** Hello world!

**/

public classApp

{public static voidmain( String[] args )

{

ApplicationContext applicationContext= new AnnotationConfigApplicationContext(HelloWorldConfig.class);

HelloWorld helloWorld = applicationContext.getBean(HelloWorld.class);

helloWorld.setMessage("Hello World!");

helloWorld.getMessage();

}

}

测试结果:

54483fb4534a581c8d324377903892c2.png

Bean的依赖性注入

当Beans依赖对方时,表达这种依赖性非常简单,只要有一个bean方法调用另一个,如下所示:

@Configurationpublic classHelloWorldConfig {

@Beanpublic HelloWorld helloWorld(){

return new HelloWorld(message());

}

@Bean

public String message(){

return new String("Hello World");

}

}

可以看出,有依赖性时直接在类中调用新建的方法即可。

例子:

pom.xml:

4.0.0

com.jsoft.testspring

testjavaconfiguration

0.0.1-SNAPSHOT

jar

testjavaconfiguration

http://maven.apache.org

UTF-8

junit

junit

3.8.1

test

org.springframework

spring-core

4.1.4.RELEASE

org.springframework

spring-context

4.1.4.RELEASE

HelloWorld.java:

packagecom.jsoft.testspring.testjavaconfiguration;public classHelloWorld {privateString message;publicHelloWorld(String message){this.message =message;

}public voidgetMessage(){

System.out.println(this.message);

}

}

HelloWorldConfig.java:

packagecom.jsoft.testspring.testjavaconfiguration;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;

@Configurationpublic classHelloWorldConfig {

@Beanpublic HelloWorld helloWorld(){

return new HelloWorld(message());

}

@Bean

public String message(){

return new String("Hello World");

}

}

App.java:

packagecom.jsoft.testspring.testjavaconfiguration;importorg.springframework.context.ApplicationContext;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;/*** Hello world!

**/

public classApp

{public static voidmain( String[] args )

{

ApplicationContext applicationContext= new AnnotationConfigApplicationContext(HelloWorldConfig.class);

HelloWorld helloWorld = applicationContext.getBean(HelloWorld.class);helloWorld.getMessage();

}

}

测试结果:

af0957b7ae467d29c856557a5da72087.png

@Import注释

@import注解允许从另一个配置类中加载Bean定义。如下所示:

@Configurationpublic classConfigA {

@BeanpublicA a() {return newA();

}

}

@Configuration

@Import(ConfigA.class)public classConfigB {

@BeanpublicB b() {return newB();

}

}

现在,当实例化上下文时,不需要同时指定ConfigA.class和ConfigB.class,只要ConfigB类需要提供,如下所示:

public static voidmain(String[] args) {

ApplicationContext ctx= new AnnotationConfigApplicationContext(ConfigB.class);

A a= ctx.getBean(A.class);

B b= ctx.getBean(B.class);

}

例子:

pom.xml:

4.0.0

com.jsoft.testspring

testjavaconfiguration

0.0.1-SNAPSHOT

jar

testjavaconfiguration

http://maven.apache.org

UTF-8

junit

junit

3.8.1

test

org.springframework

spring-core

4.1.4.RELEASE

org.springframework

spring-context

4.1.4.RELEASE

HelloWorld.java:

packagecom.jsoft.testspring.testjavaconfiguration;public classHelloWorld {privateString message;publicHelloWorld(String message){this.message =message;

}public voidgetMessage(){

System.out.println(this.message);

}

}

HelloWorldConfig.java:

packagecom.jsoft.testspring.testjavaconfiguration;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;

@Configurationpublic classHelloWorldConfig {

@Beanpublic HelloWorld helloWorld(){

return new HelloWorld(message());

}

@Bean

public String message(){

return new String("Hello World");

}

}

HelloWorldConfigB.java:

packagecom.jsoft.testspring.testjavaconfiguration;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.Import;

@Configuration

@Import(HelloWorldConfig.class)public classHelloWorldConfigB {

@BeanpublicString messageB(){return new String("Hello B");

}

}

App.java:

packagecom.jsoft.testspring.testjavaconfiguration;importorg.springframework.context.ApplicationContext;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;/*** Hello world!

**/

public classApp

{public static voidmain( String[] args )

{

ApplicationContext applicationContext= new AnnotationConfigApplicationContext(HelloWorldConfigB.class);

HelloWorld helloWorld = applicationContext.getBean(HelloWorld.class);helloWorld.getMessage();

}

}

可以看出不用再次引用HelloWorldConfig.java类了。

测试结果:

a7eb1aca5a30a13fba7d1ee83d6e3590.png

生命周期回调

@Bean注解支持指定任意的初始化和销毁​​回调方法,这与Spring元素中的Spring XML的init-method和destroy-method属性非常相似。如下所示:

public classFoo {public voidinit() {

}public voidcleanup() {

}

}

@Configurationpublic classAppConfig {

@Bean(initMethod= "init", destroyMethod = "cleanup" )publicFoo foo() {return newFoo();

}

}

例子:

pom.xml:

4.0.0

com.jsoft.testspring

testjavaconfiguration

0.0.1-SNAPSHOT

jar

testjavaconfiguration

http://maven.apache.org

UTF-8

junit

junit

3.8.1

test

org.springframework

spring-core

4.1.4.RELEASE

org.springframework

spring-context

4.1.4.RELEASE

HelloWorld.java:

packagecom.jsoft.testspring.testjavaconfiguration;public classHelloWorld {privateString message;publicHelloWorld(String message){this.message =message;

}public voidgetMessage(){

System.out.println(this.message);

}public void init(){

System.out.println("HelloWorld init");

}

public void cleanup(){

System.out.println("HelloWorld cleanup");

}

}

HelloWorldConfig.java:

packagecom.jsoft.testspring.testjavaconfiguration;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;

@Configurationpublic classHelloWorldConfig {

@Bean(initMethod="init",destroyMethod="cleanup")publicHelloWorld helloWorld(){return newHelloWorld(message());

}

@BeanpublicString message(){return new String("Hello World");

}

}

App.java:

packagecom.jsoft.testspring.testjavaconfiguration;importorg.springframework.context.ApplicationContext;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;importorg.springframework.context.support.AbstractApplicationContext;/*** Hello world!

**/

public classApp

{public static voidmain( String[] args )

{

AbstractApplicationContext applicationContext= new AnnotationConfigApplicationContext(HelloWorldConfig.class);

HelloWorld helloWorld= applicationContext.getBean(HelloWorld.class);

helloWorld.getMessage();

applicationContext.registerShutdownHook();

}

}

测试结果:

ac1f078e2c16651fe348ac57206ec5e7.png

指定Bean的作用域范围

默认范围是单实例,但是你可以重写带有@Scope注解的方法,如下所示:

@Configurationpublic classAppConfig {

@Bean

@Scope("prototype")publicFoo foo() {return newFoo();

}

}

例子:

pom.xml:

4.0.0

com.jsoft.testspring

testjavaconfiguration

0.0.1-SNAPSHOT

jar

testjavaconfiguration

http://maven.apache.org

UTF-8

junit

junit

3.8.1

test

org.springframework

spring-core

4.1.4.RELEASE

org.springframework

spring-context

4.1.4.RELEASE

HelloWorld.java:

packagecom.jsoft.testspring.testjavaconfiguration;public classHelloWorld {privateString message;public voidsetMessage(String message){this.message =message;

}public voidgetMessage(){

System.out.println(this.message);

}

}

HelloWorldConfig.java:

packagecom.jsoft.testspring.testjavaconfiguration;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.Scope;

@Configurationpublic classHelloWorldConfig {

@Bean

@Scope("prototype")publicHelloWorld helloWorld(){return newHelloWorld();

}

}

@Scope的值和xml中配置的保持一致。

App.java:

packagecom.jsoft.testspring.testjavaconfiguration;importorg.springframework.context.ApplicationContext;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;/*** Hello world!

**/

public classApp

{public static voidmain( String[] args )

{

ApplicationContext applicationContext= new AnnotationConfigApplicationContext(HelloWorldConfig.class);

HelloWorld helloWorld = applicationContext.getBean(HelloWorld.class);

helloWorld.setMessage("Hello World 1!");

helloWorld.getMessage();

HelloWorld helloWorld2 = applicationContext.getBean(HelloWorld.class);

helloWorld2.getMessage();

}

}

测试结果:

e1988017d16c2e3d8a508593309185b5.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值