本文主要是介绍Spring bean在相同xml文件和不同的xml文件中引用的方式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Bean在相同的xml文件中,可以通过ref便签,以及它的local属性来引用它。如下所示:
< 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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd";
< bean id = "StudentBean" class = "com.spenglu.Student" >
<property name="teacher" >
<ref local="helloBean"/>
</property>
</ bean >
<bean id="helloBean" class="com.spenglu.Teacher">
</bean>
</ beans >
Bean在不同的xml文件中,可以通过ref标签的bean属性来引用它。如下所示:
Student.xml:
< 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-3.1.xsd" ;
< bean id = "StudentBean" class = "com. spenglu .Student" >
<property name="teacher" >
<ref bean=" helloBean "/>
</property>
</ bean >
</ beans >
Teacher.xml:
< 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-3.1.xsd" ;
<bean id="helloBean" class="com.spenglu.Teacher">
</bean>
</ beans >
这篇关于Spring bean在相同xml文件和不同的xml文件中引用的方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!