三种依赖注入
这篇博文能够说明三种依赖注入的特点。
下面是对set注入和拓展注入的详细说明:
1.set注入(重点)
pojo包:
Student.java
package com.kuang.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.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
Address.java
package com.kuang.pojo;
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 + '\'' +
'}';
}
}
set的八种注入方式:
<?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="addr" class="com.kuang.pojo.Address">
<property name="address" value="上海市黄浦区"/>
</bean>
<bean id="student" class="com.kuang.pojo.Student">
<!--第一种:普通纸注入,value 重点-->
<property name="name" value="秦师傅"/>
<!--第二种:Bean注入,ref是引用bean的名字,重点-->
<property name="address" ref="addr"/>
<!--第三种:数组注入-->
<property name="books">
<array>
<value>骆驼祥子</value>
<value>鳄鱼君</value>
<value>红楼梦</value>
</array>
</property>
<!--第四种:List注入-->
<property name="hobbys">
<list>
<value>健身</value>
<value>看书</value>
<value>美食</value>
</list>
</property>
<!--第五种:Map注入-->
<property name="card">
<map>
<entry key="身份证" value="123442324444"/>
<entry key="信用卡" value="4789876789078"/>
<entry key="储蓄卡" value="9999999999999"/>
</map>
</property>
<!--第六种:set注入-->
<property name="games">
<set>
<value>跑步</value>
<value>俯卧撑</value>
<value>深蹲</value>
</set>
</property>
<!--第七种:Null注入-->
<property name="wife">
<null/>
</property>
<!--第八种:Properties注入-->
<property name="info">
<props>
<prop key="学号">344222</prop>
<prop key="性别">男</prop>
<prop key="url">www.fefe.com</prop>
</props>
</property>
</bean>
</beans>
测试类:
@Test
public void testOne() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
2.拓展注入
User.java
package com.kuang.pojo;
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
userBeans.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(properties)命名空间:属性依然要设置set方法-->
<bean id="user" class="com.kuang.pojo.User" p:name="老舍" p:age="99"/>
<!--c(构造:Constructor)命名空间:属性依然要设置set方法-->
<bean id="user2" class="com.kuang.pojo.User" c:name="刘海鹰" c:age="57"/>
</beans>
测试类:
/*拓展注入的测试(p和c)*/
@Test
public void testTwo() {
ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
//User user = context.getBean("user", User.class);
User user = context.getBean("user2", User.class);
System.out.println(user.toString());
}