Spring_12_Spring基于java配置

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();
}

案例:

步骤描述
1创建一个名称为 SpringExample 的项目,并且在创建项目的 c 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.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HelloWorldConfig {

    @Bean
    public HelloWorld helloWorld(){
        return new HelloWorld();
    }


}

HelloWorld.java

package com.tutorialspoint;

public class HelloWorld {
    private String message;

    public void getMessage() {
        System.out.println("message:"+message);
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

MainApp.java

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(
                HelloWorldConfig.class);
        HelloWorld helloWorld = context.getBean(HelloWorld.class);
        helloWorld.setMessage("helloMessage");
        helloWorld.getMessage();
    }
}

输出结果

message:helloMessage

注入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 通过构造函数注入来接收参考基准.现在让我们来看下一个例子.

例子:

步骤描述
1创建一个名称为 SpringExample 的项目,并且在创建项目的 c 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.Bean;
import org.springframework.context.annotation.Configuration;

@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("TextEditor  类 的  构造方法   ");
        this.spellChecker = spellChecker;
    }

    public void spellCheck() {
        spellChecker.checkSpelling();
    }

}

SpellChecker.java

package com.tutorialspoint;

public class SpellChecker {

    public SpellChecker() {
        System.out.println("SpellChecker  的  空参构造方法");
    }

    public void checkSpelling() {
        System.out.println("  SpellChecker 类的   方法    checkSpelling()");
    }

}

MainApp.java

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(
                TextEditorConfig.class);
        TextEditor te = context.getBean(TextEditor.class);
        te.spellCheck();
    }
}

一旦你完成创建所有的源文件并添加所需额外的库后,我们就可以运行该程序,你应该注意到这里不需要配置文件.如果应用程序一切正常,将输出以下信息.

SpellChecker  的  空参构造方法
TextEditor  类 的  构造方法   
  SpellChecker 类的   方法    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 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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值