spring java code配置_Spring 基于 Java 的配置

基于 Java 的配置

到目前为止,你已经看到如何使用 XML 配置文件来配置 Spring bean。如果你熟悉使用 XML 配置,那么我会说,不需要再学习如何进行基于 Java 的配置是,因为你要达到相同的结果,可以使用其他可用的配置。

基于 Java 的配置选项,可以使你在不用配置 XML 的情况下编写大多数的 Spring,但是一些有帮助的基于 Java 的注解,解释如下:

@Configuration 和 @Bean 注解

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

packagecom.tutorialspoint;

importorg.springframework.context.annotation.*;

@Configuration

public class HelloWorldConfig {

@Bean

public HelloWorldhelloWorld(){

return new HelloWorld();

}

}

上面的代码将等同于下面的 XML 配置:

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

public static voidmain(String[]args) {

ApplicationContextctx=

new AnnotationConfigApplicationContext(HelloWorldConfig.class);

HelloWorldhelloWorld=ctx.getBean(HelloWorld.class);helloWorld.setMessage("Hello World!");helloWorld.getMessage();

}

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

public static voidmain(String[]args) {

AnnotationConfigApplicationContextctx=

new AnnotationConfigApplicationContext();ctx.register(AppConfig.class, OtherConfig.class);ctx.register(AdditionalConfig.class);ctx.refresh();

MyServicemyService=ctx.getBean(MyService.class);myService.doStuff();

}

例子

让我们在恰当的位置使用 Eclipse IDE,然后按照下面的步骤来创建一个 Spring 应用程序:

步骤

描述

1

创建一个名称为SpringExample的项目,并且在创建项目的src文件夹中创建一个包com.tutorialspoint。

2

使用AddExternalJARs选项,添加所需的Spring库,解释见SpringHelloWorldExample章节。

3

因为你是使用基于java的注解,所以你还需要添加来自Java安装目录的CGLIB.jar和可以从asm.ow2.org中下载的ASM.jar库。

4

在com.tutorialspoint包中创建Java类HelloWorldConfig、HelloWorld和MainApp。

5

最后一步是创建的所有Java文件和Bean配置文件的内容,并运行应用程序,解释如下所示。

这里是 HelloWorldConfig.java 文件的内容:

packagecom.tutorialspoint;

importorg.springframework.context.annotation.*;

@Configuration

public class HelloWorldConfig {

@Bean

public HelloWorldhelloWorld(){

return new HelloWorld();

}

}

这里是 HelloWorld.java 文件的内容:

packagecom.tutorialspoint;

public class HelloWorld {

private Stringmessage;

public voidsetMessage(Stringmessage){

this.message=message;

}

public voidgetMessage(){

System.out.println("Your Message : " +message);

}

}

下面是 MainApp.java 文件的内容:

packagecom.tutorialspoint;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.annotation.*;

public class MainApp {

public static voidmain(String[]args) {

ApplicationContextctx=

new AnnotationConfigApplicationContext(HelloWorldConfig.class);

HelloWorldhelloWorld=ctx.getBean(HelloWorld.class);helloWorld.setMessage("Hello World!");helloWorld.getMessage();

}

}

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

Your Message : Hello World!

注入 Bean 的依赖性

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

packagecom.tutorialspoint;

importorg.springframework.context.annotation.*;

@Configuration

public class AppConfig {

@Bean

public Foofoo() {

return new Foo(bar());

}

@Bean

public Barbar() {

return new Bar();

}

}

这里,foo Bean 通过构造函数注入来接收参考基准。现在,让我们看到一个正在执行的例子:

例子:

让我们在恰当的位置使用 Eclipse IDE,然后按照下面的步骤来创建一个 Spring 应用程序:

步骤

描述

1

创建一个名称为SpringExample的项目,并且在创建项目的src文件夹中创建一个包com.tutorialspoint。

2

使用AddExternalJARs选项,添加所需的Spring库,解释见SpringHelloWorldExample章节。

3

因为你是使用基于java的注解,所以你还需要添加来自Java安装目录的CGLIB.jar和可以从asm.ow2.org中下载的ASM.jar库。

4

在com.tutorialspoint包中创建Java类TextEditorConfig、TextEditor、SpellChecker和MainApp。

5

最后一步是创建的所有Java文件和Bean配置文件的内容,并运行应用程序,解释如下所示。

这里是 TextEditorConfig.java 文件的内容:

packagecom.tutorialspoint;

importorg.springframework.context.annotation.*;

@Configuration

public class TextEditorConfig {

@Bean

public TextEditortextEditor(){

return new TextEditor(spellChecker() );

}

@Bean

public SpellCheckerspellChecker(){

return new SpellChecker( );

}

}

这里是 TextEditor.java 文件的内容:

packagecom.tutorialspoint;

public class TextEditor {

private SpellCheckerspellChecker;

public TextEditor(SpellCheckerspellChecker){

System.out.println("Inside TextEditor constructor." );

this.spellChecker=spellChecker;

}

public voidspellCheck(){spellChecker.checkSpelling();

}

}

下面是另一个依赖的类文件 SpellChecker.java 的内容:

packagecom.tutorialspoint;

public class SpellChecker {

public SpellChecker(){

System.out.println("Inside SpellChecker constructor." );

}

public voidcheckSpelling(){

System.out.println("Inside checkSpelling." );

}

}

下面是 MainApp.java 文件的内容:

packagecom.tutorialspoint;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.annotation.*;

public class MainApp {

public static voidmain(String[]args) {

ApplicationContextctx=

new AnnotationConfigApplicationContext(TextEditorConfig.class);

TextEditorte=ctx.getBean(TextEditor.class);te.spellCheck();

}

}

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

Inside SpellCheckerconstructor.

Inside TextEditorconstructor.

InsidecheckSpelling.

@Import 注解:

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

@Configuration

public class ConfigA {

@Bean

publicA a() {

return newA();

}

}

你可以在另一个 Bean 声明中导入上述 Bean 声明,如下所示:

@Configuration

@Import(ConfigA.class)

public class ConfigB {

@Bean

publicB a() {

return newA();

}

}

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

public static voidmain(String[]args) {

ApplicationContextctx=

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 voidinit() {

// initialization logic

}

public voidcleanup() {

// destruction logic

}

}

@Configuration

public class AppConfig {

@Bean(initMethod= "init",destroyMethod= "cleanup" )

public Foofoo() {

return new Foo();

}

}

指定 Bean 的范围:

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

@Configuration

public class AppConfig {

@Bean

@Scope("prototype")

public Foofoo() {

return new Foo();

}

}

作者:w3cschool | 来源:https://www.w3cschool.cn

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值