1. 程式人生 > >Spring筆記02(3種加載配置文件的方式)

Spring筆記02(3種加載配置文件的方式)

con 創建對象 face ati prototype property str als cati

1.不使用Spring的實例:

01.Animal接口對應的代碼:

package cn.pb.dao;

/**
 * 動物接口
 */
public interface Animal {
    //吃飯
    String eat();
    //睡覺
    void sleep();
}

02.Animal接口的實現類Dog對應的代碼:

package cn.pb.dao.impl;
/**
 * animal的實現類
 */

import cn.pb.dao.Animal;

public class Dog implements Animal{
    /**
     * 無參構造 驗證什麽時候實例被創建
     
*/ public Dog(){ System.out.println("dog被實例化了!"); } public String eat() { System.out.println("吃飯的方法"); return null; } public void sleep() { System.out.println("睡覺的方法"); } }

03.測試的代碼:

1  @Test
2     public  void  test01(){
3         //之前的一種方式    耦合的!
4 Animal animal=new Dog(); 5 animal.eat(); 6 animal.sleep(); 7 }

2.使用spring解耦的方式 創建applicationContext.xml文件 放在src的根目錄下

01.Animal接口對應的代碼:

package cn.pb.dao;

/**
 * 動物接口
 */
public interface Animal {
    //吃飯
    String eat();
    //睡覺
    void sleep();
}

02.Animal接口的實現類Dog對應的代碼:

package cn.pb.dao.impl;
/**
 * animal的實現類
 */

import cn.pb.dao.Animal;

public class Dog implements Animal{
    /**
     * 無參構造 驗證什麽時候實例被創建
     */
    public Dog(){
        System.out.println("dog被實例化了!");
    }

    public String eat() {
        System.out.println("吃飯的方法");
        return null;
    }

    public void sleep() {
        System.out.println("睡覺的方法");
    }


}

03.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"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--接收程序給我們的bean對象
    id就是我們的一個標識
    class是對應的實現類,class不能是接口
  lazy-init="true" 默認是false 按需加載,就是在getBean的時候才會創建實例
  -->
<bean id="dog" class="cn.pb.dao.impl.Dog" ></bean> </beans>

04.測試的代碼:

001.applicationContext.xml放在項目的根路徑下面

 @Test
    public void test02(){
         /*
         * 使用spring  對象交給容器來創建 解耦
         * 01.引入jar
         * 02.創建容器applicationContext.xml
         * 03.加載spring的配置文件  創建容器   會把容器中所有的bean實例化
         * 04.然後從容器中取Bean
         */
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

        System.out.println("**************");
        //因為我們在容器中定義了ID  根據id找到對應的類
        Animal dog=(Dog)context.getBean("dog");
        dog.eat();
        dog.sleep();
    }

002.applicationContext.xml放在項目的根路徑下面

 @Test
    public  void  test03(){
        /**
         * 默認applicationContext.xml放在項目的根路徑下面
         * 也可以放在電腦指定的盤符下d:/applicationContext.xml
         * 使用new FileSystemXmlApplicationContext來創建對象
         */

        ApplicationContext context=new FileSystemXmlApplicationContext("d:/applicationContext.xml");
        System.out.println("*************************");
        //因為我們在容器中定義了ID  根據id找到對應的類
        Animal dog=(Animal) context.getBean("dog");
        dog.eat();
        dog.sleep();
    }

003.使用BeanFactory來創建容器的時候,不會實例化容器中所有的Bean,

在getBean()才創建對應Bean的對象,按需加載。

 @Test
    public  void  test04(){
        /*
         * 使用BeanFactory來創建容器的時候,不會實例化容器中的Bean
         * 在getBean()才創建對應Bean的對象
         */
        BeanFactory context=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
        System.out.println("*************************");
        //因為我們在容器中定義了ID  根據id找到對應的類
        Animal dog=(Animal) context.getBean("dog");
        dog.eat();
        dog.sleep();
    }

05.在spring的核心配置文件中 所有的bean默認都是單例模式:

001.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"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--接收程序給我們的bean對象
    id就是我們的一個標識
    class是對應的實現類,class不能是接口
    -->

    <!--配置我們的student對象   lazy-init="true" 默認是false 按需加載-->
    <bean id="student" class="cn.pb.bean.Student" lazy-init="true">
        <property name="name" value="小黑"></property>
        <property name="age" value="18"></property>
    </bean>

    <!-- 在spring的核心配置文件中  所有的bean默認都是單例模式
       scope="singleton"  默認
       scope="prototype"  原型
       -->
    <bean id="student2" class="cn.pb.bean.Student" scope="singleton">
        <property name="age" value="40"/>
        <property name="name" value="小黑2"/>
    </bean>


</beans>

002.驗證代碼:

/**
     *  驗證單例模式
     *  01.默認是單例  調用同一個對象 輸出true
     *  02.之後再xml文件中的student2  增加屬性scope="prototype"
     *  03.再次驗證  兩個對象肯定不一致 輸出false
     */
    @Test
    public   void  studentTest5(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("*******************************************");
        Student student = (Student) context.getBean("student2");
        System.out.println(student);
        Student  student2 = (Student) context.getBean("student2");  //再次獲取
        System.out.println(student==student2);

    }

Spring筆記02(3種加載配置文件的方式)