ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-11-08 11:19:15  阅读:190  来源: 互联网

标签:spring-boot spring java


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

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

环境= 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之后,我得到了以下编译时错误:

Error:(43, 20) java: getEnvironment() in demo.config.CustomUserFilter
cannot implement getEnvironment() in
org.springframework.core.env.EnvironmentCapable return type
java.lang.String is not compatible with
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.谢谢!

解决方法:

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

为了避免这种冲突,我只是将属性的名称从环境更改为env,然后事情就起作用了.

标签:spring-boot,spring,java
来源: https://codeday.me/bug/20191108/2007358.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有