applicationContext.xml 中 <ref bean=""> 与 <ref local="">的区别

第一步:创建工程   FirstSpringJAVA

第二步:新建dao层类

Dao.java

package com.xuzhiwen.spring10;

public class Dao {
    
    public void save(){
        System.out.println("Dao.save()...");
    }
}

第三步:新建service类,将dao通过set的方式注入到 service中

ServiceTest.java

package com.xuzhiwen.spring10;

public class ServiceTest {
    private Dao dao;
    
    public void setDao(Dao dao) {
        this.dao = dao;
    }
    
    public void service(){
        System.out.println("ServiceTest.service()...");
        dao.save();
    }
}

第四步:测试 <ref bean=" ">  ,可以在两个配置文件中使用

applicationContext10.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <bean id="service" class="com.xuzhiwen.spring10.ServiceTest">
        <property name="dao">
            <ref bean="dao" />
        </property>
    </bean>
    
</beans>

applicationContext11.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <bean id="dao" class="com.xuzhiwen.spring10.Dao" />
</beans>

第五步:编写测试类

Test.java

package com.xuzhiwen.spring10;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        String xml[] = {"applicationContext10.xml","applicationContext11.xml"};
        ApplicationContext app = new ClassPathXmlApplicationContext(xml);
        ServiceTest test = (ServiceTest) app.getBean("service");
        test.service();
    }
}

第六步:运行结果如下

 

第七步: 测试 <ref local=" ">,只能在同一个配置文件中使用

applicationContext10.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <bean id="service" class="com.xuzhiwen.spring10.ServiceTest">
        <property name="dao">
            <ref local="dao" />
        </property>
    </bean>
    
    <bean id="dao" class="com.xuzhiwen.spring10.Dao" />
</beans>

 

第八步: 编写测试类

Test.java

package com.xuzhiwen.spring10;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        String xml[] = {"applicationContext10.xml"};
        ApplicationContext app = new ClassPathXmlApplicationContext(xml);
        ServiceTest test = (ServiceTest) app.getBean("service");
        test.service();
    }
}

第九步:运行结果如下

 

posted @ 2017-08-18 10:08  beibidewomen  阅读(277)  评论(0编辑  收藏  举报