简体   繁体   English

为什么要在Spring Boot中使用@PropertySource时需要指定@Configuration?

[英]Why do you need to specify @Configuration when you want to use @PropertySource in Spring Boot?

I know you use @Configuration when you want to add @Bean for ApplicationContext.我知道当您想为 ApplicationContext 添加@Bean 时,您会使用@Configuration。 But why do you need to specify @Configuration when you want to use @PropertySource in Spring Boot?但是为什么要在 Spring Boot 中使用@PropertySource 时需要指定@Configuration 呢?

From the API documentaion of @PropertySource :来自@PropertySource的 API 文档:

Annotation providing a convenient and declarative mechanism for adding a PropertySource to Spring's Environment.注释提供了一种方便的声明机制,用于将PropertySource添加到 Spring 的环境中。 To be used in conjunction with @Configuration classes.@Configuration类一起使用。

So, it can be deduced that Spring will register a org.springframework.core.env.PropertySource bean if we use @PropertySource which will be managed by the Spring container.因此,可以推断,如果我们使用@PropertySource将由 Spring 容器管理,Spring 将注册一个org.springframework.core.env.PropertySource bean。 And @Configuation is needed for declaring Spring beans.并且需要@Configuation来声明 Spring bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 Spring Boot 项目中的某些依赖项不需要指定版本? - Why you don't need to specify the version for some dependencies in Spring Boot project? 为什么需要指定 Java 堆大小? - Why do you need to specify the Java heap size? 如何在Spring Boot中将基于JPA的PropertySource添加到外部配置 - How do I add a JPA based PropertySource to external configuration in Spring boot 如果设置了spring.cloud.config.server.bootstrap=true,则需要使用复合配置 - If you have set spring.cloud.config.server.bootstrap=true, you need to use a composite configuration 您如何指定要使工具栏中的元素居中? - How do you specify that you want your elements in a ToolBar to be centered? 自动配置在Spring-boot中添加PropertySource - auto-configuration adding PropertySource in spring-boot Spring Boot 使用 PropertySource 为每个环境加载配置 - Spring boot Load configuration per environment using PropertySource Spring Boot:如何在 application.properties 中指定带有破折号的环境变量? - Spring Boot: How do you specify an environment variable that has dashes in the application.properties? 在Spring中,如何指定BindingResult实现 - In Spring, how do you specify the BindingResult implementation 你如何使用 Spring 引导执行器,仅在非 Web 上使用 JMX spring 引导应用程序 - How do you use Spring Boot Actuator using JMX only on a non-web spring boot app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM