为什么要初始化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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...