Spring(8):构造方法注入与示例
来源:互联网 发布:制造图片的软件 编辑:程序博客网 时间:2024/06/05 07:28
2017/12/24
spring的两种依赖注入方式:setter注入与构造方法注入,这两种方法的不同主要就是在xml文件下对应使用property和constructor-arg属性, 下面主要介绍第二种IOC(DI)方法:
使用构造方法注入,理所当然的要确保javaBean中要有一个形参赋值的构造函数。
一、示例1,创建一个Id类:
package com.loster.li; public class Id { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
(2)创建配置文件Id_Bean.xml
<?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-2.5.xsd"> <bean id="id" class="com.loster.li.Id"> <property name="id" value="123"></property> <property name="name" value="xiaoli"></property> </bean> </beans>
(3)编写测试类IdTest.java,并运行:
package com.loster.li; import org.springframework.context.support.ClassPathXmlApplicationContext; public class IdTest { public static void main(String[] args){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/loster/li/Id_Bean.xml"); Id id = (Id)context.getBean("id"); System.out.println(id.getId()); System.out.println(id.
二、示例2,多重注入。
需求;
图1
(1) 新建两个类,Person.java:name和word属性,必须有带参数的构造方法;
PersonDao.java:Person属性,同样必须有带参数的构造方法;
Person.java:
package com.spring.IOC.test;public class Person {private String name;private String word;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getWord() {return word;}public void setWord(String word) {this.word = word;}public Person(String name, String word) {this.name = name;this.word = word;}public Person() {}@Overridepublic String toString() {return "Person [name=" + name + ", word=" + word + "]";}}
PersonDao.java:
package com.spring.IOC.test;public class PersonDao {private Person person;public PersonDao(Person person){this.person = person;}public Person getPerson() {return person;}public void setPerson(Person person) {this.person = person;}public void speak(){System.out.println(person.getName()+" : "+person.getWord());}}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <bean id="Person1" class="com.spring.IOC.test.Person"><constructor-arg name="name" value="zhangga"></constructor-arg><constructor-arg name="word" value="三天不打小鬼子,手都痒痒!"></constructor-arg></bean><bean id="Person2" class="com.spring.IOC.test.Person"><constructor-arg name="name" value="Rod"></constructor-arg><constructor-arg name="word" value="世界上有10种,认识二进制和不认识二进制的。"></constructor-arg></bean><bean id="PersonDao1" class="com.spring.IOC.test.PersonDao"><constructor-arg><ref bean="Person1"/></constructor-arg></bean><bean id="PersonDao2" class="com.spring.IOC.test.PersonDao"><constructor-arg><ref bean="Person2"/></constructor-arg></bean></beans>
(3)编写单元测试 PersonDaoTest.java,直接获取了两个对象 personDao1、 personDao2,而Person1、Person2两个对象已经通过spring框架,自动的注入到personDao1、 personDao2了:
package com.spring.IOC.test;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;public class PersonDaoTest {@Testpublic void test() {//Person zhangga = new Person("zhangga","三天不打小鬼子,手都痒痒!");//Person Rod = new Person("Rod","世界上有10种,认识二进制和不认识二进制的。");ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");PersonDao personDao1 = (PersonDao) context.getBean("PersonDao1");PersonDao personDao2 = (PersonDao) context.getBean("PersonDao2");personDao1.speak();personDao2.speak();}}
输出结果:
zhangga : 三天不打小鬼子,手都痒痒!Rod : 世界上有10种,认识二进制和不认识二进制的。
PS:
那么,两种注入方式(setter方法注入、构造方法注入)有哪些区别呢?下面做简单比较:
在过去的开发过程中,这两种注入方式都是非常常用的。Spring也同时支持两种依赖注入方式:设值注入和构造注入。 这两种依赖注入的方式,并没有绝对的好坏,只是适应的场景有所不同。相比之下,设值注入有如下优点:
- 设值注入需要该Bean包含这些属性的setter方法
- 与传统的JavaBean的写法更相似,程序开发人员更容易理解、接收。通过setter方法设定依赖关系显得更加只管。
- 对于复杂的依赖关系,如果采用构造注入,会导致构造器国语臃肿,难以阅读。Spring在创建Bean实例时,需要同时实例化器依赖的全部实例,因而导致性能下降。而使用设值注入,则能避免这些问题
- 尤其是在某些属性可选的情况况下,多参数的构造器显得更加笨重
构造注入也不是绝对不如设值注入,在某些特定的场景下,构造注入比设值注入更加优秀。构造注入有以下优势:
- 构造注入需要该Bean包含带有这些属性的构造器
- 构造注入可以在构造器中决定依赖关系的注入顺序,优先依赖的优先注入。例如,组件中其他依赖关系的注入,常常要依赖于DataSrouce的注入。采用构造注入,可以在代码中清晰的决定注入顺序。
- 对于依赖关系无需变化的Bean,构造注入更有用处。因为没有Setter方法,所有的依赖关系全部在构造器内设定。因此,无需担心后续的代码对依赖关系产生破坏。
- 依赖关系只能在构造器中设定,则只有组件的创建者才能改变组件的依赖关系。对组件的调用者而言,组件内部的依赖关系完全透明,更符合高内聚的原则。
建议:采用以设值注入为主,构造注入为辅的注入策略。对于依赖关系无需变化的注入,尽量采用构造注入;而其他的依赖关系的注入,则考虑采用设值注入。
阅读全文