依赖注入
- 构造器注入
构造器注入呢在前面的案例中就有讲解今天呢主要去讲set注入。
- set方式注入(重点)
-
依赖:bean对象的创建依赖于容器!
-
注入:bean对象中的所有属性,由容器来注入!
-
【环境搭建】
复杂类型
package com.cloud.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
真实测试对象
package com.cloud.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
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> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
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 +
", card=" + card +
", 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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.cloud.pojo.Student">
<!--第一种,普通注入,value一个值-->
<property name="name" value="陈狗蛋"/>
</bean>
</beans>
测试类
import com.cloud.pojo.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());
}
}
完善注入信息
<?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.xsd">
<bean id="address" class="com.cloud.pojo.Address">
<property name="address" value="拉跨"/>
</bean>
<bean id="student" class="com.cloud.pojo.Student">
<!--第一种,普通注入,value一个值-->
<property name="name" value="陈狗蛋"/>
<!--第二种,bean注入-->
<property name="address" ref="address"/>
<!--数组-->
<property name="books">
<array>
<value>红楼们</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<!--List-->
<property name="hobbys">
<list>
<value>看电影</value>
<value>打游戏</value>
<value>听歌</value>
<value>干饭</value>
</list>
</property>
<!--Map-->
<property name="card">
<map>
<entry key="身份证" value="15645364896489168"/>
<entry key="银行卡" value="05641+69549859695"/>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!--null-->
<property name="wife">
<null/>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="driver">201925040</prop>
<prop key="url">男</prop>
<prop key="username">赵狗蛋</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
- 拓展方式注入
我们可以使用p命令空间和c命令空间进行注入
官方解释:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
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.xsd">
<!--p命名空间,可以直接注入属性的值:property-->
<bean id="user" class="com.cloud.pojo.User" p:name="陈狗蛋" p:age="58"/>
<!--p命名空间,可以直接注入属性的值:property-->
<bean id="user2" class="com.cloud.pojo.User" c:name="王狗蛋" c:age="65"/>
</beans>
测试:
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = context.getBean("user2", User.class);
System.out.println(user);
}
注意点:p命名空间和c命名空间不能直接使用,需要导入xml约束!
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
使用这种注入方式呢还需要注意两个点使用p呢就必须要有无参构造器c呢需要有参构造器 如果两个一起测试呢则都需要去创建,因为一旦创建有参构造器后无参构造器就会消失。