无法使用enableconfigurationproperties

时间:2018-07-23 13:26:27

标签: java spring spring-boot

@SpringBootApplication
@EnableConfigurationProperties({GlobalProperties.class})
public class Application {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(Application.class, args);
        GlobalProperties globalProperties = context.getBean(GlobalProperties.class);
        System.out.println(globalProperties);
    }
}

@PropertySource("classpath:global.properties")
@ConfigurationProperties("app")
public class GlobalProperties {

    private String error;
    private List<Menu> menus = new ArrayList<>();
    private Compiler compiler = new Compiler();

//getters and setters and tostring

    public static class Menu {
        private String name;
        private String path;
        private String title;

    //getters and setters and tostring

    }

    public static class Compiler {
        private String timeout;
        private String outputFolder;

    //getters and setters and tostring
    }


    @Override
    public String toString() {
        return "GlobalProperties [error=" + error + ", menus=" + menus + ", compiler=" + compiler + "]";
    }


}

global.properties位于src / main / resources

#Logging
logging.level.org.springframework.web=ERROR
logging.level.com.mkyong=DEBUG

#Global
email=test@mkyong.com
thread-pool=10

#App
app.menus[0].title=Home
app.menus[0].name=Home
app.menus[0].path=/
app.menus[1].title=Login
app.menus[1].name=Login
app.menus[1].path=/login

app.compiler.timeout=5
app.compiler.output-folder=/temp/

app.error=/error/

这是我运行上述应用程序时的结果

GlobalProperties [error=null, menus=[], compiler=Compiler{timeout='null', outputFolder='null'}]

但是,如果我在@EnableConfigurationProperties({GlobalProperties.class})类上注释掉Application注释并在@Component上添加GlobalProperties注释,我将得到预期的结果

GlobalProperties [error=/error/, menus=[Menu{name='Home', path='/', title='Home'}, Menu{name='Login', path='/login', title='Login'}], compiler=Compiler{timeout='5', outputFolder='/temp/'}]

为什么我不能在这里使用enableconfigurationproperty注释?

3 个答案:

答案 0 :(得分:1)

这应该有效

@PropertySource("classpath:global.properties")
@Configuration
@ConfigurationProperties("app")
public class GlobalProperties {

  ......

}

您必须结合使用@ConfigurationProperties@Configuration@Component

为什么?

很简单。除非您具有以下批注之一(@Configuration@Component@Service ...等),否则它是常规的Java类,而不是spring bean,因此spring将在扫描期间不扫描此类启动。也就是说,它无法注入属性。

答案 1 :(得分:0)

如果没有任何@PropertySource@Component注释,

@Configuration注释将不起作用。您必须正确注册您的属性文件。您可以这样做:

@SpringBootApplication
@PropertySource("classpath:global.properties")
@EnableConfigurationProperties({GlobalProperties.class})
public class Application {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(Application.class, args);
        GlobalProperties globalProperties = context.getBean(GlobalProperties.class);
        System.out.println(globalProperties);
    }
}

或者您可以将相关属性移动到application.properties文件,该文件默认是在春季启动中配置的。

答案 2 :(得分:0)

GlobalProperties是您定义POJO的方式,而不是Spring Bean。

用@Component或@Configuration注释该类将使该类成为Spring Bean,您将能够从Spring Context中检索它。

要了解如何使用@EnableConfigurationProperties,我建议您阅读以下文章:https://spring.io/blog/2013/10/30/empowering-your-apps-with-spring-boot-s-property-support