简体   繁体   English

属性和env始终为null

[英]Properties and env always null

I have a hard time loading properties from my properties file. 我很难从属性文件中加载属性。 I'm trying to load a property into my configuration class which is listed below. 我正在尝试将属性加载到我的配置类中,如下所示。

package dk.fitfit.budget.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;


@Configuration
@PropertySource("classpath:application.properties")
public class ApplicationConfig {
    private static final Logger logger = LoggerFactory.getLogger(ApplicationConfig.class);

    @Autowired
    private Environment env;

    @Value("${snot:test}")
    private String snot;

    public ApplicationConfig() {
        logger.info("Application config loaded!");  // Displays as expected
        logger.info("snot: {}", snot);              // snot: null
        logger.info("env: {}", env);                // env: null
    }

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

}

If I change the name of the file (application.properties) or "@PropertySource("classpath:application.properties")" I get an error about the file not being present. 如果我更改文件的名称(application.properties)或“@PropertySource(”classpath:application.properties“)”我收到有关文件不存在的错误。 So clearly it's being loaded (to some extend anyway). 很明显它正在加载(无论如何)。

I was at least expecting to see the default string "test" injected into the variable snot. 我至少期望看到注入变量snot的默认字符串“test”。 But not even that is done. 但即便如此。 I'm not able to autowire Environment either... not sure if there is a relation. 我无法自动装配环境......不确定是否存在关系。

The content of my application.properties is as below. 我的application.properties的内容如下。

snot=snog

My application.properties file is placed in src/main/resources/. 我的application.properties文件放在src / main / resources /中。

Anyone got a clue about what I'm doing wrong? 任何人都知道我做错了什么?

The purpose of a constructor is to initialize a class instance. 构造函数的目的是初始化一个类实例。 You have to construct and initialize the class instance before Spring can do anything to it. 在Spring可以对它做任何事情之前,你必须构造并初始化类实例。 As such, you cannot expect the env and snot fields to have anything other than the default null value within the constructor. 因此,您不能指望envsnot字段在构造函数中具有除默认null值之外的任何内容。

Spring (and all dependency-injection tools) can't inject fields before the instance is created with new , which is one reason why constructor injection should be preferred to field injection. Spring(以及所有依赖注入工具)在使用new创建实例之前无法注入字段,这是构造函数注入应优先于字段注入的一个原因。 Generally, you can use @Value on constructor parameters, but there are certain issues with @Configuration classes that make constructor injection impractical. 通常,您可以在构造函数参数上使用@Value ,但@Configuration类存在某些问题会导致构造函数注入不切实际。

However, there's a solution meant for exactly this situation: @PostConstruct . 但是,有一个解决方案适用于这种情况: @PostConstruct This method will be called after the DI framework has done all its magic to the bean: 在DI框架完成对bean的所有魔术之后,将调用此方法:

@PostConstruct
public void log() {
    logger.info("Application config loaded!");  // Displays as expected
    logger.info("snot: {}", snot);              // snot: null
    logger.info("env: {}", env);                // env: null
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM