Spring Boot 2无法将属性读取为字符串

时间:2018-12-18 01:51:25

标签: java spring spring-boot

将Spring Boot应用程序从Spring Boot 1.4.0迁移到Spring Boot 2时,在尝试从.properties文件读取属性时开始出现错误。

在属性文件中,属性定义为:

environment=dev

在我的一个课程中,我通过@Value注释导入属性,如下所示:

@Getter
@Setter
public class CustomUserFilter extends SwitchUserFilter {
    ...
    @Value("${environment}")
    private String environment;
    ...

上面的类重写org.springframework.security.web.authentication.switchuser.SwitchUserFilter,以使用户能够切换角色。

在Spring Boot 1.4.0之前,我可以将这个属性作为String导入我的课程中。但是,自从迁移到Spring Boot 2之后,我得到了以下编译时错误:

  

错误:(43,20)java:d​​emo.config.CustomUserFilter中的getEnvironment()   无法在中实现getEnvironment()   org.springframework.core.env.EnvironmentCapable返回类型   java.lang.String与不兼容   org.springframework.core.env.Environment

我不确定为什么会这样。我还尝试将此变量的类型更改为Environment (org.springframework.core.env.Environment)(如下所示):

...
@Value("${environment}")
private Environment environment;
...

,但随后我开始出现以下错误:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'switchUserFilter': Unsatisfied dependency expressed through field 'environment'; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.core.env.Environment'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.springframework.core.env.Environment': no matching editors or conversion strategy found
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:370)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1336)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:226)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:182)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:177)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAdaptableBeans(ServletContextInitializerBeans.java:159)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:81)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getServletContextInitializerBeans(ServletWebServerApplicationContext.java:261)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.selfInitialize(ServletWebServerApplicationContext.java:234)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:185)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:154)
    ... 52 more
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.core.env.Environment'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.springframework.core.env.Environment': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:77)
    at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:60)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1089)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:581)
    ... 70 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.springframework.core.env.Environment': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:299)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:117)
    at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:70)
    ... 74 more

关于可能出什么问题的任何线索?我正在使用 Spring Boot 2.0 Spring 5 Java 11 Tomcat 8.5.35 。谢谢!

2 个答案:

答案 0 :(得分:3)

问题与Spring Boot无关。我上面的课程扩展了org.springframework.security.web.authentication.switchuser.SwitchUserFilter,并进一步扩展了org.springframework.web.filter.GenericFilterBean。该GenericFilterBean类还具有属性private Environment environment;。此外,该类在Spring 5版本中进行了修改,以在此属性上包含getter方法。春季5在类getEnvironment()中引入了一个返回类型为Environment的{​​{1}},它与我的返回类型为GenericFilterBean的{​​{1}}冲突。

为避免发生这种冲突,我只是将属性名称从getEnvironment()更改为String,然后事情就解决了。

答案 1 :(得分:0)

将@Component添加到您的班级并尝试