Spring引导环境变量没有正确反映

时间:2018-03-02 18:55:42

标签: java spring spring-mvc spring-boot spring-profiles

我正在开发spring boot应用程序,我希望通过配置服务器动态地完全外部化我的环境变量。

以下是我写的代码。

Application.java

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const morgan = require('morgan');
const sqlite = require('sqlite3');
const db = new sqlite.Database('./issues.sqlite');
const dbname = 'issueInventory';
const PORT = process.env.PORT || 4001

app.use(bodyParser.json());
app.use(morgan('dev'));


app.get('/', (req,res,next) => {
  db.all(`select * from $dbname;`,
    {
      $dbname: dbname
  },
  (err,rows) =>{
    if (err) {
      res.status(500).send(`Something went wrong, we are working on it`)
    }
    res.status(200).send({issues: rows})
  });
});


app.listen(PORT, () => {
  console.log(`Application Server listening on ${PORT}`)
});

module.exports = app;

application.properties

package com.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("com.myPackage, com.util")
@PropertySource("classpath:application.properties")
public class Application extends SpringBootServletInitializer {

    static final Logger logger = LoggerFactory.getLogger(Application.class);
    public static ApplicationContext ctx;

    public static void main(String[] args) throws Exception {
        logger.info("Application starting....");
        ctx = SpringApplication.run(Application.class, args);
        logger.info("Application started successfully....");

    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer
    propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

配置服务器:

server.port=${server.port}    
ENDPOINT_SHAN=${name.mya}

读取属性

APPLICATION_NAME=myapp
server.port=8081
name.mya=myName

所以,当我尝试获取属性 import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; import com.apptium.Application; @Component("configurationProperties") public class ConfigurationProperties { private static Environment prop; public static String getConfigValue(String key) { String value = null; if (prop == null) { prop = Application.ctx.getBean(Environment.class); } if (prop.getProperty(key) != null) { value = prop.getProperty(key).trim(); } if (value == null && System.getenv(key) !=null) { value = System.getenv(key).trim(); } return value; } } 时,它正在返回 ENDPOINT_SHAN但需要返回ENDPOINT_SHAN==${name.mya}

还想知道如何正确使用server.port的属性。

无论如何,我想知道如何获得ENDPOINT_SHAN==myName的实际属性。

1 个答案:

答案 0 :(得分:0)

您说name.mya是在“配置服务器”中定义的,但您的代码未显示与此服务器建立的任何连接。您需要在${name.mya}可解析之前从服务器读取它。