Spring @Configuration 注释是 Spring 核心框架的一部分。 Spring Configuration 注释表明该类具有 @Bean 定义方法,因此 Spring 容器可以处理该类并生成 Spring 豆,用于应用程序中。
春天 配置
Spring @Configuration 注释允许我们为 依赖注射使用注释。让我们来了解如何创建 Spring Configuration 类。
1package com.journaldev.spring;
2
3public class MyBean {
4
5 public MyBean() {
6 System.out.println("MyBean instance created");
7 }
8
9}
在使用 Spring 框架类之前,我们将不得不将其依赖性添加到 maven 项目中。
1<dependency>
2 <groupId>org.springframework</groupId>
3 <artifactId>spring-context</artifactId>
4 <version>5.0.6.RELEASE</version>
5</dependency>
现在,让我们创建春季配置类。
1package com.journaldev.spring;
2
3import org.springframework.context.annotation.Bean;
4import org.springframework.context.annotation.Configuration;
5
6@Configuration
7public class MyConfiguration {
8
9 @Bean
10 public MyBean myBean() {
11 return new MyBean();
12 }
13
14}
让我们写一个简单的类,并配置我们的简单的春季配置类。
1package com.journaldev.spring;
2
3import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4
5public class MySpringApp {
6
7 public static void main(String[] args) {
8 AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
9 ctx.register(MyConfiguration.class);
10 ctx.refresh();
11
12 // MyBean mb1 = ctx.getBean(MyBean.class);
13
14 // MyBean mb2 = ctx.getBean(MyBean.class);
15
16 ctx.close();
17 }
18
19}
如果您运行应用程序以上,它将产生这样的输出:
1May 23, 2018 12:34:54 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
2INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@ff5b51f: startup date [Wed May 23 12:34:54 IST 2018]; root of context hierarchy
3MyBean instance created
4May 23, 2018 12:34:54 PM org.springframework.context.support.AbstractApplicationContext doClose
5INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@ff5b51f: startup date [Wed May 23 12:34:54 IST 2018]; root of context hierarchy
请注意,在我们甚至要求它之前,春季会将豆子加载到其背景中,这就是为了确保所有豆子都正确配置,如果有什么不对劲,应用程序就会失败。
1Exception in thread "main" java.lang.IllegalStateException: org.springframework.context.annotation.AnnotationConfigApplicationContext@f0f2775 has not been refreshed yet
2 at org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1076)
3 at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1106)
4 at com.journaldev.spring.MySpringApp.main(MySpringApp.java:11)
如果你拒绝评论我正在获得MyBean实例的陈述,你会注意到它没有调用MyBean的构建者。 这是因为 [春豆]的默认范围( / 社区 / 教程 / 春-ioc-bean - 示例 - 教程)是Singleton。 我们可以使用@Scope
注释来改变它。
如果我们删除 @Configuration 注释怎么办?
如果我们从 MyConfiguration 类中删除 @Configuration 注释,会发生什么?你会注意到它仍然按预期运作,春豆被注册并被检索为 singleton 类,但在这种情况下,如果我们调用 myBean()
方法,那么它将是一个简单的 java 方法调用,我们将得到一个新的 MyBean 实例,它不会保持 singleton. 为了证明这一点,让我们定义使用 MyBean 实例的另一个 bean。
1package com.journaldev.spring;
2
3public class MyBeanConsumer {
4
5 public MyBeanConsumer(MyBean myBean) {
6 System.out.println("MyBeanConsumer created");
7 System.out.println("myBean hashcode = "+myBean.hashCode());
8 }
9
10}
我们的更新春季配置类是:
1package com.journaldev.spring;
2
3import org.springframework.context.annotation.Bean;
4import org.springframework.context.annotation.Configuration;
5
6//@Configuration
7public class MyConfiguration {
8
9 @Bean
10 public MyBean myBean() {
11 return new MyBean();
12 }
13
14 @Bean
15 public MyBeanConsumer myBeanConsumer() {
16 return new MyBeanConsumer(myBean());
17 }
18}
现在当我们运行MySpringApp
类时,它会产生以下输出。
1MyBean instance created
2MyBean instance created
3MyBeanConsumer created
4myBean hashcode = 1647766367
所以MyBean不再是单曲,现在让我们再次注释MyConfiguration
与@Configuration
注释,然后运行MySpringApp
类。
1MyBean instance created
2MyBeanConsumer created
3myBean hashcode = 1095088856
因此,最好使用@Configuration
注释与配置类,以确保我们的春节容器以我们想要的方式行为. 如果您不希望出于一些奇怪的原因使用@Configuration
注释,我们仍然可以通过不调用myBean()
方法来创建我们的配置类,而是使用通过 @Autowired注释配置的MyBean实例变量。
1package com.journaldev.spring;
2
3import org.springframework.beans.factory.annotation.Autowired;
4import org.springframework.context.annotation.Bean;
5import org.springframework.context.annotation.Configuration;
6
7//@Configuration
8public class MyConfiguration {
9
10 @Autowired
11 MyBean myBean;
12
13 @Bean
14 public MyBean myBean() {
15 return new MyBean();
16 }
17
18 @Bean
19 public MyBeanConsumer myBeanConsumer() {
20 return new MyBeanConsumer(myBean);
21 }
22}
这就是春季配置注释的全部,我们将在未来的帖子中查看其他春季注释。
您可以从我们的 GitHub 存储库下载示例代码。