1. 构造器注入
- 略(前面已写)
2. set方式注入【重点】
- 依赖注入:Set注入(实体类里的set方法)
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象所有属性 , 由容器来注入
实测:
- Student.java
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> game;
private Properties info;
private String wife;
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> getGame() {
return game;
}
public void setGame(Set<String> game) {
this.game = game;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", game=" + game +
", info=" + info +
", wife='" + wife + '\'' +
'}';
}
}
- Address.java
public class Address {
private String address;
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
}
- applicationContext.xml:
<bean id="address" class="com.qk.pojo.Address">
<property name="address" value="湖北"></property>
</bean>
<bean id="student" class="com.qk.pojo.Student">
<!-- 第一种:普通注入,value-->
<property name="name" value="qk"></property>
<!-- 第二种:bean注入,ref-->
<property name="address" ref="address"></property>
<!-- 第三种:数组注入,array-->
<property name="books">
<array>
<value>红楼梦</value>
<value>三国演义</value>
<value>西游记</value>
<value>水浒传</value>
</array>
</property>
<!-- 第四种:list注入,list-->
<property name="hobbys">
<list>
<value>唱</value>
<value>跳</value>
<value>rap</value>
</list>
</property>
<!-- 第五种:map注入,map-->
<property name="card">
<map>
<entry key="邮政" value="110111"></entry>
<entry key="中商" value="120222"></entry>
<entry key="工商" value="119333"></entry>
</map>
</property>
<!-- 第六种:set注入,set-->
<property name="game">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!-- 第七种:null注入,null-->
<property name="wife">
<null></null>
</property>
<!-- 第二种:property 注入,props-->
<property name="info">
<props>
<prop key="学号">16421</prop>
<prop key="宿舍">125</prop>
<prop key="床号">6</prop>
</props>
</property>
</bean>
- 测试类:
public class myTestes {
@Test
public void test01(){
ApplicationContext Context = new ClassPathXmlApplicationContext("applictionContext.xml");
Student student = (Student) Context.getBean("student");
System.out.println(student.toString());
}
}
- 输出结果:
Student{name='qk', address=Address{address='湖北'}, books=[红楼梦, 三国演义, 西游记, 水浒传], hobbys=[唱, 跳, rap], card={邮政=110111, 中商=120222, 工商=119333}, game=[LOL, COC, BOB], info={宿舍=125, 床号=6, 学号=16421}, wife='null'}
3. 拓展方式注入
p命名空间注入:
- 需要在头文件中加入约束文件
导入约束 : xmlns:p="http://www.springframework.org/schema/p"
public class User {
private String name;
private int 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 +
'}';
}
}
<!--p命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="com.qk.pojo.User" p:name="qk" p:age="18"></bean>
c命名空间注入:
- 需要在头文件中加入约束文件
导入约束 : xmlns:c="http://www.springframework.org/schema/c"
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 +
'}';
}
}
<!--c命名空间注入,可以通过构造器注入:constructor-arg-->
<bean id="user2" class="com.qk.pojo.User" c:age="28" c:name="qq"></bean>
@Test
public void test03(){
ApplicationContext Context = new ClassPathXmlApplicationContext("userbean.xml");
User user = (User) Context.getBean("user2",User.class);
System.out.println(user);
}
个人总结:
- 不同:
p命名空间是通过实体类属性注入
c命名空间是通过有参构造方法注入 - 相同:
都需要导入约束