基于set方式的依赖注入

打印 上一主题 下一主题

主题 1018|帖子 1018|积分 3054

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
基于set方式的依赖注入

概述

依赖注入的方式分为基于构造器的注入方式和基于set的注入方式,而基于set方式的依赖注入是是依赖注入的核心,本文详细讲解多种类型的数据注入的方式,包括普通注入,bean注入,array|list|map|set|props|null
更详尽内容请查看官网
实践

地址类,用于测试bean注入
  1. package com.kuangstudy.di;
  2. /**
  3. * 功能描述
  4. *
  5. * @since 2022-06-25
  6. */
  7. public class Address {
  8.     private String address;
  9.     public String getAddress() {
  10.         return address;
  11.     }
  12.     public void setAddress(String address) {
  13.         this.address = address;
  14.     }
  15.     @Override
  16.     public String toString() {
  17.         return "Address{" +
  18.                 "address='" + address + '\'' +
  19.                 '}';
  20.     }
  21. }
复制代码
学生类,用于测试全部类型
  1. package com.kuangstudy.di;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Properties;
  6. import java.util.Set;
  7. /**
  8. * 功能描述
  9. *
  10. * @since 2022-06-25
  11. */
  12. public class Student {
  13.     private String name;
  14.     private Address address;
  15.     private String[] books;
  16.     private List<String> hobbies;
  17.     private Map<String, String> card;
  18.     private Set<String> games;
  19.     private String wife;
  20.     private Properties info;
  21.     public String getName() {
  22.         return name;
  23.     }
  24.     public void setName(String name) {
  25.         this.name = name;
  26.     }
  27.     public Address getAddress() {
  28.         return address;
  29.     }
  30.     public void setAddress(Address address) {
  31.         this.address = address;
  32.     }
  33.     public String[] getBooks() {
  34.         return books;
  35.     }
  36.     public void setBooks(String[] books) {
  37.         this.books = books;
  38.     }
  39.     public List<String> getHobbies() {
  40.         return hobbies;
  41.     }
  42.     public void setHobbies(List<String> hobbies) {
  43.         this.hobbies = hobbies;
  44.     }
  45.     public Map<String, String> getCard() {
  46.         return card;
  47.     }
  48.     public void setCard(Map<String, String> card) {
  49.         this.card = card;
  50.     }
  51.     public String getWife() {
  52.         return wife;
  53.     }
  54.     public void setWife(String wife) {
  55.         this.wife = wife;
  56.     }
  57.     public Properties getInfo() {
  58.         return info;
  59.     }
  60.     public void setInfo(Properties info) {
  61.         this.info = info;
  62.     }
  63.     public Set<String> getGames() {
  64.         return games;
  65.     }
  66.     public void setGames(Set<String> games) {
  67.         this.games = games;
  68.     }
  69.     @Override
  70.     public String toString() {
  71.         return "Student{" +
  72.                 "name='" + name + '\'' +
  73.                 ", address=" + address +
  74.                 ", books=" + Arrays.toString(books) +
  75.                 ", hobbies=" + hobbies +
  76.                 ", card=" + card +
  77.                 ", games=" + games +
  78.                 ", wife='" + wife + '\'' +
  79.                 ", info=" + info +
  80.                 '}';
  81.     }
  82. }
复制代码
测试类,用于执行main函数测试
  1. import org.springframework.context.ApplicationContext;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. import com.kuangstudy.di.Student;
  4. /**
  5. * 功能描述
  6. *
  7. * @since 2022-06-25
  8. */
  9. public class Test01 {
  10.     public static void main(String[] args) {
  11.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
  12.         Student s1 = (Student) applicationContext.getBean("s1");
  13.         System.out.println(s1);
  14.     }
  15. }
复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  4.   https://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="address1" >
  6.         <property name="address" value="xi'an"></property>
  7.     </bean>
  8.     <bean id="s1" >
  9.         <property name="name" value="xiaoming"></property>
  10.         <property name="address" ref="address1"></property>
  11.         <property name="books">
  12.             <array>
  13.                 <value>西游记</value>
  14.                 <value>红楼梦</value>
  15.                 <value>三国演义</value>
  16.                 <value>水浒传</value>
  17.             </array>
  18.         </property>
  19.         <property name="hobbies">
  20.             <list>
  21.                 <value>swimming</value>
  22.                 <value>painting</value>
  23.                 <value>playing basketball</value>
  24.             </list>
  25.         </property>
  26.         <property name="card">
  27.             <map>
  28.                 <entry key="Student card" value="000010"></entry>
  29.                 <entry key="Id card" value="xxxxxxxxxxxx"></entry>
  30.                 <entry key="Bank card" value="xxxxxxxxxxxxxxxx"></entry>
  31.             </map>
  32.         </property>
  33.         <property name="games">
  34.             <set>
  35.                 <value>LOL</value>
  36.                 <value>COC</value>
  37.                 <value>BOB</value>
  38.             </set>
  39.         </property>
  40.         <property name="wife">
  41.             <null></null>
  42.         </property>
  43.         <property name="info">
  44.             <props>
  45.                 <prop key="username">root</prop>
  46.                 <prop key="password">123</prop>
  47.             </props>
  48.         </property>
  49.     </bean>
  50. </beans>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

天津储鑫盛钢材现货供应商

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表