【Spring基础】IOC使用Setter依赖注入

雨点打透心脏的1/2处 2022-06-03 00:50 274阅读 0赞

前言

Github:https://github.com/yihonglei/thinking-in-spring(spring-ioc-xml工程)

如果Spring使用XML配置形式,最常用有两种依赖注入方式:setter注入和构造器注入。

这里主要讨论基于setter方法的依赖注入。

一 Setter注入Bean

1、创建一个HelloService接口

  1. package com.jpeony.spring.common;
  2. /**
  3. * @author yihonglei
  4. */
  5. public interface HelloService {
  6. void sayHello(String name);
  7. }

2、HelloServiceImpl实现类

  1. package com.jpeony.spring.common;
  2. /**
  3. * @author yihonglei
  4. */
  5. public class HelloServiceImpl implements HelloService {
  6. @Override
  7. public void sayHello(String name) {
  8. System.out.println("hello:" + name);
  9. }
  10. }

3、创建一个SelfIntroductionService(自我介绍)接口

  1. package com.jpeony.spring.setter;
  2. /**
  3. * @author yihonglei
  4. */
  5. public interface SelfIntroductionService {
  6. void selfIntroduction();
  7. }

4、SelfIntroductionServiceImpl实现类

  1. package com.jpeony.spring.setter;
  2. import com.jpeony.spring.common.HelloService;
  3. /**
  4. * @author yihonglei
  5. */
  6. public class SelfIntroductionServiceImpl implements SelfIntroductionService {
  7. private HelloService helloService;
  8. /**
  9. * setter方式注入Bean
  10. */
  11. public void setHelloService(HelloService helloService) {
  12. this.helloService = helloService;
  13. }
  14. @Override
  15. public void selfIntroduction() {
  16. // 向大家打招呼
  17. helloService.sayHello("大家好!");
  18. }
  19. }

5、Spring XML配置applicationContext-Setter-Bean.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!--
  7. Bean声明:
  8. 该bean类似于javaConfig中的@Bean注解;
  9. 用于创建bean的类通过class属性来指定,并且需要使用全限定的类名。
  10. 通过id指定bean的ID。如果不显示指定,默认使用class的全限定名进行命名。
  11. eg:
  12. HelloServiceImpl#0,其#0是一个计数器的形式,
  13. 用来区分相同类型的其他bean。
  14. 使用自动化命名很方便,但是没有多少实际用处,还是建议自己给bean显示设定ID。
  15. -->
  16. <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>
  17. <!-- setter注入bean -->
  18. <bean id="selfIntroductionService" class="com.jpeony.spring.setter.SelfIntroductionServiceImpl">
  19. <property name="helloService" ref="helloService"/>
  20. </bean>
  21. </beans>

6. 测试setter注入Bean

  1. package com.jpeony.spring;
  2. import com.jpeony.spring.setter.SelfIntroductionService;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. /**
  7. * 测试setter注入(bean、字面量、集合)
  8. *
  9. * @author yihonglei
  10. */
  11. public class SetterTest {
  12. /**
  13. * 注入bean
  14. *
  15. * @author yihonglei
  16. */
  17. @Test
  18. public void testBean() {
  19. // 根据spring配置文件创建应用上下文
  20. ApplicationContext context =
  21. new ClassPathXmlApplicationContext("applicationContext-Setter-Bean.xml");
  22. // 从容器中获取bean
  23. SelfIntroductionService selfIntroductionService
  24. = (SelfIntroductionService) context.getBean("selfIntroductionService");
  25. // 调用自我介绍
  26. selfIntroductionService.selfIntroduction();
  27. }
  28. }

7. 运行结果

hello:大家好!

总结:

自我介绍类中SelfIntroductionServiceImpl,通过set方法注入bean,

在XML中通过指明依赖关系。

二 Setter方法注入字面量

setter方法除了上面应用于注入bean之外,还可以用于注入字面量。

1、创建一个实体类

  1. package com.jpeony.spring.entity;
  2. /**
  3. * 个人信息(setter注入使用)
  4. *
  5. * @author yihonglei
  6. */
  7. public class PersonSetter {
  8. /**
  9. * 姓名
  10. */
  11. private String name;
  12. /**
  13. * 性别
  14. */
  15. private String sex;
  16. /**
  17. * 年龄
  18. */
  19. private int age;
  20. /**
  21. * 兴趣爱好
  22. */
  23. private String hobby;
  24. public String getName() {
  25. return name;
  26. }
  27. public void setName(String name) {
  28. this.name = name;
  29. }
  30. public String getSex() {
  31. return sex;
  32. }
  33. public void setSex(String sex) {
  34. this.sex = sex;
  35. }
  36. public int getAge() {
  37. return age;
  38. }
  39. public void setAge(int age) {
  40. this.age = age;
  41. }
  42. public String getHobby() {
  43. return hobby;
  44. }
  45. public void setHobby(String hobby) {
  46. this.hobby = hobby;
  47. }
  48. }

2、spring配置applicationContext-Setter-Constant.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <!-- setter注入字面量 -->
  6. <bean id="person" class="com.jpeony.spring.entity.Person">
  7. <property name="name" value="tom"/>
  8. <property name="sex" value="男"/>
  9. <property name="age" value="26"/>
  10. <property name="hobby" value="爱好女人,哈哈!开个玩笑!"/>
  11. </bean>
  12. </beans>

3、测试setter注入字面量

  1. package com.jpeony.spring;
  2. import com.jpeony.spring.entity.PersonSetter;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. /**
  7. * 测试setter注入(bean、字面量、集合)
  8. *
  9. * @author yihonglei
  10. */
  11. public class SetterTest {
  12. /**
  13. * 注入字面量
  14. *
  15. * @author yihonglei
  16. */
  17. @Test
  18. public void testConstant() {
  19. // 根据spring配置文件创建应用上下文
  20. ApplicationContext context =
  21. new ClassPathXmlApplicationContext("applicationContext-Setter-Constant.xml");
  22. // 从容器中获取bean
  23. PersonSetter person = (PersonSetter) context.getBean("personSetter");
  24. // 打印个人属性
  25. System.out.println(" 姓名: " + person.getName() +
  26. " 性别: " + person.getSex() +
  27. " 年龄: " + person.getAge() +
  28. " 兴趣爱好: " + person.getHobby());
  29. }
  30. }

4、运行结果

姓名: tom 性别: 男 年龄: 26 兴趣爱好: 爱好女人,哈哈!开个玩笑!

三 Setter方法注入集合

setter方法还可以用来注入集合,直接看看实例。

1、创建集合类

  1. package com.jpeony.spring.entity;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Set;
  6. /**
  7. * Spring 注入集合
  8. *
  9. * @author yihonglei
  10. */
  11. public class Collection {
  12. private Set<String> set;
  13. private List<String> list;
  14. private Map<String, String> map;
  15. private String[] array;
  16. public Set<String> getSet() {
  17. return set;
  18. }
  19. public void setSet(Set<String> set) {
  20. this.set = set;
  21. }
  22. public List<String> getList() {
  23. return list;
  24. }
  25. public void setList(List<String> list) {
  26. this.list = list;
  27. }
  28. public Map<String, String> getMap() {
  29. return map;
  30. }
  31. public void setMap(Map<String, String> map) {
  32. this.map = map;
  33. }
  34. public String[] getArray() {
  35. return array;
  36. }
  37. public void setArray(String[] array) {
  38. this.array = array;
  39. }
  40. @Override
  41. public String toString() {
  42. return "Collection{" +
  43. " set=" + set +
  44. ", list=" + list +
  45. ", map=" + map +
  46. ", array=" + Arrays.toString(array) +
  47. '}';
  48. }
  49. }

2、Spring xml配置applicationContext-Setter-List.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <!-- setter注入集合 -->
  6. <bean id="collection" class="com.jpeony.spring.collection.Collection">
  7. <!-- Set集合的属性注入 -->
  8. <property name="set">
  9. <set>
  10. <value>set1</value>
  11. <value>set2</value>
  12. <value>set3</value>
  13. </set>
  14. </property>
  15. <!-- List集合的属性注入 -->
  16. <property name="list">
  17. <list>
  18. <value>list1</value>
  19. <value>list2</value>
  20. <value>list3</value>
  21. </list>
  22. </property>
  23. <!-- 数组的注入 -->
  24. <property name="array">
  25. <list>
  26. <value>array1</value>
  27. <value>array1</value>
  28. <value>array1</value>
  29. </list>
  30. </property>
  31. <!-- Map集合的属性注入 -->
  32. <property name="map">
  33. <map>
  34. <entry key="key1" value="map1"></entry>
  35. <entry key="key2" value="map2"></entry>
  36. <entry key="key3" value="map3"></entry>
  37. </map>
  38. </property>
  39. </bean>
  40. </beans>

3、测试Setter注入集合

  1. package com.jpeony.spring;
  2. import com.jpeony.spring.entity.Collection;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. /**
  7. * 测试setter注入(bean、字面量、集合)
  8. *
  9. * @author yihonglei
  10. */
  11. public class SetterTest {
  12. /**
  13. * 注入集合
  14. *
  15. * @author yihonglei
  16. */
  17. @Test
  18. public void testList() {
  19. // 根据spring配置文件创建应用上下文
  20. ApplicationContext context =
  21. new ClassPathXmlApplicationContext("applicationContext-Setter-List.xml");
  22. // 从容器中获取bean
  23. Collection collection = (Collection) context.getBean("collection");
  24. // 打印个人属性
  25. System.out.println("Setter注入集合:" + collection.toString());
  26. }
  27. }

4、运行结果

Setter注入集合:Collection{ set=[set1, set2, set3], list=[list1, list2, list3], map={key1=map1, key2=map2, key3=map3}, array=[array1, array1, array1]}

四 总结

Spring setter注入可用于注入bean、字面量、集合;

如果是新项目,建议使用Spring注解,因为一堆一堆的xml不好维护,同时功能也不如注解强大。

发表评论

表情:
评论列表 (有 0 条评论,274人围观)

还没有评论,来说两句吧...

相关阅读