当前位置: 首页 > 知识库问答 >
问题:

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

柯建业
2023-03-14

在将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;
    ...
...
@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 Boot2.0、Spring5、Java11和Tomcat8.5.35。谢了!

共有1个答案

奚翰海
2023-03-14

这个问题与Spring靴无关。上面的类扩展了org.springframework.security.web.authentication.switchuser.switchuserfilter,它进一步扩展了org.springframework.web.filter.genericfilterbean。这个GenericFilterBean类还有一个属性Private Environment Environment;。此外,这个类在Spring5版本中进行了修改,在这个属性上包含了一个getter方法。Spring 5在类GenericFilterBean中引入了返回类型EnvironmentGetEnvironment(),这与返回类型String冲突。

为了避免这种冲突,我简单地将属性的名称从environment更改为env,在此之后就可以工作了。

 类似资料:
  • 我是新来的node.js.我正在尝试创建一个注册页面,并使用mysql将输入的值保存在数据库中。 我可以通过在请求头上传递参数来实现这一点。但是,在请求头中传递参数始终不是一个好主意。所以尝试在请求体上传递参数。 我使用邮差测试我的node.js服务。 下面是我的演示应用程序的github链接 https://github.com/debasish283/node_gulp 我还需要做什么来传递请

  • 问题内容: 删除并重新安装node_modules文件夹后,我面临的是LayoutPropTypes.js文件中我不理解的问题。 在 以下变量未定义中: 反应本机:0.45.1反应:16.0.0-alpha.12 问题答案: React.PropTypes现在已被弃用: 注意:从React v15.5开始不推荐使用React.PropTypes。请改用prop-types库。 您现在需要单独添加p

  • 问题内容: 我是React的新手,正在使用React,Redux和React Router构建我的第一个应用程序,到目前为止,我已经成功设置了样板,但是当我想使用React-Router并创建路由时,出现以下错误 这仅在我尝试导入和使用时发生 这是我的index.js 还有我的package.json 任何知识都将受到欢迎,谢谢! 问题答案: 尝试将PropTypes包添加到您的应用中。 看来您只

  • 问题内容: 我正在尝试从无头CMS(Sanity.io)返回一些数据,但是我的React一直遇到错误。我已遵循此示例,但是现在出现错误 我的密码 关于我在做什么错的任何想法吗? 问题答案: 渲染组件时,可以将country定义为未定义或为null。确保它在初始渲染时是数组: 或者, 您可以将地图用于这样的国家/地区: 这样可以确保仅在有国家/地区时才运行地图。

  • 问题内容: 我是ReactJS的新手,遇到一些问题。 我还了解了语法,它说以下代码具有相同的含义。 1。 2。 但是,第一种方法引发此错误 问题答案: YTSearch({key: API_KEY, term: ‘nba’}, function(videos) { this.setState({ videos }); }); 引发错误,因为在此回调内部并未引用函数本身的上下文,因此此处未定义。 在

  • 我试图在Springboot应用程序中使用logback-spring.xml创建日志记录功能,但是无法读取logback-spring.xml文件中的属性值(例如:log.dest.path)。 我通过@PropertySource基于概要文件动态加载不同环境(开发、阶段、生产)的属性文件(YAML)。概要分析工作正常,并且加载了正确的YAML文件(例如:- application.dev.ym