首页 > 解决方案 > 自定义属性文件在较低的项目层中读取为实体

问题描述

我想使用我的自定义属性类。我的项目 hiearcy 像这样,每个都是不同的项目。

我可以在 mycontroller 项目中使用带有 Autowired 注释的 ConfigurationProperties,但是当我在我的 dao 项目中尝试它时,自动装配的 myproperties 为空。

-MyController-->Autowired myproperties ok
-MyService

-MyDAO-->Autowired myproperties 为空

 @Component 
//@Configuration
 @PropertySource("classpath:my.properties")
@ConfigurationProperties public class MyProperties {

我想在我的底层项目中使用我的自定义属性文件作为对象。

在我的 dao 层中,我可以读取自定义属性文件,例如键值,但我想将其作为实体读取。

  prop = new Properties();
            String filename = "my.properties";
            input = MyProperties.class.getClassLoader().getResourceAsStream(filename);
prop.load(input);

        Enumeration<?> e = prop.propertyNames();
        while (e.hasMoreElements()) {....

标签: javaspringspring-bootproperties-file

解决方案


推荐阅读