在自定义启动器中访问Spring应用程序名称

时间:2018-11-13 10:54:28

标签: java spring spring-boot

我正在尝试通过自定义启动程序自动配置访问spring应用程序名称。

header('Location: indexm.php');

spring.factories as

@Configuration
public class CustomAutoConfiguration {

    @Value("${spring.application.name}")
    private String appName;

}

在使用此自定义启动程序的应用程序中,我已经在org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ co.test.CustomAutoConfiguration

中定义了应用程序名称。
bootstrap.yaml

但是,我看到appName是spring: application: name: test-app 。我的猜测与加载顺序有关吗?无论如何要实现这一目标?

4 个答案:

答案 0 :(得分:1)

我过去也遇到过类似的问题,因此我通过自动装配org.springframework.core.env.Environment来解决了;像这样的东西:

@Configuration
public class CustomAutoConfiguration {
    @Autowired
    private Evinronment env;
    private String appName;
    @PostConstruct
    public void initialize(){
       this.appName = env.getProperty("spring.application.name");
    }

}

未经测试,但可以正常工作

天使

答案 1 :(得分:1)

这才是最终的结果。

@Configuration
public class CustomAutoConfiguration implements EnvironmentAware {

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    // And then accessing via this.environment.getProperty("spring.application.name")

}

答案 2 :(得分:0)

可能与bootstrap.yml的加载顺序有关。我们在@Value类中一直无休止地@Configuration,但是我们使用application.properties。您是否尝试过在那里设置?还是在命令行上?

答案 3 :(得分:-1)

相信,您的配置类需要使用-2 bytes for count of columns in the record -1 byte to store one bit per column in the record 注释的私有字段的getter和setters,以便Spring能够设置该字段!