[英]Get value from application.properties without Main application
I' m working in a Spring Boot project, consisted in several applications booted with embedded Tomcat and a unique application used only as library for running applications.我正在一个 Spring Boot 项目中工作,该项目由几个使用嵌入式 Tomcat 启动的应用程序和一个仅用作运行应用程序的库的独特应用程序组成。 In that library application I need to retrieve a value from application.properties file for a variable.
在那个库应用程序中,我需要从 application.properties 文件中检索一个变量的值。 Being it a library application, it has not a Main class, so the variable will be always null.
由于它是一个库应用程序,它没有 Main 类,因此该变量将始终为 null。 My try was these:
我的尝试是这些:
public class AuthAdapter implements IAuthAdapter {
@Value("${signin.key}")
private String key;
@Override
public String getSigningKey() {
return key;
}
}
When the getSigninKey() is invoked by an application that uses it, the variable key is null.当使用 getSigninKey() 的应用程序调用 getSigninKey() 时,变量 key 为 null。 How can I do to fill the variable key with the value present on appliaction.properties file, considering the situation explained before?
考虑到之前解释的情况,我该如何用 appliaction.properties 文件中存在的值填充变量键?
You need to annotate your AuthAdapter class with @Service/@Component/@Repository/@Configuration based on your requirement.您需要根据您的要求使用 @Service/@Component/@Repository/@Configuration 注释您的 AuthAdapter 类。 By doing so, spring boot will inject the application.properties to your class during the spring context initialization.
通过这样做,spring boot 将在 spring 上下文初始化期间将 application.properties 注入您的类。
@Value Spring annotation @Value Spring 注解
This annotation can be used for injecting values into fields in Spring-managed beans
此注解可用于将值注入 Spring 管理的 bean 中的字段
So make AuthAdapter
as spring bean in some config class所以在一些配置类
AuthAdapter
作为 spring bean
@Configuration
public class ApplicationConfig {
@Bean
public AuthAdapter getAuthAdapter() {
return new AuthAdapter():
}
}
with property in application.yml
or application.properties
在
application.yml
或application.properties
有属性
application.yml应用程序.yml
signin:
key: value
Or if AuthAdapter
is annotated with any stereotype annotations here , Just enable that class in ComponentScan
或者,如果
AuthAdapter
在此处使用任何AuthAdapter
型注释进行注释,只需在ComponentScan
启用该类
@SpringBootApplication
@ComponentScan({ "com.project1", "com.library.AuthAdapter" })
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class);
}
}
If the variable key==null, this means that Spring did not inject it.如果变量key==null,这意味着Spring没有注入它。 This could come from sevreral reasons:
这可能来自几个原因:
the property "signin.key" is not present in the application.properties application.properties 中不存在属性“signin.key”
SpringBoot did not start => make sure that in your application there is a main annoted with @springbootapplication SpringBoot 没有启动 => 确保在您的应用程序中有一个带有 @springbootapplication 的 main 注释
The AuthAdapter is not instanciate by Spring: AuthAdapter 不是由 Spring 实例化的:
=> The AuthAdapter shall not be instanciate like: => AuthAdapter 不应像这样实例化:
authAdapter = new AuthAdapter();
Instead, it must be instanciate and injected by Spring itself like:相反,它必须由 Spring 本身实例化并注入,例如:
@Autowired
private authAdapter AuthAdapter;
For that the AuthAdapter must be annoted with a springBoot annotation: @Service/@Component/@Repository/@Configuration
.为此,必须使用 springBoot 注释对 AuthAdapter 进行注释:
@Service/@Component/@Repository/@Configuration
。
Finally, the package containing the AuthAdapter class must be in the component scan perimeter to tell Spring to instanciate it.最后,包含 AuthAdapter 类的包必须在组件扫描范围内,以告诉 Spring 实例化它。
确保“signin.key”存在于 application.properties 中并且存在于生成的 jar 中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.