在非 spring 注入类中使用 application.properties

Boe*_*dal 2 java javabeans properties-file spring-boot

我有一个在此类中创建的类,new B(this);BI 希望使用 application.properties 中的值。但是因为(据我所知)因为它不是用 Spring 创建的,所以不会有任何注入(我使用注释@Value

这就是类的创建方式:

@Component
public class A {

    public B getB(){return new B(this);}

    /**
        a lot more stuff
    **/
}
Run Code Online (Sandbox Code Playgroud)

有问题的班级:

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
    **/

}
Run Code Online (Sandbox Code Playgroud)

所以我的问题如下:

1)如何使用 application.properties 文件中的值或

2)B怎么写,它是用Spring创建的?

一些背景信息:

  • 最初的代码不是我写的,所以不想改变太多,但想修改一下,以便将来可以更好地维护
  • 我没有那么多 Spring 知识,只是开始越来越熟悉它。
  • 在第2点)我因为构造函数以及如何通过Spring设置它而苦苦挣扎......

任何帮助,将不胜感激

k9y*_*osh 5

@ConfigurationProperties下面是使用将您的绑定application.properties到 POJO的示例。

假设你有一个application.properties这样的文件,

mail.hostname=mailer@mail.com
mail.port=9000
Run Code Online (Sandbox Code Playgroud)

您可以为上述场景创建一个像这样的 POJO。

注意:如果你的 Spring Boot 版本低于 2.2,你可能还想用@Configuration和注释这个类@Component

@ConfigurationProperties(prefix = "mail")
public class ConfigProperties {

    private String hostname;
    private String port;

    // Create Getters and Setters

}
Run Code Online (Sandbox Code Playgroud)

当 Spring Boot 应用程序初始化时,它会自动将值映射application.properties到此 POJO 中。如果你想使用它,你所要做的就是创建一个变量并将其标记为@Autowire

@Component
public class TestClass {

    @Autowire
    private ConfigProperties properties;

    public void printSomething() {
       System.println(properties.getHostname());
       System.println(properties.getPort());
    }

}
Run Code Online (Sandbox Code Playgroud)

跟进你的问题...

由于示例中的类不是由 spring 管理的,并且由于某种原因您必须保持这种方式,因此您可以使用以下帮助器类在非 spring 托管类中手动自动装配 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);
    }

}
Run Code Online (Sandbox Code Playgroud)

要在您的中使用它,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());
    }

}
Run Code Online (Sandbox Code Playgroud)