Java Spring引用的bean使用ref标记的本地属性存在于其他xml中
我正在创建示例spring程序来理解ref标记的本地属性 我已经创建了两个bean文件 第一个[applicationcontext.xml]Java Spring引用的bean使用ref标记的本地属性存在于其他xml中,java,spring,inversion-of-control,Java,Spring,Inversion Of Control,我正在创建示例spring程序来理解ref标记的本地属性 我已经创建了两个bean文件 第一个[applicationcontext.xml] <bean class="org.vik.spring.SequenceGenerator" name="sequenceProperty_Other"> <property name="prefix"> <ref local="prefixGeneratorOther" /
<bean class="org.vik.spring.SequenceGenerator" name="sequenceProperty_Other">
<property name="prefix">
<ref local="prefixGeneratorOther" />
</property>
<property name="suffix" value="23"></property>
</bean>
<bean class="org.vik.spring.DatePrefixGenerator" id="prefixGeneratorOther" p:prefix="other"/>
当我请求bean“sequenceProperty\uOther”时,spring成功地返回了它
SequenceGenerator sequenceConstrutornerator = applicationContext.getBean( "sequenceProperty_Other",SequenceGenerator.class);
我可以从中理解的是,作为“prefixGeneratorOther”bean,它不在同一个xml文件(applicationcontext.xml)中,并且我使用本地属性来引用它,Spring应该通过异常。但在我的情况下,它的工作。我错过了什么吗
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- <bean class="org.vik.spring.SequenceGenerator" name="sequence"> </bean> -->
<bean class="org.vik.spring.DatePrefixGenerator" id="prefixGenerator"
p:prefix="122333">
</bean>
<bean class="org.vik.spring.SequenceGenerator" name="sequenceProperty_Locale">
<property name="prefix">
<ref local="prefixGenerator" />
</property>
<property name="suffix" value="23"></property>
</bean>
<bean class="org.vik.spring.SequenceGenerator" name="sequenceProperty_Other">
<property name="prefix">
<ref local="prefixGeneratorOther" />
</property>
<property name="suffix" value="23"></property>
</bean>
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.vik.spring.DatePrefixGenerator" id="prefixGeneratorOther" p:prefix="other"/>
</beans>
此特定行为适用于3.x分支中Spring3.1.0(包括)之后的任何版本。如果您使用最新的3.0.x(即3.0.7)进行测试,您将得到一个异常。如果您使用Spring4进行测试,您将得到一个异常,但会得到一个不同的异常 如果仔细查看Spring 3.0.7中的异常,这涉及到XML解析:
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
这意味着限制在xml模式级别(我相信它也在Java代码级别)
这一行为在Spring3.1.0(以及之后)中已经发生了变化。从所有与之相关的JIRA问题来看,似乎可以解释发生了什么:限制已经从3.1模式中消除,并且
ref-local
进入了一种“不推荐”状态(因为在3.1.x和3.2.x中可以使用它),而在Spring 4ref-local
已经完全消除。在Spring 4中,xsd模式也被更新(从某种意义上说,ref
不再接受local
).您确定applicationcontext.xml中没有prefixGeneratorOther
bean吗?或者您在applicationcontext中使用的xml文件可能不是您认为正在使用的文件。我已验证applicationcontext.xml没有bean prefixGeneratorOther。为了验证代码引用的是同一个xml文件,我将xml文件重命名为spring抛出异常“applicationcontext.xml file not found”。在我看来,您发布的代码不应该工作。我认为它也不应该工作,这就是我发布它的原因。感谢您的详细解释
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("applicationcontext.xml",
"prefix_context.xml");
System.out.println(applicationContext.getBean("prefixGenerator", PrefixGenerator.class).getPrefix());
SequenceGenerator sequenceProperty = applicationContext.getBean("sequenceProperty_Locale",
SequenceGenerator.class);
System.out.println(sequenceProperty);
SequenceGenerator sequenceConstrutornerator = applicationContext.getBean("sequenceProperty_Other",
SequenceGenerator.class);
System.out.println(sequenceConstrutornerator);
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)