在非 spring 注入 class 中使用 application.properties
Use application.properties in a non-spring injected class
我有一个 class,它是在 class 中用 new B(this);
创建的 我想使用 application.properties 中的值。但是因为(据我所知)因为它不是用 Spring 创建的,所以不会有任何注入(我使用 @Value
注释)
这就是 class 的创建方式:
@Component
public class A {
public B getB(){return new B(this);}
/**
a lot more stuff
**/
}
有问题的class:
public class B {
private A a;
public B(A a){
this.a = a;
}
@Value("${threshold}")
private String threshold; //this is null because it's not created with Spring
public String getThreshold(){
return threshold;
}
/**
a lot more stuff
**/
}
所以我的问题如下:
1) 如何使用 application.properties 文件或
中的值
2) 如何写 B 它是用 Spring 创建的?
一些背景信息:
- 我没有写初始代码所以不想改动太多
但想修改它以便将来更好地维护它
- 我没有那么多 Spring 知识,只会开始获得更多
也越来越熟悉了。
- 在第 2 点中,我正在苦苦挣扎,因为构造函数以及如何通过 Spring...
设置它
如有任何帮助,我们将不胜感激
这是一个使用 @ConfigurationProperties
将您的 application.properties
绑定到 POJO 的示例。
假设您有一个这样的 application.properties
文件,
mail.hostname=mailer@mail.com
mail.port=9000
您可以为上述场景创建这样的 POJO。
(注意: 如果您的 spring 引导版本低于 2.2,您可能需要用 @Configuration
和 @Component
还有)
@ConfigurationProperties(prefix = "mail")
public class ConfigProperties {
private String hostname;
private String port;
// Create Getters and Setters
}
当 spring 引导应用程序初始化时,它会自动将 application.properties
中的值映射到此 POJO。如果你想使用它,你所要做的就是创建一个变量并用 @Autowire
标记它
@Component
public class TestClass {
@Autowire
private ConfigProperties properties;
public void printSomething() {
System.println(properties.getHostname());
System.println(properties.getPort());
}
}
正在跟进您的问题...
由于您的示例中的 class 不是由 spring 管理的,并且出于某种原因您必须保持这种方式,您可以使用以下帮助程序 class 手动在非 spring 管理的 class.
中自动装配一个 spring bean
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
@Service
public class BeanUtil implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
/**
*Use this method to manually autowire Spring Beans into classes that are not managed by Spring.
*
*@param beanClass - Class type of the required bean.
**/
public static <T> T getBean(Class<T> beanClass) {
return context.getBean(beanClass);
}
}
要在您的 class B
中使用它,请执行以下操作。
public class B {
private ConfigProperties properties;
public B() {
BeanUtil.getBean(ConfigProperties.class); // This will initialize properties variable properly.
}
public void printSomething() {
System.println(properties.getHostname());
System.println(properties.getPort());
}
}
你可以这样写:
@Component
public class A {
@Value("${threshold}")
private String threshold;
public B getB(){
return new B(this);
}
public String getThreshold() {
return threshold;
}
}
public class B {
private A a;
private String threshold;
public B(A a){
this.a = a;
this.threshold=a.getThreshold();
}
public String getThreshold(){
return threshold;
}
}
您可以创建一个 class 来直接从文件加载属性。
该文件必须在资源包中。
配置属性 class 其中定义了这些属性
@ConfigurationProperties(prefix = "reader")
public class ReaderProperties {
private String threshold;
}
属性class
import java.util.Properties;
class MyProperties {
private final Properties props;
private MyProperties(Class<?> propertiesClass) throws IOException {
this.props = new Properties();
this.props.load(propertiesClass.getResourceAsStream("/application.properties"));
}
public String getThreshold() {
return props.getProperty("threshold");
}
}
然后在B里面:
public class B {
private A a;
private String threshold;
public B(A a){
this.a = a;
MyProperties props = new MyProperties(ReaderProperties.class);
this.threshold = props.getThreshold();
}
public String getThreshold(){
return threshold;
}
/**
a lot more stuff
**/
}
我有一个 class,它是在 class 中用 new B(this);
创建的 我想使用 application.properties 中的值。但是因为(据我所知)因为它不是用 Spring 创建的,所以不会有任何注入(我使用 @Value
注释)
这就是 class 的创建方式:
@Component
public class A {
public B getB(){return new B(this);}
/**
a lot more stuff
**/
}
有问题的class:
public class B {
private A a;
public B(A a){
this.a = a;
}
@Value("${threshold}")
private String threshold; //this is null because it's not created with Spring
public String getThreshold(){
return threshold;
}
/**
a lot more stuff
**/
}
所以我的问题如下:
1) 如何使用 application.properties 文件或
中的值2) 如何写 B 它是用 Spring 创建的?
一些背景信息:
- 我没有写初始代码所以不想改动太多 但想修改它以便将来更好地维护它
- 我没有那么多 Spring 知识,只会开始获得更多 也越来越熟悉了。
- 在第 2 点中,我正在苦苦挣扎,因为构造函数以及如何通过 Spring... 设置它
如有任何帮助,我们将不胜感激
这是一个使用 @ConfigurationProperties
将您的 application.properties
绑定到 POJO 的示例。
假设您有一个这样的 application.properties
文件,
mail.hostname=mailer@mail.com
mail.port=9000
您可以为上述场景创建这样的 POJO。
(注意: 如果您的 spring 引导版本低于 2.2,您可能需要用 @Configuration
和 @Component
还有)
@ConfigurationProperties(prefix = "mail")
public class ConfigProperties {
private String hostname;
private String port;
// Create Getters and Setters
}
当 spring 引导应用程序初始化时,它会自动将 application.properties
中的值映射到此 POJO。如果你想使用它,你所要做的就是创建一个变量并用 @Autowire
@Component
public class TestClass {
@Autowire
private ConfigProperties properties;
public void printSomething() {
System.println(properties.getHostname());
System.println(properties.getPort());
}
}
正在跟进您的问题...
由于您的示例中的 class 不是由 spring 管理的,并且出于某种原因您必须保持这种方式,您可以使用以下帮助程序 class 手动在非 spring 管理的 class.
中自动装配一个 spring beanimport org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
@Service
public class BeanUtil implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
/**
*Use this method to manually autowire Spring Beans into classes that are not managed by Spring.
*
*@param beanClass - Class type of the required bean.
**/
public static <T> T getBean(Class<T> beanClass) {
return context.getBean(beanClass);
}
}
要在您的 class B
中使用它,请执行以下操作。
public class B {
private ConfigProperties properties;
public B() {
BeanUtil.getBean(ConfigProperties.class); // This will initialize properties variable properly.
}
public void printSomething() {
System.println(properties.getHostname());
System.println(properties.getPort());
}
}
你可以这样写:
@Component
public class A {
@Value("${threshold}")
private String threshold;
public B getB(){
return new B(this);
}
public String getThreshold() {
return threshold;
}
}
public class B {
private A a;
private String threshold;
public B(A a){
this.a = a;
this.threshold=a.getThreshold();
}
public String getThreshold(){
return threshold;
}
}
您可以创建一个 class 来直接从文件加载属性。 该文件必须在资源包中。
配置属性 class 其中定义了这些属性
@ConfigurationProperties(prefix = "reader")
public class ReaderProperties {
private String threshold;
}
属性class
import java.util.Properties;
class MyProperties {
private final Properties props;
private MyProperties(Class<?> propertiesClass) throws IOException {
this.props = new Properties();
this.props.load(propertiesClass.getResourceAsStream("/application.properties"));
}
public String getThreshold() {
return props.getProperty("threshold");
}
}
然后在B里面:
public class B {
private A a;
private String threshold;
public B(A a){
this.a = a;
MyProperties props = new MyProperties(ReaderProperties.class);
this.threshold = props.getThreshold();
}
public String getThreshold(){
return threshold;
}
/**
a lot more stuff
**/
}