依赖注入
声明:本文章属于学习笔记,根据狂神说的Spring编写
Spring官方文档:Spring官方文档
一丶依赖注入:
依赖注入(Dependency Injection),是这样一个过程:由于某客户类只依赖于服务类的一个接口,而不依赖于具体服务类,所以客户类只定义一个注入点。在程序运行过程中,客户类不直接实例化具体服务类实例,而是客户类的运行上下文环境或专门组件负责实例化服务类,然后将其注入到客户类中,保证客户类的正常运行。
二丶DI依赖注入环境
首先我们看这两个实体类:
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
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.toString() +
", 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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="stu" class="com.kdy.pojo.Student">
<property name="name" value="kdy"/>
</bean>
</beans>
测试类测试:
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student stu = (Student)context.getBean("stu");
System.out.println(stu.getName());
}
}
运行结果:
三丶依赖注入之set注入
我们还是来用student类来进行set的注入。
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.kdy.pojo.Address"></bean>
<bean id="stu" class="com.kdy.pojo.Student">
<property name="name" value="kdy"/>
<property name="address" ref="address"/>
<!--数组-->
<property name="books">
<array>
<value>西游记</value>
<value>红楼梦</value>
</array>
</property>
<!--list-->
<property name="hobbys">
<list>
<value>抽烟</value>
<value>喝酒</value>
<value>烫头</value>
<value>打游戏</value>
</list>
</property>
<property name="card">
<map>
<entry key="姓名" value="kdy" />
<entry key="学号" value="041940223" />
</map>
</property>
<property name="games">
<set>
<value>lol</value>
<value>cf</value>
<value>dnf</value>
</set>
</property>
<property name="wife">
<null/>
</property>
<property name="info">
<props>
<prop key="name" > kdy</prop>
<prop key="password" > kdy1</prop>
<prop key="lll" > kdy2</prop>
</props>
</property>
</bean>
</beans>
测试类测试:
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student stu = (Student)context.getBean("stu");
System.out.println(stu.toString());
}
运行结果:
Student{name=‘kdy’, address=Address{address=‘null’}, books=[西游记, 红楼梦], hobbys=[抽烟, 喝酒, 烫头, 打游戏], card={姓名=kdy, 学号=041940223}, games=[lol, cf, dnf], wife=‘null’, info={password=kdy1, name=kdy, lll=kdy2}}
四丶c命名和p命名空间注入
首先如果我们要使用这个c命名或者是p命名空间注入,那么就必须要引入标签连接:
xmlns:p=“http://www.springframework.org/schema/p”
xmlns:c=“http://www.springframework.org/schema/c”
userbeans.xml文件配置:
运行结果: