2016-10-17 83 views
0

我有一个myapp.properties文件,定义为键值对:自定义类型转换值

prefix.int-field=123 
prefix.string-field=asdf 
prefix.custom-type-field=my.package.CustomType 

我想通过在下面的类使用@Value注释注入这些属性:

@PropertySource(value = "classpath:myapp.properties") 
@Component 
class MySettings { 
    @Value("${prefix.int-field}") 
    private int intField; 

    @Value("${prefix.string-field}") 
    private String stringField; 

    @Value("${prefix.custom-type-field}") // <-- this is the problem 
    private CustomInterface customField; 
} 

class CustomType implements CustomInterface {...} 

interface CustomInterface {...} 

现在,intFieldstringField与预期的期望值得到初始化,但customField抛出异常:

Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [my.package.CustomInterface]: no matching editors or conversion strategy found 
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:303) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:125) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
    at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:61) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 

如何将文本属性值转换为我的自定义类型?

我试着去咨询documentation,但是我看不到正确的做法。我正在使用Spring Boot 1.3.6。

+1

您是否尝试添加自定义转换器?它应该是一个类实现Converter ,更多详细信息http://stackoverflow.com/questions/34239585/how-to-register-custom-converters-in-spring-boot – freakman

+0

为什么你不只是注入它像一颗豆子? –

+0

@freakman我还没有尝试过,我是新来的春天和困惑的事情。我会给它一个 – radoh

回答

1

要解决您的直接问题,您需要查看bean上的@PostConstruct选项。这将允许您在bean可用于上下文之前对事情采取行动。

@PropertySource(value = "classpath:myapp.properties") 
@Component 
class MySettings { 
    @Value("${prefix.int-field}") 
    private int intField; 

    @Value("${prefix.string-field}") 
    private String stringField; 

    @Value("${prefix.custom-type-field}") 
    private String customFieldType; 

    private CustomInterface customField; 

    @PostConstruct 
    public void init() { 
     customField = (CustomInterface) Class.forName(customFieldType).newInstance(); // short form... will need checks that it finds the class and can create a new instance 
    } 
} 

class CustomType implements CustomInterface {...} 

interface CustomInterface {...} 

我很好奇,如果你可能想使用一个类的@Configuration注释和创建一个提供上Spring的ApplicationContext一个bean CustomInterface的一个实例。要做到这一点,你反而会做这样的事情:

@Component 
@ConfigurationProperties(prefix = "prefix") 
class MySettings { 
    private int intField; 

    private String stringField; 

    private String customTypeField; 

    // getters and setters 
} 

这将随后在@Configuration类中使用:

@Configuration 
class MyConfiguration { 
    @Bean 
    public CustomInterface customInterface(MySettings mySettings) { 
     return (CustomInterface) Class.forName(mySettings.getCustomTypeField()).newInstance(); 
    } 
} 

在这一点上,你现在将有一个实例化的bean的CustomInterface,你可以将Spring自动装入其他对象。

+0

感谢您的回答。我已经尝试了你建议的两种解决方案,他们都很好地工作。 (尽管第一个更详细,它提供了对'$ {...}'值字符串进行ctrl + click(intellij想法)的能力,该字符串可以直接转到'.properties'文件中的属性。接受你的答案,虽然我可能不会最终使用它,因为我的自定义类型可以有构造函数参数,所以我可能必须完全不同。 – radoh

相关问题