Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot 2.1.3 update from 2.0.6 causes an BeanDefinitionParsingException for util:map

I have below map bean definition in application context xml and used the map in controller which is causing BeanDefinitionParsingException for spring boot 2.1.3 upgrade. It works fine in 2.0.6 version.

Does anyone know how to resolve this issue?

Defining "spring.main.allow-bean-definition-overriding=true" in application properties doesn't fix the issue.

@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;

}

Error:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Invalid bean definition with name 'lookup' defined in null: Cannot register bean definition [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] for bean 'lookup': There is already [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] bound.

like image 603
skumar Avatar asked May 03 '19 14:05

skumar


1 Answers

I have workaround for this problem. Basically moved the map from applicationContext.xml to application properties and retrieved using @Value like below.

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;
like image 187
skumar Avatar answered Nov 17 '22 10:11

skumar