在Spring中实现IoC容器的方法是依赖注入,依赖注入的做用是在使用Spring框架建立对象时动态地将其所依赖的对象(例如属性值)注入Bean组件中。java
Spring框架的依赖注入一般有两种实现方式,一种是使用构造方法注入,另外一种是使用属性的setter方法注入。spring
使用构造方法注入
Spring框架能够采用Java反射机制,经过构造方法完成依赖注入。api
建立项目及导入Maven模块过程请看《使用IDEA开发Spring入门程序》,在这就不赘述了。在这继续前面的项目,按照下面的步骤补充:app
建立entity包,建立Person类框架
package entity; public class Person { private String name; private String sex; public Person() { System.out.println("无参构造调用了..."); } public Person(String name, String sex) { this.name = name; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
构造方法注入方式一
编写配置文件ide
在src根目录下建立Spring配置文件applicationContext.xml。在配置文件中首先将entity.Person类托管给Spring,让Spring建立其对象,同时给构造方法传递实参。测试
配置文件的具体代码以下:this
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--将指定类TestDaoImpl配置给Spring,即注册一个TestDaoImpl对象,让Spring建立其实例--> <!-- 一个Bean标签能够注册一个组件(对象、类) class:写要注册的组件的全类名 id:这个对象的惟一标识 --> <bean id="test" class="dao.TestDaoImpl"/> <bean id="person1" class="entity.Person"/> <!--使用构造方法注入--> <bean id="person2" class="entity.Person"> <!--使用有参构造器进行建立对象并赋值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <constructor-arg name="name" value="泰斗贤若如"></constructor-arg> <constructor-arg name="sex" value="男"></constructor-arg> </bean> </beans>
在测试类TestDemo中测试3d
package test; import dao.TestDao; import entity.Person; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.FileSystemResource; public class TestDemo { @Test public void test4(){ //初始化spring容器ApplicationContext,加载配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //经过容器获取test实例 Person person2 =(Person) applicationContext.getBean("person2"); System.out.println("姓名:"+person2.getName()+";"+"性别:"+person2.getSex()); } }
测试结果
构造方法注入方式二
编写配置文件code
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--将指定类TestDaoImpl配置给Spring,即注册一个TestDaoImpl对象,让Spring建立其实例--> <!-- 一个Bean标签能够注册一个组件(对象、类) class:写要注册的组件的全类名 id:这个对象的惟一标识 --> <bean id="test" class="dao.TestDaoImpl"/> <bean id="person1" class="entity.Person"/> <!--使用构造方法注入--> <bean id="person2" class="entity.Person"> <!--使用有参构造器进行建立对象并赋值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <constructor-arg name="name" value="泰斗贤若如"></constructor-arg> <constructor-arg name="sex" value="男"></constructor-arg> </bean> <!--能够省略name属性,严格按照构造器参数的位置赋值--> <bean id="person3" class="entity.Person"> <!--使用有参构造器进行建立对象并赋值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <constructor-arg value="泰斗贤若如"></constructor-arg> <constructor-arg value="男"></constructor-arg> </bean> </beans>
在测试类TestDemo中测试
package test; import dao.TestDao; import entity.Person; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.FileSystemResource; public class TestDemo { @Test public void test5(){ //初始化spring容器ApplicationContext,加载配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //经过容器获取test实例 Person person3 =(Person) applicationContext.getBean("person3"); System.out.println("姓名:"+person3.getName()+";"+"性别:"+person3.getSex()); } }
测试结果
须要注意的是,若是使用这种方法,要严格按照构造器参数的位置赋值,若是不这样赋值,固然也不会报错,但会形成赋值错乱,好比会把姓名赋值成性别,这固然是咱们不肯意看到的,若是你非不按要求赋值(有点极端,皮一下),有种方法能够避免你赋值错乱,请看下面代码:
编写配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--将指定类TestDaoImpl配置给Spring,即注册一个TestDaoImpl对象,让Spring建立其实例--> <!-- 一个Bean标签能够注册一个组件(对象、类) class:写要注册的组件的全类名 id:这个对象的惟一标识 --> <bean id="test" class="dao.TestDaoImpl"/> <bean id="person1" class="entity.Person"/> <!--使用构造方法注入--> <bean id="person2" class="entity.Person"> <!--使用有参构造器进行建立对象并赋值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <constructor-arg name="name" value="泰斗贤若如"></constructor-arg> <constructor-arg name="sex" value="男"></constructor-arg> </bean> <!--能够省略name属性,严格按照构造器参数的位置赋值--> <bean id="person3" class="entity.Person"> <!--使用有参构造器进行建立对象并赋值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <constructor-arg value="泰斗贤若如"></constructor-arg> <constructor-arg value="男"></constructor-arg> </bean> <bean id="person4" class="entity.Person"> <!--使用有参构造器进行建立对象并赋值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <!--index="1",为参数指定索引,从0开始--> <constructor-arg value="男" index="1"></constructor-arg> <constructor-arg value="泰斗贤若如" ></constructor-arg> </bean> </beans>
在测试类TestDemo中测试
package test; import dao.TestDao; import entity.Person; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.FileSystemResource; public class TestDemo { @Test public void test5(){ //初始化spring容器ApplicationContext,加载配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //经过容器获取test实例 Person person4 =(Person) applicationContext.getBean("person4"); System.out.println("姓名:"+person4.getName()+";"+"性别:"+person4.getSex()); } }
测试结果
不要觉得这样就完了,我在想,若是出现重载的状况,该如何办?且看我向下分解:
将entity包下的Person类修改以下
package entity; public class Person { private String name; private String sex; private Integer age; private String email; public Person() { System.out.println("无参构造调用了..."); System.out.println("Person建立了..."); } public Person(String name, String sex) { this.name = name; this.sex = sex; System.out.println("有参构造器"); } public Person(String name, String sex, Integer age) { this.name = name; this.sex = sex; this.age = age; } public Person(String name, String sex, String email) { this.name = name; this.sex = sex; this.email = email; } public String getName() { return name; } public void setName(String 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; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
编写配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--将指定类TestDaoImpl配置给Spring,即注册一个TestDaoImpl对象,让Spring建立其实例--> <!-- 一个Bean标签能够注册一个组件(对象、类) class:写要注册的组件的全类名 id:这个对象的惟一标识 --> <bean id="test" class="dao.TestDaoImpl"/> <bean id="person1" class="entity.Person"/> <!--使用构造方法注入--> <bean id="person2" class="entity.Person"> <!--使用有参构造器进行建立对象并赋值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <constructor-arg name="name" value="泰斗贤若如"></constructor-arg> <constructor-arg name="sex" value="男"></constructor-arg> </bean> <!--能够省略name属性,严格按照构造器参数的位置赋值--> <bean id="person3" class="entity.Person"> <!--使用有参构造器进行建立对象并赋值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <constructor-arg value="泰斗贤若如"></constructor-arg> <constructor-arg value="男"></constructor-arg> </bean> <bean id="person4" class="entity.Person"> <!--使用有参构造器进行建立对象并赋值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <!--index="1",为参数指定索引,从0开始--> <constructor-arg value="男" index="1"></constructor-arg> <constructor-arg value="泰斗贤若如" ></constructor-arg> </bean> <bean id="person5" class="entity.Person"> <!--使用有参构造器进行建立对象并赋值--> <!--public Person(String name, String sex, Integer age) { this.name = name; this.sex = sex; this.age = age; } public Person(String name, String sex, String email) { this.name = name; this.sex = sex; this.email = email; } --> <!--重载的状况下type能够指定参数的类型--> <constructor-arg value="男" index="1"></constructor-arg> <constructor-arg value="泰斗贤若如" index="0"></constructor-arg> <constructor-arg value="22" index="2" type="java.lang.Integer"></constructor-arg> </bean> </beans>
在测试类TestDemo中测试
package test; import dao.TestDao; import entity.Person; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.FileSystemResource; public class TestDemo { @Test public void test6(){ //初始化spring容器ApplicationContext,加载配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //经过容器获取test实例 Person person5 =(Person) applicationContext.getBean("person5"); System.out.println("姓名:"+person5.getName()+";"+"性别:"+person5.getSex()+";"+"年龄:"+person5.getAge()); } }
测试结果
不过话又说过来了,明明name能搞定的事情弄这么复杂干吗,因此经常使用的仍是方式一
使用属性的setter方法注入这部分放到下一篇讲解吧,篇幅有点多了,请持续关注!