我有两个豆。我想OtherBean
取决于SomeBean
。如果我写的是显式depends-on="someBean"
,那就行了。但是我也想从配置文件depends-on="${depends.bean}"
中读取bean名称,但是它不起作用。
线程“主”中的异常 org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有定义名为“ $ {depends.bean}”的bean
属性是在构造函数中读取的,而不是在depends-on
属性中读取的。
我的示例项目:
SomeBean.java
:
package my.package;
public class SomeBean {
public SomeBean(String example) {
System.out.println("Example some: " + example);
}
}
OtherBean.java
:
package my.package;
public class OtherBean {
public OtherBean(String example) {
System.out.println("Example other: " + example);
}
}
application-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="classpath:configuration.properties" />
<bean id="someBean" class="my.package.SomeBean">
<constructor-arg value="${example.some}" />
</bean>
<bean id="otherBean" class="my.package.OtherBean"
depends-on="${depends.bean}">
<constructor-arg value="${example.other}" />
</bean>
</beans>
Runner类StartApp.java
:
package my.package;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StartApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("application-context.xml");
}
}
configuration.properties
:
example.some=abc
example.other=def
depends.bean=someBean