Spring bean在相同xml文件和不同的xml文件中引用的方式

来源:互联网 发布:java怎么输入字符串 编辑:程序博客网 时间:2024/06/05 04:28
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/beanshttp://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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd";
    
     <bean id="helloBean" class="com.spenglu.Teacher">
     </bean>
</beans>

原创粉丝点击