从2.0.6更新的Spring Boot 2.1.3导致util:map的BeanDefinitionParsingException

时间:2019-05-03 14:15:18

标签: spring spring-boot

我在应用程序上下文xml中具有以下映射bean定义,并在控制器中使用了映射,这导致BeanDefinitionParsingException导致 spring boot 2.1.3升级。在 2.0.6版本中可以正常使用。

有人知道如何解决此问题吗?

在应用程序属性中定义“ spring.main.allow-bean-definition-overriding=true”不能解决问题。

@SpringBootApplication
@PropertySource("classpath:app.properties")
public class Application extends SpringBootServletInitializer {


    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {// NOSONAR
        SpringApplication.run(Application.class, args);
    }

}

@Configuration
public class ApplicationConfig {

    @Configuration
    @ImportResource("classpath*:applicationContext.xml")    
    public static class XmlImportingConfiguration {
    }

}

app.properties
#Spring Boot
server.contextPath=/myapp
server.servlet.context-path=/myapp
spring.application.name=myapp
server.tomcat.max-threads=200
server.port=901
server.error.whitelabel.enabled=false

logging.level.org.springframework.web: INFO
logging.level.org.springframework: INFO
logging.level.com.wellsfargo: INFO
server.tomcat.accessLogEnabled=false
logging.config=config/log4j2.xml

spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp

applicationContext.xml 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

        <util:map id="lookup">
            <entry key="60" value="1 hour"></entry>
            <entry key="480" value="8 hours"></entry>
            <entry key="1440" value="24 hours"></entry>
            <entry key="2880" value="2 days"></entry>
        </util:map>
</beans>

@Controller
@RequestMapping("/")
public class MyController{
    @Resource(name="lookup")
    private Map<String,String> lookup;

}

错误:

  

org.springframework.beans.factory.parsing.BeanDefinitionParsingException:   配置问题:名称为“ lookup”的无效bean定义   在null中定义:无法注册bean定义[Generic bean:class   [org.springframework.beans.factory.config.MapFactoryBean]; scope =;   abstract = false; lazyInit = false; autowireMode = 0; dependencyCheck = 0;   autowireCandidate = true; primary = false; factoryBeanName = null;   factoryMethodName = null; initMethodName = null; destroyMethodName = null]   对于bean'lookup':已经有[Generic bean:class   [org.springframework.beans.factory.config.MapFactoryBean]; scope =;   abstract = false; lazyInit = false; autowireMode = 0; dependencyCheck = 0;   autowireCandidate = true; primary = false; factoryBeanName = null;   factoryMethodName = null; initMethodName = null; destroyMethodName = null]   绑定。

3 个答案:

答案 0 :(得分:6)

我有解决此问题的方法。基本上将地图从applicationContext.xml移动到应用程序属性,并使用@Value进行检索,如下所示。

app.properties
lookup={'60':'Last 1 hour','480':'Last 8 hours','1440':'Last 24 hours','2880':'Last 2 days'}

ApplicationProperties.java
    @Value("#{${lookup}}")
    private Map<String,String> lookupTimeinterval;

答案 1 :(得分:0)

这可能不是您要查找的内容,但是为什么不用Java Config替换XML。您不应该启用bean覆盖,因为它会使调试Spring IoC启动问题非常非常困难。

@Configuration
public class ApplicationConfig {

    @Bean
    public Map<String, String> lookup() {
      return Map.of(
            "60", "1 hour", 
            "480", "8 hour", 
            "1440", "24 hours", 
            "2880", "2 days"
      );
    }

}

答案 2 :(得分:0)

如果您不想使用“ application.properties”文件,也可以使用“ yaml”文件。

Yaml支持属性中的键值。

application.yaml

myVales:
  60: "1 hour" 
  480: "8 hour" 
  1440: "24 hours" 
  2880: "2 days"

MyConfig.class

@Configuration
@ConfigurationProperties
public class MyConfig {

    private Map<String, String> myValues;

    //getters

}