• Spring(IOC_DI)依赖注入(配置文件)2022/08/22


    代码整体布局:

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

    代码如下:

    pom.xml:

    
    
        4.0.0
    
        org.example
        mybatis_0822_KTLX
        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

    spring-1.xml:

    
    
    
        
            
            
            
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    Student:

    package com.test;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    //@Component:用于告诉spring,当前类的对象需要spring来管理。
    //          里面的名字就相当于bean标签的id,作为对象的唯一标识
    @Component("stu")
    public class Student {
        private String name;
        private String sex;
        private Integer age;
    
        public Student() {
            System.out.println("=====Student 无参构造=====");
        }
    
        public Student(String name, String sex, Integer age) {
            this.name = name;
            this.sex = sex;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            System.out.println("========setName========:"+name);
            this.name = name;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", sex='" + sex + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    
    • 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
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    Test:

    package com.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import javax.annotation.Resource;
    
    public class Test {
        @Resource
        private Student stu;
        public static void main(String[] args) {
            //创建spring容器
    
            ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-1.xml"); //运行结果:=====Student 无参构造===== ========setName========:张三
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    Test2:

    package com.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test2 {
        public static void main(String[] args) {
            //通过注解从spring容器中获取对象
            //1.在spring配置文件中,配置包扫描
            //2.在需要被spring管理的类上加特定注解
            ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-1.xml");
    
            Student stu=ctx.getBean(Student.class);
            System.out.println(stu); //运行结果:=====Student 无参构造===== ========setName========:张三  Student{name='张三', sex='男', age=20}
    
            Student stu1=(Student)ctx.getBean("stu");
            System.out.println(stu1); //运行结果: Student{name='张三', sex='男', age=20}
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    spring-2.xml:

    
    
    
    
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Car:

    package com.test2;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Repository;
    import org.springframework.stereotype.Service;
    
    @Component("mycar") //通过注释,可以用在任意对象上
    //@Service  //本质就是@Component这个注释的别名,用于Service层的对象
    //@Repository //本质就是@Component这个注释的别名,用于数据访问层的对象
    public class Car {
        //品牌
        @Value("宝马")
        private String brand;
        //价格
        @Value("2000")
        private int price;
        //时速
        @Value("120")
        private int space;
    
        public Car() {
        }
    
        public Car(String brand, int price, int space) {
            this.brand = brand;
            this.price = price;
            this.space = space;
        }
    
        public void run(){
            System.out.println(this.brand+"在跑");
        }
    
        public String getBrand() {
            return brand;
        }
    
        public void setBrand(String brand) {
            this.brand = brand;
        }
    
        public int getPrice() {
            return price;
        }
    
        public void setPrice(int price) {
            this.price = price;
        }
    
        public int getSpace() {
            return space;
        }
    
        public void setSpace(int space) {
            this.space = space;
        }
    
        @Override
        public String toString() {
            return "Car{" +
                    "brand='" + brand + '\'' +
                    ", price=" + price +
                    ", space=" + space +
                    '}';
        }
    }
    
    
    • 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
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    Driver:

    package com.test2;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.Resource;
    
    @Component  //当注释没有指定id名字的时候,spring自动用类名小驼峰做对象的id名
    public class Driver {
        //@Value只用于注入普通值
        @Value("张三")
        private String name;
        @Value("20")
        private Integer age;
    
        //@Autowired 用于注入对象类型的值(按照类型注入对象,前提是spring容器中有该类型的对象)
        //           该注释属于spring框架下的注释。特征是按照类型注入对象
        //@Autowired
        @Resource
        private Car car;
    
        public Driver() {
        }
    
        public Driver(String name, Integer age, Car car) {
            this.name = name;
            this.age = age;
            this.car = car;
        }
    
        //开车
        public void drive(){
            System.out.println(this.name+"开车");
            this.car.run();
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            //注解方式没有调用setter方法
            System.out.println("==========setName==============:"+name);
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public Car getCar() {
            return car;
        }
    
        public void setCar(Car car) {
            this.car = car;
        }
    
        @Override
        public String toString() {
            return "Driver{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", car=" + car +
                    '}';
        }
    }
    
    
    • 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
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    Test:

    package com.test2;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
        public static void main(String[] args) {
            ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-2.xml");
            //获取容器中的司机对象
            Driver driver=ctx.getBean(Driver.class);
            //调用开车的方法
            driver.drive(); //运行结果:张三开车 宝马在跑
    
    
            Car car=ctx.getBean(Car.class);
            System.out.println(car);   //运行结果:Car{brand='宝马', price=2000, space=120}
    
            Car mycar=(Car)ctx.getBean("mycar");
            System.out.println(mycar); //运行结果:Car{brand='宝马', price=2000, space=120}
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    spring-3.xml:

    
    
    
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    IA:

    package com.test3;
    
    public interface IA {
        void test();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    ClsA1:

    package com.test3;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class ClsA1 implements IA{
        @Override
        public void test() {
            System.out.println("这是A1测试");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    ClsA2:

    package com.test3;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class ClsA2 implements IA{
        @Override
        public void test() {
            System.out.println("这是A2测试");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    MyCls:

    package com.test3;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.Resource;
    
    @Component //当注释没有指定id名字的时候,spring自动用类名小驼峰做对象的id名
    public class MyCls {
    //    @Autowired
    //    private IA a;  //运行结果:ERROR:expected single matching bean but found 2: clsA1,clsA2
    
    //    //@Autowired先按类型,再按照名字注入
    //    @Autowired
    //    private IA clsA1;    //指定名字注入。 ClsA1类名小写clsA1 。
    //
    //    public void show(){
    //        this.clsA1.test();
    //    }                   //运行结果:这是A1测试
    
        //@Autowired先按类型,再按照名字注入
        @Autowired
        @Qualifier("clsA1") //按名字注入对象a
        private IA a;
    
        public void show(){
            this.a.test();
        }                   //运行结果:这是A1测试
    
    }
    
    
    • 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
    • 29
    • 30
    • 31
    • 32

    MyCls2:

    package com.test3;
    
    import org.springframework.stereotype.Component;
    
    import javax.annotation.Resource;
    
    @Component
    public class MyCls2 {
    
    //    @Resource(name = "clsA1")
    //    private IA a;
    //
    //    public void show(){
    //        this.a.test();
    //    }                   //运行结果:这是A1测试
    
    //    @Resource
    //    private IA clsA1; //指定名字注入
    //
    //    public void show(){
    //        this.clsA1.test();
    //    }                   //运行结果:这是A1测试
    
    //    //先按名字,再按类型注入
    //    @Resource
    //    private IA a;
    //
    //    public void show(){
    //        this.a.test();
    //    }  //运行结果:BeanNotOfRequiredTypeException: Bean named 'a' is expected to be of type 'com.test3.IA' but was actually of type 'com.test3.Dog'
    
        //先按名字,再按类型注入
        @Resource
        private IA a;
    
        public void show(){
            this.a.test();
        } //运行结果:NoUniqueBeanDefinitionException: No qualifying bean of type 'com.test3.IA' available: expected single matching bean but found 2: clsA1,clsA2
    }
    
    
    
    • 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
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    Dog:

    package com.test3;
    
    import org.springframework.stereotype.Component;
    
    //@Component("a")
    @Component("dog")
    public class Dog {
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Test:

    package com.test3;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("spring-3.xml");
            MyCls myCls=ac.getBean(MyCls.class);
    //        ac.getBean(MyCls.class);
            myCls.show();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    Test2:

    package com.test3;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test2 {
        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("spring-3.xml");
            MyCls2 myCls2=ac.getBean(MyCls2.class);
            myCls2.show();
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    讲解参考:

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

    // 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
  • 相关阅读:
    【c#】使用Prometheus监控Windows系统
    如何保护我们的网络安全
    虚拟机Ubuntu操作系统常用终端命令(2)(详细解释+详细演示)
    【蓝桥杯选拔赛真题25】python输出指定数据 青少年组蓝桥杯python 选拔赛STEMA比赛真题解析
    protobuf使用详解
    centos获取服务器公网ip
    Spring Boot、Spring Cloud 自定义配置文件(如何整合配置中心)
    营销技术(Martech)的持续爆炸式增长,市场总监的工作变得更加艰难
    SpringBoot整合MongoDB
    [资源推荐]langchain、LLM相关
  • 原文地址:https://blog.csdn.net/Liu_wen_wen/article/details/126470859