文章内容
一、属性注入简介
所谓依赖注入,是指程序运行过程中,如果需要调用另一个对象协助时,无须在代码中创建被调用者,而是依赖于外部的注入。Spring的依赖注入对调用者和被调用者几乎没有任何要求,完全支持对POJO之间依赖关系的管理。
1、创建实体学生类
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | @Data @AllArgsConstructor @NoArgsConstructor public class Student { // 学生的ID String id; // 学生的姓名 String username; // 学生的密码 String password; // 学生的年龄 String age; // 学生的性别 Boolean gender; // 学生的爱好数组 String[] hobbys; // 存储学生学习的课程名称 List<String> list; // 学生的教师名称 Set<String> set; // 学生姓名和分数 Map<String, Integer> score; // 属性信息 Properties properties; // 学生信息 School school; } |
2、创建班级实体类
1 2 3 4 5 6 7 8 9 | @Data @AllArgsConstructor @NoArgsConstructor public class School { // 学校名称 String name; // 学校地址 String address; } |
二、基本类型和String属性注入
1、属性注入
1 2 3 4 5 | < bean id = "student" class = "com.ntan520.demo.spring.domain.Student" > < property name = "username" value = "潘金莲" ></ property > < property name = "password" value = "123" ></ property > < property name = "age" value = "18" ></ property > </ bean > |
2、测试
01 02 03 04 05 06 07 08 09 10 | @Test public 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、属性注入
01 02 03 04 05 06 07 08 09 10 | < bean id = "student" class = "com.ntan520.demo.spring.domain.Student" > <!-- 对象类型--> < property name = "school" ref = "school" /> </ bean > <!-- 学校信息属性注入--> < bean id = "school" class = "com.ntan520.demo.spring.domain.School" > < property name = "name" value = "山西大学" ></ property > < property name = "address" value = "山西省太原市" ></ property > </ bean > |
2、测试
01 02 03 04 05 06 07 08 09 10 | @Test public 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、属性注入
1 2 3 4 5 6 7 8 9 | < bean id = "student" class = "com.ntan520.demo.spring.domain.Student" > <!-- 数组类型--> < property name = "hobbys" > < array > < value >读书</ value > < value >学习</ value > </ array > </ property > </ bean > |
2、测试
01 02 03 04 05 06 07 08 09 10 | @Test public 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、属性注入
01 02 03 04 05 06 07 08 09 10 | < bean id = "student" class = "com.ntan520.demo.spring.domain.Student" > <!-- list集合类型--> < property name = "list" > < list > < value >语文</ value > < value >数学</ value > < value >英语</ value > </ list > </ property > </ bean > |
2、测试
01 02 03 04 05 06 07 08 09 10 | @Test public 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、属性注入
1 2 3 4 5 6 7 8 9 | < bean id = "student" class = "com.ntan520.demo.spring.domain.Student" > <!-- set集合--> < property name = "set" > < set > < value >西门庆</ value > < value >潘金莲</ value > </ set > </ property > </ bean > |
2、测试
01 02 03 04 05 06 07 08 09 10 | @Test public 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、属性注入
1 2 3 4 5 6 7 8 9 | < bean id = "student" class = "com.ntan520.demo.spring.domain.Student" > <!-- map对象--> < property name = "score" > < map > < entry key = "语文" value = "99" /> < entry key = "数学" value = "98" /> </ map > </ property > </ bean > |
2、测试
01 02 03 04 05 06 07 08 09 10 | @Test public 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、属性注入
1 2 3 4 5 6 7 8 9 | < bean id = "student" class = "com.ntan520.demo.spring.domain.Student" > <!-- properties--> < property name = "properties" > < props > < prop key = "邮箱" >11111@qq.com</ prop > < prop key = "电话" >18800000000</ prop > </ props > </ property > </ bean > |
2、测试
01 02 03 04 05 06 07 08 09 10 | @Test public void testHello() { // 从类路径 ClassPath 中寻找指定的 XML 配置文件, // 找到并装载完成 ApplicationContext 的实例化工作 ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" ); // 根据ID得到实例化对象 Student student = context.getBean( "student" , Student. class ); // 调用对象的方法 System.out.println( "student = " + student); } |