java - spring boot 从不同的 maven 模块读取属性文件

标签 java spring-mvc properties spring-boot

我的maven项目有3个模块,web, service, common

我项目的某些部分是这样的:

demo-parent:
  --web
    --src
      --main
        --java
          --Application.java
        --resources
          --application.properties
          --application-mysql.properties
   --service
   --common
     --src
       --main
         --java
           --ErrorCode.java
         --resources
           --application-errors.properties

在网络模块 Application.java 中,我想从 common 模块 application-errors.properties 中读取内容。

这是我在 common 中的 ErrorCode.java:

@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties
@ConfigurationProperties(locations = "classpath:application-errors.properties")
public class ErrorCode {
    private int code;
    private String message;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

因为我想在 web 模块中使用这个 application-errors.properties,所以在 web 模块中,在 Application. java:

@SpringBootApplication
@PropertySources({
    @PropertySource("application-mysql.properties"),
    @PropertySource("application-errors.properties")
})
@EnableConfigurationProperties({ErrorCode.class})
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.run(args);
    }
}

现在,我将 EnableConfigurationProperties 添加到包含 main 方法的 Application.java 中,我可以从 application 中获取键和值-errors.properties 文件,我的问题是:

如果我想在服务等其他模块中调用appliction-errors.propertie,而服务没有main方法,我该怎么办?

我们可以使用 java Properties 类读取属性文件,但我想使用 spring 方式

欢迎任何建议,在此先感谢。

最佳答案

无需访问 application-error.properties,您可以在其他模块的服务中自动连接 ErrorCode 类(假设 ErrorCode 存在于这些服务的构建类路径中)。

首先定义错误消息配置组件。

@Configuration
@ConfigurationProperties(locations = "classpath:application-error.properties")
public class ErrorCode {

    private int code;
    private String message;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

在服务中自动连接配置组件

@Component
public class Service {

    @Autowired
    private ErrorCode errorCode;


    public void accessErrorCode() {
        System.out.println(errorCode.getCode()); //Print code property
        System.out.println(errorCode.getMessage()); //print message property
    }

}

在spring应用中启用自动配置

@SpringBootApplication
@EnableAutoConfiguration
@EnableConfigurationProperties
public class Application {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplicationBuilder(Application.class).web(false).application();
        ConfigurableApplicationContext context = application.run(args);
        Service s = context.getBean(Service.class);
        s.accessErrorCode();
    }
}

根据 EnableConfigurationProperties 的 Javadocs,

{@link ConfigurationProperties} beans can be registered in the standard way (for * example using {@link Bean @Bean} methods) or, for convenience, can be specified * directly on this annotation.

因此,所有使用注释 ConfigurationProperties 定义的 beans 都在应用程序上下文中被检测到,并且可以在其他服务中自动连接。

关于java - spring boot 从不同的 maven 模块读取属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33995035/

相关文章:

java - Android 最近的设备按钮监听器

java - 如何使详细主题应用于异常处理生成的错误页面?

java - 我想使用 java 和 angularjs 显示数据列表

java - 从 java 中的包加载 log4j 属性

Springboot无法读取Yaml配置

ios - 在两个 VC 之间传递数据

java - Java中十进制和​​十六进制之间的转换

java - 如何在 Apache Beam 中以 byte[] 形式读取文件?

java - 如果一个对象只有一个实例变量可访问,垃圾收集器会做什么

java - Spring 3,JSR-303(bean 验证)和验证集合