• bean实例化的三种方式 bean标签常用属性 单例模式和多例模式的对象 BeanFactory和ApplicationContext:


    学习网站/博客:

    spring https://spring.io/
    在这里插入图片描述
    The loC Container https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans
    在这里插入图片描述
    Spring官方文档(中文版!!!) https://blog.csdn.net/li1376417539/article/details/104951358/
    在这里插入图片描述

    1.bean实例化的三种方式:

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    页面结构整体布局:

    在这里插入图片描述

    运行结果如下:

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    代码如下:

    pom.xml:

    
    
        4.0.0
    
        org.example
        lesson0820_spring
        1.0-SNAPSHOT
    
        
            8
            8
        
    
        
            
            
                org.springframework
                spring-context
                5.3.18
            
        
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    Student:

    package com.entity;
    
    public class Student {
    
        public Student(){
            System.out.println("这是Student的构造方法...");
        }
    
        public void introduce(){
            System.out.println("我是一个学生");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    MyStaticFactory :

    package com.factory;
    
    import com.entity.Student;
    
    /**
     * 静态工厂
     */
    public class MyStaticFactory {
        /**
         * 静态返回学生实例
         * @return
         */
        public static Student createInstance(){
            System.out.println("调用静态工厂.....MyStaticFactory...createInstance");
            return new Student();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    MyInstanceFactory :

    package com.factory;
    
    import com.entity.Student;
    
    /**
     * 实例工厂
     */
    public class MyInstanceFactory {
        /**
         * 定义创建对象的实例方法
         * @return
         */
        public Student createInstance(){
            return new Student();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    Test:

    package com.test;
    
    import com.entity.Student;
    import com.factory.MyInstanceFactory;
    import com.factory.MyStaticFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
        public static void main(String[] args) {
            //Student student = new Student();
            //创建spring bean容器对象
            ApplicationContext ac = new ClassPathXmlApplicationContext("spring-1.xml");
    //        Student student = ac.getBean(Student.class);
    
            //静态工厂创建的对象,通过id名字,获取bean对象
    //        Student s = (Student) ac.getBean("stu");
    //        s.introduce();
    
            //实例工厂方式创建对象
            Student s2 = (Student) ac.getBean("stu2");
            s2.introduce();
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    spring-1.xml:

    
    
    
        
    
    
        
        
    
        
        
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    2.bean标签常用属性:

    在这里插入图片描述

    代码整体布局:

    在这里插入图片描述

    运行结果:

    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述

    添加/修改的代码如下:

    spring-2.xml:(添加)

    
    
    
        
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    Student:(修改)

    package com.entity;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    public class Student {
    
        public Student(){
            System.out.println("这是Student的构造方法...");
        }
    
        //自我介绍
        public void introduce(){
            System.out.println("我是一个学生");
        }
    
        //学生初始化
        public void initStu(){
            System.out.println("调用了学生的初始化方法");
        }
    
        //学生销毁
        public void destroyStu(){
            System.out.println("调用了学生的销毁方法");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    (test)Test2:

    package com.test;
    
    import com.entity.Student;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test2 {
        public static void main(String[] args) {
            //创建spring容器
            ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-2.xml");
            //根据id获取学生对象
            Student s1 =(Student) ctx.getBean("stu");
            System.out.println("s1:"+s1);
            //根据类型获取学生对象
            Student s2 = (Student) ctx.getBean(Student.class);
            System.out.println("s2:"+s2);
            //根据id和类型获取学生对象
            Student s3 = ctx.getBean("stu",Student.class);
            System.out.println("s3:"+s3);
            //根据name属性获取学生对象
            Student s4 = (Student) ctx.getBean("s1");
            Student s5 = (Student) ctx.getBean("s2");
            System.out.println("s4:"+s4);
            System.out.println("s5:"+s5);
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    3.单例模式和多例模式的对象:

    在这里插入图片描述

    运行结果:

    在这里插入图片描述

    在这里插入图片描述

    God:

    package com.singleton;
    
    public class God {
    
        public void createWorld(){
            System.out.println("上帝创造了世界....");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Test:

    package com.singleton;
    
    public class Test {
        public static void main(String[] args) {
            God god = new God();
            god.createWorld();
            System.out.println(god);
    
            God god2 = new God();
            god2.createWorld();
            System.out.println(god2);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    (singleton)God:(添加)

    package com.singleton;
    
    public class God {
    
        //3.定义静态私有对象
        private static God god;
    
        //1.构造私有化
        private God(){
    
        }
    
        //2.定义静态工厂获取实例的方法,用于获取对象
        public static God createInstance(){
            if (god==null){
                god = new God();
            }
            return god;
        }
    
        public void createWorld(){
            System.out.println("上帝创造了世界....");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    (singleton)Test:(添加)

    package com.singleton;
    
    public class Test {
        public static void main(String[] args) {
            God god = God.createInstance();
            god.createWorld();
            System.out.println(god);
    
            God god2 = God.createInstance();
            god2.createWorld();
            System.out.println(god2);
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4.BeanFactory和ApplicationContext:

    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    // A code block
    var foo = 'bar';
    
    • 1
    • 2

    // A code block
    var foo = 'bar';
    
    • 1
    • 2

    // A code block
    var foo = 'bar';
    
    • 1
    • 2

    // A code block
    var foo = 'bar';
    
    • 1
    • 2

    // A code block
    var foo = 'bar';
    
    • 1
    • 2

    // A code block
    var foo = 'bar';
    
    • 1
    • 2

    // A code block
    var foo = 'bar';
    
    • 1
    • 2

    // A code block
    var foo = 'bar';
    
    • 1
    • 2

    // A code block
    var foo = 'bar';
    
    • 1
    • 2
  • 相关阅读:
    【Qt+FFmpeg】 - FFmpeg解码详细流程
    matlab实现多目标优化求解-NSGA-II 算法
    Linux中查找某路径下,包含某个字符串的所有文件
    JVM内存配置参数
    win10+Android(华为)系统原生日历同步方案+Sol日历桌面显示
    Android开发基础——广播实践
    安全运营之通行字管理
    Windows平台下私有云盘搭建
    查阅相关文献描述CN,SMC,EMCI,LMCI,AD五类疾病的早期干预和诊断标准|2022数维杯国赛C题
    mac上安装brew(最简易)
  • 原文地址:https://blog.csdn.net/Liu_wen_wen/article/details/126491469