java - 在非 spring 注入(inject)类中使用 application.properties

标签 java spring-boot javabeans properties-file

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

这就是类的创建方式:

@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 设置它而陷入困境......

如有任何帮助,我们将不胜感激

最佳答案

以下是使用 @ConfigurationPropertiesapplication.properties 绑定(bind)到 POJO 的示例。

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

mail.hostname=mailer@mail.com
mail.port=9000

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

(注意:如果您的 Spring Boot 版本低于 2.2,您可能需要使用 @Configuration@Component 注释此类好吧)

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

    private String hostname;
    private String port;

    // Create Getters and Setters

}

当 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());
    }

}

正在跟进您的问题...

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

}

要在您的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());
    }

}

关于java - 在非 spring 注入(inject)类中使用 application.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59197165/

相关文章:

java - Spring Jpa 规范 用 like 连接表

java - JSP 中的表单 - 在每个帖子旁边添加日期

spring - Bean 属性 'xxx' 不可写或具有无效的 setter 方法

java - 使用 MockMvc 在 Spring MVC 中进行单元测试/登录

java - 无法使用 StoredProcedureItemReader 读取 REF_CURSOR

java - (spring boot) jar 无法在其他计算机上执行

mysql - 我将如何托管多个与 Heroku 共享相同 Mysql 数据库的 Spring Boot Web 应用程序?

java - 在jsf中使用不同bean的策略

java - 如何在JBoss AS中登录 'activate' SLF4J

java - FileInputStream.read 抛出 java.io.IOException : Result too large