03.Spring常见的属性注入

一、属性注入简介

所谓依赖注入,是指程序运行过程中,如果需要调用另一个对象协助时,无须在代码中创建被调用者,而是依赖于外部的注入。Spring的依赖注入对调用者和被调用者几乎没有任何要求,完全支持对POJO之间依赖关系的管理。

1.创建实体学生类

@Data@AllArgsConstructor@NoArgsConstructorpublic class Student {    //学生的ID    String id;    //学生的姓名    String username;    //学生的密码    String password;    //学生的年龄    String age;    //学生的性别    Boolean gender;    //学生的爱好数组    String[] hobbys;    //存储学生学习的课程名称    List list;    //学生的教师名称    Set set;    //学生姓名和分数    Map score;    //属性信息    Properties properties;    //学生信息    School school;}

2.创建班级实体类

@Data@AllArgsConstructor@NoArgsConstructorpublic class School {    //学校名称    String name;    //学校地址    String address;}

二、基本类型和String属性注入

1.属性注入

            

2.打印测试

@Testpublic void testHello(){    //从类路径 ClassPath 中寻找指定的 XML 配置文件,    //找到并装载完成 ApplicationContext 的实例化工作    ApplicationContext context =            new ClassPathXmlApplicationContext("applicationContext.xml");    //根据ID得到实例化对象    Student student = context.getBean("student", Student.class);    //调用对象的方法    System.out.println("student = " + student);}

三、对象类型属性注入

1.属性注入

                    

2.打印测试

@Testpublic void testHello(){    //从类路径 ClassPath 中寻找指定的 XML 配置文件,    //找到并装载完成 ApplicationContext 的实例化工作    ApplicationContext context =            new ClassPathXmlApplicationContext("applicationContext.xml");    //根据ID得到实例化对象    Student student = context.getBean("student", Student.class);    //调用对象的方法    System.out.println("student = " + student);}

四、数组类型属性注入

1.属性注入

                            读书            学习            

2.打印测试

@Testpublic void testHello(){    //从类路径 ClassPath 中寻找指定的 XML 配置文件,    //找到并装载完成 ApplicationContext 的实例化工作    ApplicationContext context =            new ClassPathXmlApplicationContext("applicationContext.xml");    //根据ID得到实例化对象    Student student = context.getBean("student", Student.class);    //调用对象的方法    System.out.println("student = " + student);}

五、List集合属性注入

1.属性注入

                            语文            数学            英语            

2.打印测试

@Testpublic void testHello(){    //从类路径 ClassPath 中寻找指定的 XML 配置文件,    //找到并装载完成 ApplicationContext 的实例化工作    ApplicationContext context =            new ClassPathXmlApplicationContext("applicationContext.xml");    //根据ID得到实例化对象    Student student = context.getBean("student", Student.class);    //调用对象的方法    System.out.println("student = " + student);}

六、Set集合属性注入

1.属性注入

                            贾小辉            谢小婉            

2.打印测试

@Testpublic void testHello(){    //从类路径 ClassPath 中寻找指定的 XML 配置文件,    //找到并装载完成 ApplicationContext 的实例化工作    ApplicationContext context =            new ClassPathXmlApplicationContext("applicationContext.xml");    //根据ID得到实例化对象    Student student = context.getBean("student", Student.class);    //调用对象的方法    System.out.println("student = " + student);}

七、Map集合属性注入

1.属性注入

                                                    

2.打印测试

@Testpublic void testHello(){    //从类路径 ClassPath 中寻找指定的 XML 配置文件,    //找到并装载完成 ApplicationContext 的实例化工作    ApplicationContext context =            new ClassPathXmlApplicationContext("applicationContext.xml");    //根据ID得到实例化对象    Student student = context.getBean("student", Student.class);    //调用对象的方法    System.out.println("student = " + student);}

八、Properties属性注入

1.属性注入

                            782000000@qq.com            17600000000            

2.打印测试

@Testpublic void testHello(){    //从类路径 ClassPath 中寻找指定的 XML 配置文件,    //找到并装载完成 ApplicationContext 的实例化工作    ApplicationContext context =            new ClassPathXmlApplicationContext("applicationContext.xml");    //根据ID得到实例化对象    Student student = context.getBean("student", Student.class);    //调用对象的方法    System.out.println("student = " + student);}
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章