当前位置:K88软件开发文章中心编程语言JavaSpring → 文章内容

Spring 基于 Java 的配置

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 16:20:56

由 陈 创建, 最后一次修改 2016-08-12 基于 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.*;@Configurationpublic 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.*;@Configurationpublic 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.*;@Configurationpublic 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.*;@Configurationpublic 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 spe

[1] [2]  下一页


Spring 基于 Java 的配置