Springboot 不替换 application.properties 文件中的环境变量 [英] Springboot not replacing environment variables in application.properties file

查看:62
本文介绍了Springboot 不替换 application.properties 文件中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SpringBoot 运行 Quartz Scheduler.使用 Quartz Jdbc 数据存储.出于安全原因,我们希望从属性文件中选择 Db 凭据.从我从这里了解到的(在 Spring Boot 的 application.properties 中使用 env 变量) 和 springBoot 文档,SpringBoot 会自动替换 application.properties 中的环境变量,但我没有看到这个.这是我在运行应用程序之前获取的系统环境文件

I am trying to run Quartz Scheduler using SpringBoot. Using Quartz Jdbc Data Store. Due to security reasons , we wish to pick the Db credentials from the properties file. From what I understand from here(Using env variable in Spring Boot's application.properties) and springBoot docs, SpringBoot automatically replaces environment variables in application.properties but I am not seeing this . Here is my system environment file which i am sourcing before running the application

export DB_HOST=localhost
export DB_PORT=11111
export DB_USER=root
export DB_PASSWORD=root
export QUARTZ_DB_NAME=quartz

这是我的 application.properties

And here is my application.properties

org.quartz.dataSource.quartzDataSource.URL =jdbc:mysql://${DB_HOST}:${DB_PORT}/${QUARTZ_DB_NAME}
org.quartz.dataSource.quartzDataSource.user = ${DB_USER}
org.quartz.dataSource.quartzDataSource.password = ${DB_PASSWORD}

还有我的配置类

@Configuration
public class ConfigureQuartz {

@Autowired
private ApplicationContext applicationContext;

@Bean
public SchedulerFactoryBean schedulerFactoryBean() throws IOException
{
    final SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();
    quartzScheduler.setSchedulerName("mdsScheduler");

    quartzScheduler.setQuartzProperties(quartzProperties());
    final AutoWiringSpringBeanJobFactory jobFactory = new AutoWiringSpringBeanJobFactory();
    jobFactory.setApplicationContext(applicationContext);
    quartzScheduler.setJobFactory(jobFactory);
    return quartzScheduler;
}



 @Bean
 public Properties quartzProperties() throws IOException {
  final PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
  propertiesFactoryBean.setLocation(new ClassPathResource("/application.properties"));
  propertiesFactoryBean.afterPropertiesSet();
  return propertiesFactoryBean.getObject();

}

但是当我使用 java -jar <>.java 运行我的 spring 应用程序时,我没有看到被替换的值.

But when i run my spring application using java -jar <>.java , I dont see the values substitued .

我可以通过使用 System.getEnv() 读取值来解决此问题,但如果可以替换这些值,那就太好了.不知道为什么它不起作用:(

I can workaround by reading the values using System.getEnv() but would be great if the values can be substitued . Not sure why its not working :(

推荐答案

Spring-boot 允许我们提供多种方法来提供外部化配置,您可以尝试使用 application.yml 或 yaml 文件代替属性文件并根据不同的环境提供不同的属性文件设置.我们可以将每个环境的属性分离到单独的spring配置文件下的单独的yml文件中.然后在部署时可以使用:

Spring-boot allows us several methods to provide externalized configurations , you can try using application.yml or yaml files instead of the property file and provide different property files setup according to different environments.We can separate out the properties for each environment into separate yml files under separate spring profiles.Then during deployment you can use :

java -jar -Drun.profiles=SpringProfileName

指定要使用的弹簧配置文件.注意yml文件的名称应该像

to specify which spring profile to use.Note that the yml files should be name like

application-{environmentName}.yml

让它们被 springboot 自动占用.参考:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-个人资料特定属性

for them to be automatically taken up by springboot. Reference : https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties

从 application.yml 或属性文件中读取:

从属性文件或yml中读取一个值最简单的方法是使用spring的@value注解.Spring会自动将yml中的所有值加载到spring环境中,所以我们可以直接使用来自环境的那些值,例如:

The easiest way to read a value from the property file or yml is to use the spring @value annotation.Spring automatically loads all values from the yml to the spring environment , so we can directly use those values from the environment like :

@Component
public class MyBean {

    @Value("${name}")
    private String name;

    // ...

}

或者spring提供的另一种读取强类型bean的方法如下:

Or another method that spring provides to read strongly typed beans is as follows:

YML

acme:
    remote-address: 192.168.1.1
    security:
        username: admin
        roles:
          - USER
          - ADMIN

对应的POJO读取yml:

Corresponding POJO to read the yml :

@ConfigurationProperties("acme")
public class AcmeProperties {

    private boolean enabled;

    private InetAddress remoteAddress;

    private final Security security = new Security();

    public boolean isEnabled() { ... }

    public void setEnabled(boolean enabled) { ... }

    public InetAddress getRemoteAddress() { ... }

    public void setRemoteAddress(InetAddress remoteAddress) { ... }

    public Security getSecurity() { ... }

    public static class Security {

        private String username;

        private String password;

        private List<String> roles = new ArrayList<>(Collections.singleton("USER"));

        public String getUsername() { ... }

        public void setUsername(String username) { ... }

        public String getPassword() { ... }

        public void setPassword(String password) { ... }

        public List<String> getRoles() { ... }

        public void setRoles(List<String> roles) { ... }

    }
}

上述方法适用于 yml 文件.

The above method works well with yml files.

参考:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

从环境中读取:

如果您在 linux 上运行该应用程序,请将 env 设置如下:

If you are running the application on linux set the env as below :

export DB_HOST=localhost
export DB_PORT=11111
export DB_USER=root
export DB_PASSWORD=root
export QUARTZ_DB_NAME=quartz

如果你在 Windows 上设置如下:

if you are on windows set it like :

SET DB_HOST=localhost
SET DB_PORT=11111
SET DB_USER=root
SET DB_PASSWORD=root
SET QUARTZ_DB_NAME=quartz

在您的 application.properties 中,如果您保留如下键,它应该会自动从环境中解析该值:

In your application.properties if you keep the keys as below , it should automatically resolve the value from the environment :

spring.datasource.url = ${DB_HOST}/"nameofDB"
spring.datasource.username = ${DB_USER}
spring.datasource.password = ${DB_PASSWORD}

这篇关于Springboot 不替换 application.properties 文件中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆