微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么要初始化lazy-init spring bean的占位符?

如何解决为什么要初始化lazy-init spring bean的占位符?

我有一个遗留代码,该代码使用xml配置初始化spring bean。 我定义了一个lazy豆,其ID为“过滤器”,如下所述

<bean id="filter" class="some.package.IdFilter" lazy-init="true">
        <constructor-arg type="java.lang.String" value="${id.start}"/>
</bean>

我有条件逻辑,这是使用Bean初始化的时间

IdFilter filterObj = context.getBean(IdFilter.class);

我假设在不初始化“过滤器” bean时,可能不会提供参数${id.start},因为它仅用于初始化“过滤器” bean。

但是,我看到相反的情况,在加载spring上下文文件时,容器试图找到${id.start}并最终失败。这违反了懒豆的目标

我做错什么了吗?

编辑

添加堆栈跟踪

DEBUG 16:38:13     2597 o.s.c.e.PropertySourcesPropertyResolver [main]- Could not find key 'id.start' in any property source. Returning [null]
WARN 16:38:13     2600 o.s.c.s.AbstractApplicationContext [main]- Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanDeFinitionStoreException: Invalid bean deFinition with name 'filter' defined in class path resource [contextFile.xml]: Could not resolve placeholder 'id.start' in string value "${id.start}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'id.start' in string value "${id.start}"
        at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessproperties(PlaceholderConfigurerSupport.java:211)
        at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processproperties(PropertySourcesPlaceholderConfigurer.java:180)
        at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessbeanfactory(PropertySourcesPlaceholderConfigurer.java:155)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokebeanfactoryPostProcessors(PostProcessorRegistrationDelegate.java:265)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokebeanfactoryPostProcessors(PostProcessorRegistrationDelegate.java:162)
        at org.springframework.context.support.AbstractApplicationContext.invokebeanfactoryPostProcessors(AbstractApplicationContext.java:606)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
        at some.package.util.SpringUtil.buildApplicationContext(springUtil.java:60)
        at some.package.Importerapp.execute(Importerapp.java:50)
        at some.package.Main.main(Main.java:38)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'id.start' in string value "${id.start}"
        at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
        at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
        at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
        at org.springframework.core.env.AbstractPropertyResolver.resolverequiredPlaceholders(AbstractPropertyResolver.java:178)
        at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175)
        at org.springframework.beans.factory.config.BeanDeFinitionVisitor.resolveStringValue(BeanDeFinitionVisitor.java:282)
        at org.springframework.beans.factory.config.BeanDeFinitionVisitor.resolveValue(BeanDeFinitionVisitor.java:204)
        at org.springframework.beans.factory.config.BeanDeFinitionVisitor.visitGenericArgumentValues(BeanDeFinitionVisitor.java:159)
        at org.springframework.beans.factory.config.BeanDeFinitionVisitor.visitBeanDeFinition(BeanDeFinitionVisitor.java:85)
        at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessproperties(PlaceholderConfigurerSupport.java:208)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。