Spring依赖注入
1. 构造器注入
之前写过:Spring中IOC创建对象的方式
2. Set注入(重点)
依赖注入:set注入(核心)
依赖:bean对象的创建依赖于容器
注入:bean对象中的所有属性,由容器来注入
【环境搭建】
- 复杂类型
package com.wbx.Student;/*
*@author 王炳祥
*@createTime
*/
public class Address {
private String adderss;
public String getAdderss() {
return adderss;
}
public void setAdderss(String adderss) {
this.adderss = adderss;
}
}
- 真实测试对象
package com.wbx.Student;/*
*@author 王炳祥
*@createTime
*/
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> cards;
private Set<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCards() {
return cards;
}
public void setCards(Map<String, String> cards) {
this.cards = cards;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", cards=" + cards +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
- beans.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.wbx.Student.Address">
<property name="adderss" value="西安"/>
</bean>
<bean id="student" class="com.wbx.Student.Student">
<!--第一种,普通注入,value-->
<property name="name" value="小余"/>
<!--第二种,bean注入,ref-->
<property name="address" ref="address"/>
<!--组数注入-->
<property name="books">
<array>
<value>高数</value>
<value>高数2</value>
</array>
</property>
<!--list-->
<property name="hobbys">
<list>
<value>唱</value>
<value>跳</value>
</list>
</property>
<!--map-->
<property name="cards">
<map>
<entry key="身份证" value="6666666"/>
<entry key="身份证2" value="5555555"/>
</map>
</property>
<!--set-->
<property name="games">
<set>
<value>lol</value>
<value>ow</value>
</set>
</property>
<!--Null-->
<property name="wife">
<null></null>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="学号">201606014</prop>
<prop key="性别">男</prop>
</props>
</property>
</bean>
</beans>
- 测试类
import com.wbx.Student.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mytest {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
}
}
3. 其他注入
我们可以使用p/c命名空间进行主注入
官方解释:
实体类:
package com.wbx.Student;/*
*@author 王炳祥
*@createTime
*/
public class User {
private String uname;
private int age;
//使用c命名空间,必须要有有参构造器,当然也必须要有无参构造器
public User(String uname, int age) {
this.uname = uname;
this.age = age;
}
public User() {
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"uname='" + uname + '\'' +
", age=" + age +
'}';
}
}
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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--上面引用了带p命名空间的XML快捷方式-->
<!--p命名空间注入,可以直接注入属性的值-->
<bean id="user" class="com.wbx.Student.User" p:uname="喜欢小余" p:age="1314"/>
<!--上面引用了带c命名空间的XML快捷方式-->
<!--c命名空间通过构造器注入-->
<bean id="user2" class="com.wbx.Student.User" c:uname="小余" c:age="520"/>
</beans>
测试类:
@Test
public void testuser(){
ApplicationContext context = new ClassPathXmlApplicationContext("Userbeans.xml");
//此处的反射声明了类型,则不需要在进行强转
User user2 = context.getBean("user2", User.class);
User user = context.getBean("user", User.class);
System.out.println(user.toString());
System.out.println(user2.toString());
}
注意:p/c命名空间不能直接使用,需要导入xml约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"