什么网站可以自己做配图,有关小城镇建设网站,外国的免费网站网站,拉新推广怎么做代理目录 引言
1、依赖注入之setter注入
2、依赖注入之构造器注入
3、为类类型属性赋值#xff08;依赖注入之对象注入#xff09;
4、为数组类型属性赋值 引言
Spring框架中的依赖注入#xff08;Dependency Injection#xff0c;DI#xff09;是一种设计模式和编程实践…目录 引言
1、依赖注入之setter注入
2、依赖注入之构造器注入
3、为类类型属性赋值依赖注入之对象注入
4、为数组类型属性赋值 引言
Spring框架中的依赖注入Dependency InjectionDI是一种设计模式和编程实践它通过反转控制Inversion of Control, IoC实现组件之间的解耦。在传统的对象创建过程中一个类通常需要自己创建并管理对其他类的引用这会导致高度耦合。而依赖注入则是由一个第三方容器IOC容器负责创建对象并将这些对象所依赖的对象实例作为构造函数参数、setter方法或字段直接注入到目标对象中。 在Spring框架中依赖注入可以通过构造函数注入、Setter方法注入或字段注入来实现。通过依赖注入开发人员可以将对象之间的依赖关系配置在Spring容器中然后由Spring容器在运行时动态地将依赖关系注入到对象中从而实现对象之间的解耦和灵活的管理。这样可以使得代码更加模块化和易于扩展同时也方便进行单元测试和集成测试。
1、依赖注入之setter注入
①创建学生类Student
public class Student {private Integer id;private String name;private Integer age;private String sex;public Student() {}public Integer getId() { return id;}public void setId(Integer id) { this.id id;}public String getName() { return name;}public void setName(String name) { this.name name;}public Integer getAge() { return age;}public void setAge(Integer age) { this.age age;}public String getSex() { return sex;}public void setSex(String sex) { this.sex sex;}Overridepublic String toString() { return Student{ id id , name name , age age , sex sex };}
} ②配置bean时为属性赋值
property标签就是通过组件类的setXxx()方法给对象设置属性在运行时容器会动态地将依赖关系注入到对象。
bean idstudentOne classcom.softeem.bean.Student!-- property标签通过组件类的setXxx()方法给组件对象设置属性 --!-- name属性指定属性名这个属性名是getXxx()、setXxx()方法定义的和成员变量无关--!-- value属性指定属性值 --property nameid value1001/propertyproperty namename value张三/propertyproperty nameage value23/propertyproperty namesex value男/property
/bean
③测试
getBean()方法里面的参数一定要配置文件中bean标签中的id一致
Test
public void studentTest(){ApplicationContext ac new ClassPathXmlApplicationContext(applicationContext.xml);Student student ac.getBean(studentOne, Student.class);System.out.println(student student);
} 2、依赖注入之构造器注入
①在Student类中添加有参构造
public Student(Integer id, String name, Integer age, String sex) { this.id id;this.name name; this.age age; this.sex sex;
}
②配置bean bean idstudentTwo classcom.softeem.bean.Studentconstructor-arg value1002/constructor-argconstructor-arg value李四/constructor-argconstructor-arg value33/constructor-argconstructor-arg value女/constructor-arg/bean
注意
constructor-arg标签还有两个属性可以进一步描述构造器参数
index属性指定参数所在位置的索引从0开始
name属性指定参数名
③测试
Test
public void testDIBySet(){ApplicationContext ac new ClassPathXmlApplicationContext(spring-di.xml); Student studentOne ac.getBean(studentTwo, Student.class); System.out.println(studentOne);
} 3、为类类型属性赋值依赖注入之对象注入
①创建班级类Clazz
public class Clazz {private Integer clazzId;private String clazzName;public Integer getClazzId() { return clazzId;} public void setClazzId(Integer clazzId) { this.clazzId clazzId;}public String getClazzName() { return clazzName;}public void setClazzName(String clazzName) { this.clazzName clazzName;}Overridepublic String toString() { return Clazz{ clazzId clazzId , clazzName clazzName };}public Clazz() {}public Clazz(Integer clazzId, String clazzName) { this.clazzId clazzId;this.clazzName clazzName;}
}
②修改Student类
在Student类中添加班级类属性及get、set方法。
为类类型属性赋值方式一引用外部已声明的bean
配置Clazz类型的bean
bean idclazzOne classcom.softeem.bean.Clazzproperty nameclazzId value1111/propertyproperty nameclazzName value财源滚滚班/property
/bean
为Student中的clazz属性赋值
bean idstudentFour classcom.softeem.spring.bean.Studentproperty nameid value1004/propertyproperty namename value赵六/propertyproperty nameage value26/propertyproperty namesex value女/property!-- ref属性引用IOC容器中某个bean的id将所对应的bean为属性赋值 --property nameclazz refclazzOne/property
/bean
property nameclazz refclazzOne/property 就是property为clazz属性赋值但此处不能用value进行赋值而要使用ref对Clazz类型的bean参考。可以看到ref属性的值就是Clazz类型bean的id。 为类类型属性赋值方式二内部bean
bean idstudentFour classcom.softeem.bean.Studentproperty nameid value1004/propertyproperty namename value赵六/propertyproperty nameage value26/propertyproperty namesex value女/propertyproperty nameclazz!-- 在一个bean中再声明一个bean就是内部bean --!-- 内部bean只能用于给属性赋值不能在外部通过IOC容器获取因此可以省略id属性 --bean classcom.softeem.bean.Clazzproperty nameclazzId value2222/propertyproperty nameclazzName value远大前程班/property/bean/property
/bean 为类类型属性赋值方式三级联属性赋值用的极少
bean idstudentFour classcom.softeem.spring.bean.Studentproperty nameid value1004/propertyproperty namename value赵六/propertyproperty nameage value26/propertyproperty namesex value女/property!-- 一定先引用某个bean为属性赋值才可以使用级联方式更新属性 --property nameclazz refclazzOne/propertyproperty nameclazz.clazzId value3333/propertyproperty nameclazz.clazzName value最强王者班/property
/bean 4、为数组类型属性赋值
这个简单只用在property标签中增加array标签即可然后在value属性赋值
property namehobbiesarrayvalue抽烟/valuevalue喝酒/valuevalue烫头/value/array
/property
综上就是Spring框架中IOC容器依赖注入的几种方法。
spring框架概述及第一个入门实验见作者spring框架专栏中https://blog.csdn.net/m0_66111719/article/details/135839708?spm1001.2014.3001.5501