我正在创建示例spring程序来理解,ref标签的局部属性。
我创建了两个bean文件
第一个[applicationcontext.xml]
<bean class="org.vik.spring.SequenceGenerator" name="sequenceProperty_Other">
<property name="prefix">
<ref local="prefixGeneratorOther" />
</property>
<property name="suffix" value="23"></property>
</bean>
第二个xml文件[prefix_context.xml]
<bean class="org.vik.spring.DatePrefixGenerator" id="prefixGeneratorOther" p:prefix="other"/>
我已经创建了应用程序上下文如下
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("applicationcontext.xml" ,"prefix_context.xml");
当我请求bean"sequenceProperty_Other"时,spring成功返回它
SequenceGenerator sequenceConstrutornerator = applicationContext.getBean( "sequenceProperty_Other",SequenceGenerator.class);
我从中可以理解的是,作为"prefixGeneratorOther"bean在不相同的xml文件(applicationcontext.xml)中,我使用本地属性来引用它,Spring应该通过异常。但在我的情况下,它起作用了。我错过了什么吗?
中
<?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>
prefix_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: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>
Java类 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);
此行为适用于Spring 3.1.0(包含)之后的任何版本。x分支。如果您使用最新的3.0进行测试。X(它是3.0.7),你会得到一个异常。如果你用Spring 4测试,你会得到一个异常,但是一个不同的。
如果仔细看一下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代码级别)。
由于这个JIRA问题,这个行为在Spring 3.1.0(及之后)中发生了变化。从所有与之相关的JIRA问题来看,这个问题似乎解释了发生的事情:限制已经从3.1模式中消除,并且ref local
进入了一种"弃用"状态(因为在3.1中。X和3.2。在Spring 4中,ref local
已被完全消除。在Spring 4中,文档说不再支持ref local
,而且xsd模式已经更新(从某种意义上说,ref
不再接受local
)。