Spring Boot从不同的Maven模块读取属性文件

勤奋

我的Maven项目有三个模块,webservicecommon

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

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

这是我ErrorCode.java的位置common

@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;
    }
}

因为我想application-errors.propertiesweb模块中使用它,所以在模块webApplication.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);
    }
}

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

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

我们可以使用javaProperties读取属性文件,但是我想使用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();
    }
}

根据的Javadocs EnableConfigurationProperties

{@link ConfigurationProperties} bean可以以标准方式注册(例如*使用{@link Bean @Bean}方法),或者为方便起见,可以直接在此批注上指定*。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

是否可以从Maven多模块项目的父模块内的config文件夹中加载Spring-Boot属性?

来自分类Dev

Java-从不同的Maven模块读取文件

来自分类Dev

使用Spring Boot的Maven模块

来自分类Dev

Spring Boot无法从属性文件读取

来自分类Dev

如何使用Spring Boot从Java属性文件读取数据

来自分类Dev

Java:如何从不同的模块读取文件?

来自分类Dev

如何从不同的Maven项目导入Spring资源?

来自分类Dev

如何从不同的Maven项目导入Spring资源?

来自分类Dev

Spring Boot和多模块Maven项目

来自分类Dev

Spring Boot:打包Maven项目多模块

来自分类Dev

在Angular中读取Spring Boot属性

来自分类Dev

Spring Boot配置服务器属性文件在不同的文件夹下?

来自分类Dev

Spring Boot配置服务器属性文件在不同的文件夹下?

来自分类Dev

spring-boot:使用硬编码的文件名配置属性文件的不同版本?

来自分类Dev

使用Spring注释读取文件属性

来自分类Dev

从Spring属性文件中读取值

来自分类Dev

在Java Spring中从文件读取属性

来自分类Dev

Spring读取属性文件,但值为空

来自分类Dev

如何在spring boot中从属性文件中读取

来自分类Dev

Spring MVC Maven模块

来自分类Dev

属性文件中的Java Spring Maven值

来自分类Dev

在Spring Boot中如何同时使用来自不同配置文件的属性?

来自分类Dev

Spring Boot更改值属性文件

来自分类Dev

加载活动属性文件Spring Boot

来自分类Dev

Spring Boot外部属性文件更新

来自分类Dev

在Spring Boot中访问属性文件

来自分类Dev

Spring boot 加载多个属性文件

来自分类Dev

具有不同Maven模块的Spring资源解析器

来自分类Dev

具有不同Maven模块的Spring资源解析器

Related 相关文章

热门标签

归档