zoukankan      html  css  js  c++  java
  • Spring笔记02(3种加载配置文件的方式)

    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);
    
        }
  • 相关阅读:
    vue2.0 移动端,下拉刷新,上拉加载更多 封装组件
    Mac 安装RN android开发环境
    JavaScript 复杂判断的更优雅写法
    JSBridge的原理及使用
    FlatList列表组件的使用
    React Native ScrollView中TextInput焦点问题
    pod update报错(Cocoapods: Failed to connect to GitHub to update the CocoaPods/Specs specs repo)报错解决方案
    如何在pc端通过adb连接手机调试,不用usb数据线
    react学习之搭建后台管理系统
    node+koa2+axios踩坑记
  • 原文地址:https://www.cnblogs.com/lyb0103/p/7611097.html
Copyright © 2011-2022 走看看