1、注入基本值
容器会将id的字符串值"10001"自动转换为和类匹配的类型,此处为int类型。以下两种方式效果一样。
package pers.zky.entity;
import java.io.Serializable;
/**
* @author Zky
* 实体Book,用于测试基本值的注入
*/
public class Book implements Serializable{
private int id;
private String name;
public Book(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<bean id="book" class="pers.zky.entity.Book">
<property name="id" value="10001"></property>
<property name="name">
<value>西游记</value>
</property>
</bean>
2、注入bean对象
用ref引用刚刚注入的Book类的bean的id,此处ref的值和上面的id不一致时会报错。
package pers.zky.entity;
import java.io.Serializable;
/**
* @author Zky
* 实体Student,用于测试bean对象的注入
*/
public class Student implements Serializable{
private String name;
private int id;
private Book book;
public Student(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
}
<bean id="student" class="pers.zky.entity.Student">
<property name="id" value="1001"></property>
<property name="name" value="张三"></property>
<property name="book" ref="book"></property>
</bean>
3、注入集合
set采用了引入的方式注入,其他的集合也类似。引入的方式有利于代码的重用。
package pers.zky.entity;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
*
* @author Zky
* 实体Message,用于测试集合的注入
*/
public class Message implements Serializable{
private List list;
private Set set;
private Map<String,Object> map;
private Properties properties;
public Message(){
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
<util:set id="uset">
<value>usetValue1</value>
<value>usetValue2</value>
<value>usetValue3</value>
<value>usetValue4</value>
</util:set>
<bean id="message" class="pers.zky.entity.Message">
<property name="list">
<list>
<value>listValue1</value>
<value>listValue2</value>
<value>listValue3</value>
</list>
</property>
<!--
<property name="set">
<set>
<value>setValue1</value>
<value>setValue2</value>
<value>setValue3</value>
<value>setValue4</value>
</set>
</property>
-->
<!-- 引入的方式注入集合set -->
<property name="set" ref="uset"></property>
<property name="map">
<map>
<entry key="mapKey1" value="mapValue1"></entry>
<entry key="mapKey2" value="mapValue2"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="url">propertiesValue1</prop>
<prop key="jdbc">propertiesValue2</prop>
<prop key="driver">propertiesValue3</prop>
</props>
</property>
</bean>
4、注入空值和表达式注入
<span style="font-size:10px;">package pers.zky.entity;
import java.io.Serializable;
/**
*
* @author Zky
* 实体User,用于测试空值的注入和表达式注入
*/
public class User implements Serializable{
private int id;
private String name;
private String password;
public User(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}</span><span style="font-size: 18px;">
</span>
<bean id="user" class="pers.zky.entity.User">
<property name="id" value="#{student.id}"></property>
<property name="name" value=""></property>
<property name="password"><null></null></property>
</bean>
5、引入注入的方式注入properties集合
package pers.zky.entity;
import java.io.Serializable;
/**
*
* @author Zky
* 实体JDBCProperties ,用于读取配置文件db.properties的jdbc参数
*/
public class JDBCProperties implements Serializable{
private String username;
private String password;
private String jdbc;
private String driver;
public JDBCProperties(){
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getJdbc() {
return jdbc;
}
public void setJdbc(String jdbc) {
this.jdbc = jdbc;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
}
<util:properties id="jdbc" location="classpath:db.properties"></util:properties>
<bean id="jdbcproperties" class="pers.zky.entity.JDBCProperties">
<property name="username" value="#{jdbc.username}"></property>
<property name="password" value="#{jdbc.password}"></property>
<property name="jdbc" value="#{jdbc.jdbc}"></property>
<property name="driver" value="#{jdbc.driver}"></property>
</bean>