Adder :
package com.cn.pojo;
public class Adder {
/**
作者
时间
类的描述
***/
private String adder;
public String getAdder() {
return adder;
}
public void setAdder(String adder) {
this.adder = adder;
}
@Override
public String toString() {
return "Adder{" +
"adder='" + adder + '\'' +
'}';
}
}
Student:
package com.cn.pojo;
import java.util.*;
public class Student {
/**
作者
时间
类的描述
***/
private String name;
private Adder adder;//引用类型
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> youxi;
private String wife;
private Properties info;
public String getName() {
return name;
}
public Set<String> getYouxi() {
return youxi;
}
public void setYouxi(Set<String> youxi) {
this.youxi = youxi;
}
public void setName(String name) {
this.name = name;
}
public Adder getAdder() {
return adder;
}
public void setAdder(Adder adder) {
this.adder = adder;
}
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 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 + '\'' +
", adder=" + adder +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", youxi=" + youxi +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
spring配置文件bean.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-3.0.xsd">
<bean id="adders" class="com.cn.pojo.Adder">
<property name="adder" value="北京"/>
</bean>
<bean id="student" class="com.cn.pojo.Student">
<!--第一注入方式:set注入-->
<property name="name" value="韩永红"/>
<!--第二bean注入:bean注入 ref-->
<property name="adder" ref="adder"/>
<!--3、数组注入-->
<property name="books">
<array>
<value>三国演义1</value>
<value>三国演义2</value>
<value>三国演义3</value>
<value>三国演义4</value>
</array>
</property>
<!--list注入-->
<property name="hobbys">
<list>
<value>学习</value>
<value>学习1</value>
<value>学习2</value>
</list>
</property>
<!--map注入-->
<property name="card">
<map>
<entry key="身份证" value="111111111111111111"/>
<entry key="银行卡" value="123456"/>
</map>
</property>
<!--set注入-->
<property name="youxi">
<set>
<value>和平精英</value>
<value>和平精英1</value>
<value>和平精英2</value>
</set>
</property>
<!--null注入-->
<property name="info">
<null/>
</property>
</bean>
<bean id="adder" class="com.cn.pojo.Adder"/>
</beans>
总结spring注入方式:
第一种:set的注入:name:实体类中的具体属性 value:属性进行赋值 class类的全路径
<bean id="student" class="com.cn.pojo.Student">
<!--第一注入方式:普通属性注入-->
<property name="name" value="java"/>
</bean>
第二种:bean注入:
<bean id="adders" class="com.cn.pojo.Adder"/>
<bean id="student" class="com.cn.pojo.Student">
//将第一bean注入到第二个bean中 ref:引用
<property name="adder" ref="adder"/>
</bean>
第三种:数组注入 :
<property name="books">
<array>
<value>三国演义1</value>
<value>三国演义2</value>
<value>三国演义3</value>
<value>三国演义4</value>
</array>
</property>
第四种:list注入
<!--list注入-->
<property name="hobbys">
<list>
<value>学习</value>
<value>学习1</value>
<value>学习2</value>
</list>
</property>
第五种:map键值对注入
<!--map注入-->
<property name="card">
<map>
<entry key="身份证" value="111111111111111111"/>
<entry key="银行卡" value="123456"/>
</map>
第六种:set注入
<!--set注入-->
<property name="youxi">
<set>
<value>和平精英</value>
<value>和平精英1</value>
<value>和平精英2</value>
</set>
</property>
第七:null注入
<!--null注入-->
<property name="info">
<null/>
</property>
test测试:
import com.cn.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyStudent {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");//获取spring的容器bea
Student s = (Student) context.getBean("student");//向上转型需要强壮
// //System.out.println(s.getName());
System.out.println(s.toString());
}
}
输出:
Student{name='韩永红', adder=Adder{adder='null'}, books=[三国演义1, 三国演义2, 三国演义3, 三国演义4], hobbys=[学习, 学习1, 学习2], card={身份证=111111111111111111, 银行卡=123456}, youxi=[和平精英, 和平精英1, 和平精英2]}
adder=Adder{adder='null'}中adder属性为空的原因:bean注入后未进行被注入bean的属性赋值导致产生。