为什么我的Spring PropertySource注释在编码属性上失败?

时间:2018-10-25 23:50:42

标签: java spring spring-mvc

我正在开发一个新的Spring Web应用程序,并且遵循的模式在早期Web应用程序中效果很好。我有一个Web应用程序配置类:

@Configuration
@EnableWebMvc
@EnableJpaRepositories
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.example" })
@PropertySource(value = "classpath:application.properties")
public class WebApplicationConfiguration extends WebMvcConfigurerAdapter { ... }

启动Web应用程序(在Tomcat下)时,出现以下错误:

[RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Initializing Spring root WebApplicationContext
[RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class 
[org.springframework.web.context.ContextLoaderListener] org.springframework.beans.factory.BeanDefinitionStoreException:
    Failed to parse configuration class [com.example.WebApplicationConfiguration];
    nested exception is java.lang.IllegalArgumentException:
    Attribute 'encoding' not found in attributes for annotation [org.springframework.context.annotation.PropertySource]
    at org.springframework.context.annotation.ConfigurationClassParser.parse (ConfigurationClassParser.java:187)
...
Caused by: java.lang.IllegalArgumentException:
    Attribute 'encoding' not found in attributes for annotation
    [org.springframework.context.annotation.PropertySource]

在较早的Web应用程序中,我不需要在encoding注释上指定@PropertySource属性,因此我不确定为什么现在会出现错误。无论如何,我尝试了以下方法:

@PropertySource(value = "classpath:application.properties", encoding = "UTF-8")

...这没有区别,即。我遇到了同样的错误。这向我表明,根本原因可能不是@PropertySource注释。关于我可能做错了什么的任何建议

1 个答案:

答案 0 :(得分:2)

这样的错误通常是混合来自不同版本的Spring的jar的信号。在这种情况下,@PropertySource注释的注释处理器希望encoding属性存在。

但是在运行时它不存在,因此会产生错误。

您要检查(传递)依赖项中是否存在冲突的版本(或未明确管理的版本)。

相关问题