【Spring笔记】依赖注入

依赖注入:bean对象的创建依赖于容器。bean对象中的所有属性,由容器来注入。

可分为构造器注入,Set方式注入,拓展方式注入

环境搭建:

实体类:

beans.xml

 

 

测试类:

    @Test
    public void mytest(){
        ClassPathXmlApplicationContext context
                = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());

    }

测试结果:

Student{name='秦江', address=Address{address='南京'},
books=[红楼梦, 西游记, 水浒传, 三国演义], 
hobbies=[睡觉, 吃饭, 听歌], 
card={身份证=1001, 校园卡=1002}, 
games=[王者荣耀, 吃鸡], 
info={学号=123, 专业=计算机, 性别=男}}

猜你喜欢

转载自blog.csdn.net/m0_52043808/article/details/124384141