DI注入
1 2 3 public class Address { 4 private String address; 5 6 public String getAddress() { 7 return address; 8 } 9 10 public void setAddress(String address) { 11 this.address = address; 12 } 13 14 @Override 15 public String toString() { 16 return "Address{" + 17 "address='" + address + '\'' + 18 '}'; 19 } 20 }
1 public class Age { 2 private int age; 3 4 public int getAge() { 5 return age; 6 } 7 8 public void setAge(int age) { 9 this.age = age; 10 } 11 12 @Override 13 public String toString() { 14 return "Age{" + 15 "age=" + age + 16 '}'; 17 } 18 }
1 public class Student { 2 private String name; 3 /*自动装配*/ 4 // @Autowired 5 private Address address; 6 // @Autowired 7 private Age age; 8 private String[] books; 9 private List<String> hobbys; 10 private Map<String,String> card; 11 private Set<String> games; 12 private String wife; 13 private Properties info; 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public Address getAddress() { 24 return address; 25 } 26 27 public void setAddress(Address address) { 28 this.address = address; 29 } 30 31 public Age getAge() { 32 return age; 33 } 34 35 public void setAge(Age age) { 36 this.age = age; 37 } 38 39 public String[] getBooks() { 40 return books; 41 } 42 43 public void setBooks(String[] books) { 44 this.books = books; 45 } 46 47 public List<String> getHobbys() { 48 return hobbys; 49 } 50 51 public void setHobbys(List<String> hobbys) { 52 this.hobbys = hobbys; 53 } 54 55 public Map<String, String> getCard() { 56 return card; 57 } 58 59 public void setCard(Map<String, String> card) { 60 this.card = card; 61 } 62 63 public Set<String> getGames() { 64 return games; 65 } 66 67 public void setGames(Set<String> games) { 68 this.games = games; 69 } 70 71 public String getWife() { 72 return wife; 73 } 74 75 public void setWife(String wife) { 76 this.wife = wife; 77 } 78 79 public Properties getInfo() { 80 return info; 81 } 82 83 public void setInfo(Properties info) { 84 this.info = info; 85 } 86 87 @Override 88 public String toString() { 89 return "Student{" + 90 "name='" + name + '\'' + 91 ", address=" + address + 92 ", age=" + age + 93 ", books=" + Arrays.toString(books) + 94 ", hobbys=" + hobbys + 95 ", card=" + card + 96 ", games=" + games + 97 ", wife='" + wife + '\'' + 98 ", info=" + info + 99 '}'; 100 } 101 }
applicationContext.xml 需要加
1 http://www.springframework.org/schema/context 2 http://www.springframework.org/schema/context/spring-context.xsd
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 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 https://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> 9 <!--扫描这个包下的类的注解 要在你要 加上注解 Autowired--> 10 <context:component-scan base-package="com.rzk.pojo"/> 11 <bean id="address" class="com.rzk.pojo.Address"> 12 <property name="address" value="中国社区"/> 13 </bean> 14 15 <bean id="age" class="com.rzk.pojo.Age"> 16 <property name="age" value="18"/> 17 </bean> 18 19 <bean id="student" class="com.rzk.pojo.Student"> 20 <!--第一种普通值注入--> 21 <property name="name" value="小明"/> 22 <!--bean注入 ref--> 23 <property name="address" ref="address"/> 24 <property name="age" ref="age"/> 25 26 <!--数组注入--> 27 <property name="books"> 28 <array> 29 <value>西游记1</value> 30 <value>西游记2</value> 31 <value>西游记3</value> 32 <value>西游记4</value> 33 </array> 34 </property> 35 <!--list 注入 --> 36 <property name="hobbys"> 37 <list> 38 <value>看电影</value> 39 <value>看电影</value> 40 <value>看电影</value> 41 </list> 42 </property> 43 <!--Map--> 44 <property name="card"> 45 <map> 46 <entry value="1" key="A"/> 47 <entry value="2" key="B"/> 48 <entry value="3" key="C"/> 49 </map> 50 </property> 51 <!--Set--> 52 <property name="games"> 53 <set> 54 <value>lol</value> 55 <value>开门狗</value> 56 <value>刺客信条</value> 57 </set> 58 </property> 59 <!--null--> 60 <property name="wife"> 61 <null/> 62 </property> 63 <!--properties--> 64 <property name="info"> 65 <props> 66 <prop key="driver">连接</prop> 67 <prop key="root">用户</prop> 68 <prop key="pwd">密码</prop> 69 </props> 70 </property> 71 </bean> 72 </beans>
测试
1 public class Mytest { 2 public static void main(String[] args) { 3 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 4 Student student = (Student) context.getBean("student"); 5 System.out.println(student); 6 7 } 8 }
--------------------------------------------------
加上自动装配 需要在xml添加
在xml配置箭头所指的
加上注解的话可以把xml配置ref bean注释掉
注解就 替换了xml的配置
/*autowired 默认是byType*/
autowire="byType"
autowire="byName"
如果设置了byName 的话会直接找 id
如果设置了byType 的话会直接找 class属性所对应的类名
@Autowired :自动转配通过类型,名字
如果Autowired不能通过唯一自动装配上属性,如果想要用byName,则需要通过 @Qualifier(value = "xxx")