代码之家  ›  专栏  ›  技术社区  ›  TheJeff

弹簧5.0.5@值未解析为正确的类型

  •  1
  • TheJeff  · 技术社区  · 7 年前

    我在spring 5中遇到了以下错误,并且阅读了所有关于这个问题的帖子,但都没有发现运气。我只是通过PropertiesConfig类重构了应用程序以使用@配置,而不是appliationContext。属性占位符定义的xml

    Unsatisfied dependency expressed through field 'secureExpirationHours'; nested 
    exception is org.springframework.beans.TypeMismatchException: Failed to convert 
    value of type 'java.lang.String' to required type 'int'; nested exception is 
    java.lang.NumberFormatException: For input string: "${asset.href.expiration.hours.secure}
    

    参考var:

    public class TestRepository {
        @Value("${asset.href.expiration.hours.secure}")
        private int secureExpirationHours;
    }
    

    混合配置:

    @Configuration
    @ComponentScan
    @Import({PropertiesConfig.class})
    @ImportResource({
        "classpath:META-INF/spring/applicationContext-assets.xml",
        "classpath:META-INF/spring/applicationContext-mongo.xml",
        "classpath:META-INF/spring/applicationContext-security.xml",
        "classpath:META-INF/spring/applicationContext.xml"})
    public class CoreConfig {
    }
    

    属性配置。类别:

    @Configuration
    public class PropertiesConfig {
    
        public static PropertySourcesPlaceholderConfigurer commonEnvConfig;
    
        @Bean(name="commonConfig")
        public static PropertiesFactoryBean commonConfig() {
            PropertiesFactoryBean commonConfig = new PropertiesFactoryBean();
            commonConfig.setLocation(new ClassPathResource("META-INF/spring/config.properties"));
            return commonConfig;
        }
    
        @Bean(name="envProperties")
        public static YamlPropertiesFactoryBean yamlProperties() {
              YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
              yaml.setResources(new ClassPathResource("application-dev.yaml"));
              return yaml;
        }
    
        @Bean(name="commonConfigPropertyPlaceholder")
        public static PropertySourcesPlaceholderConfigurer commonConfigPropertyPlaceholder() throws IOException {
            if (commonEnvConfig == null) {
                commonEnvConfig = new PropertySourcesPlaceholderConfigurer();
            }
    
            PropertiesFactoryBean commonConfig = PropertiesConfig.commonConfig();
            try {
                commonEnvConfig.setProperties(commonConfig.getObject());
            } catch (IOException e) {
                e.printStackTrace();
                throw e;
            }
    
            YamlPropertiesFactoryBean yaml = PropertiesConfig.yamlProperties();
            commonEnvConfig.setProperties(yaml.getObject());
            commonEnvConfig.setIgnoreUnresolvablePlaceholders(true);
    
            return commonEnvConfig;
        }
    }
    

    请并感谢您的帮助!

    1 回复  |  直到 7 年前
        1
  •  0
  •   TheJeff    7 年前

    解决了问题-有两个。

    首先,你必须打电话。AfterPropertieSet();在PropertiesFactoryBean上,否则。getObject()返回null。

    @Bean(name="commonConfig")
    public static PropertiesFactoryBean commonConfig() throws IOException {
        PropertiesFactoryBean commonConfig = new PropertiesFactoryBean();
        commonConfig.setLocation(new ClassPathResource("META-INF/spring/config.properties"));
        try {
            commonConfig.afterPropertiesSet();
        }
        catch (IOException e) {
            e.printStackTrace();
            throw e;
        }
        return commonConfig;
    }
    

    其次,必须使用以下两个属性调用PropertySourcesPlaceholderConfigurer上的“setPropertiesArray()”:

        PropertySourcesPlaceholderConfigurer commonEnvConfig = new PropertySourcesPlaceholderConfigurer();
        commonEnvConfig.setPropertiesArray(commonConfig().getObject(), yamlProperties().getObject());
    
    推荐文章