码迷,mamicode.com
首页 > 编程语言 > 详细

Spring笔记02(3种加载配置文件的方式)

时间:2017-09-29 16:51:41      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:接口   标识   ram   加载   frame   根据   电脑   factor   实现类   

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种加载配置文件的方式)

标签:接口   标识   ram   加载   frame   根据   电脑   factor   实现类   

原文地址:http://www.cnblogs.com/lyb0103/p/7611097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!