Spring 中的 JavaConfig 是什么?
What is JavaConfig in Spring?
刚刚想明白注解@Bean的含义和用法,遇到了叫JavaConfig的词in this文档(2.2.1.章) .上下文如下:
To declare a bean, simply annotate a method with the @Bean annotation. When JavaConfig encounters such a method, it will execute that method and register(...)
我不明白 JavaConfig 是什么 Spring...
它到底有什么作用?
什么时候运行?
为什么是 运行?
我也看到了 this documentation,但并没有让我更了解它。
它指的是基于注释的配置,而不是旧的、原始的基于 XML 的配置。
实际的 JavaConfig 组件是通过 class 文件和注释来构建配置的组件(与通过 XML 文件来构建配置相反)。
用@Configuration 注释class 表明class 可以被Spring IoC 容器用作bean 定义的来源。 @Bean 注释(您询问的)告诉 Spring 用 @Bean 注释的方法将 return 一个应该在 Spring 应用程序上下文中注册为 bean 的对象。最简单的@Configuration class 如下 -
package com.test;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}
以上代码将等同于以下XML配置-
<beans>
<bean id = "helloWorld" class = "com.test.HelloWorld" />
</beans>
在这里,方法名称用 @Bean 注释作为 bean ID,它创建并 returns 实际的 bean。您的配置 class 可以有多个 @Bean 的声明。定义配置 classes 后,您可以使用 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();
}
你可以加载各种配置classes如下-
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();
}
用法示例:
这是HelloWorldConfig.java文件的内容
package com.test;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}
这是HelloWorld.java文件的内容
package com.test;
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.test;
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的含义和用法,遇到了叫JavaConfig的词in this文档(2.2.1.章) .上下文如下:
To declare a bean, simply annotate a method with the @Bean annotation. When JavaConfig encounters such a method, it will execute that method and register(...)
我不明白 JavaConfig 是什么 Spring... 它到底有什么作用? 什么时候运行? 为什么是 运行?
我也看到了 this documentation,但并没有让我更了解它。
它指的是基于注释的配置,而不是旧的、原始的基于 XML 的配置。
实际的 JavaConfig 组件是通过 class 文件和注释来构建配置的组件(与通过 XML 文件来构建配置相反)。
用@Configuration 注释class 表明class 可以被Spring IoC 容器用作bean 定义的来源。 @Bean 注释(您询问的)告诉 Spring 用 @Bean 注释的方法将 return 一个应该在 Spring 应用程序上下文中注册为 bean 的对象。最简单的@Configuration class 如下 -
package com.test;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}
以上代码将等同于以下XML配置-
<beans>
<bean id = "helloWorld" class = "com.test.HelloWorld" />
</beans>
在这里,方法名称用 @Bean 注释作为 bean ID,它创建并 returns 实际的 bean。您的配置 class 可以有多个 @Bean 的声明。定义配置 classes 后,您可以使用 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();
}
你可以加载各种配置classes如下-
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();
}
用法示例:
这是HelloWorldConfig.java文件的内容
package com.test;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}
这是HelloWorld.java文件的内容
package com.test;
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.test;
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!