gpt4 book ai didi

Spring boot 2.1.3 从 2.0.6 更新导致 BeanDefinitionParsingException 用于 util :map

转载 作者:IT老高 更新时间:2023-10-28 13:50:24 28 4
gpt4 key购买 nike

我在应用程序上下文 xml 中有以下映射 bean 定义,并在 Controller 中使用了导致 spring boot 2.1.3 升级BeanDefinitionParsingException 的映射。它在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: 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.

最佳答案

我有解决这个问题的方法。基本上将 map 从 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;

关于Spring boot 2.1.3 从 2.0.6 更新导致 BeanDefinitionParsingException 用于 util :map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55971630/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com