1、设值注入
<bean id="person" class="com.oupeng.person.Person">
<property name="name">
<value>Rod</value>
</property>
<property name="speck">
<value>世界上有10种人,认识二进制的和不认识二进制的</value>
</property>
</bean>
2、构造注入
类的构造方法完成依赖组件的注入
1.增加构造方法
UserDao ud;
private String str;
public UserServiceImpl() {
System.out.println("业务逻辑类的默认构造方法");
}
public UserServiceImpl(UserDao _ud,String str) {
this.ud = _ud;
this.str=str;
}
配置
<!-- 配置数据访问层bean -->
<bean id="userdao" class="com.oupeng.dao.impl.UserDaoImpl"/>
<!-- 配置业务逻辑层bean -->
<!--<bean id="userService" class="com.oupeng.service.impl.UserServiceImpl">
<property name="ud" ref="userdao"></property>
</bean> -->
<!--构造注入 -->
<bean id="userService" class="com.oupeng.service.impl.UserServiceImpl">
<constructor-arg index="0"> <ref bean="userdao"/> </constructor-arg> <constructor-arg
index="1" type="String"> <value>我是一个小小的小鸟...</value> </constructor-arg>
</bean>
3、p命名空间注入
简化代码,往往用于对象初始化
头文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
配置文件进行配置
<!-- p命名空间注入方式 -->
<bean id="userService" class="com.oupeng.service.impl.UserServiceImpl"
p:ud-ref="userdao" p:str="哈哈" />
<!-- P命名空间注入一个实体类对象 -->
<bean id="user" class="com.oupeng.pojo.User" p:id="001"
p:userName="张飞" p:password="123456" p:email="zhangfei@sanguo.com" />
语法:
p:注入的组件属性名-ref="引用的Bean组件ID"
p:注入的属性名="值"
优点:代码简洁
缺点:灵活性有限
二、为不同的数据类型完成注入:
编写具有不同数据类型的实体类:
提供setter以及getter方法
增加打印各属性的方法
package com.oupeng.pojo;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class TestEntity {
private String character1;
private String character2;
private User innerBean;
private List<String> list;
private String[] array;
private Set<String> set;
private Map<String,String> map;
private Properties properties;
private String emptyString;
private String nullString;
public String getCharacter1() {
return character1;
}
public void setCharacter1(String character1) {
this.character1 = character1;
}
public String getCharacter2() {
return character2;
}
public void setCharacter2(String character2) {
this.character2 = character2;
}
public User getInnerBean() {
return innerBean;
}
public void setInnerBean(User innerBean) {
this.innerBean = innerBean;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public String[] getArray() {
return array;
}
public void setArray(String[] array) {
this.array = array;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public String getEmptyString() {
return emptyString;
}
public void setEmptyString(String emptyString) {
this.emptyString = emptyString;
}
public String getNullString() {
return nullString;
}
public void setNullString(String nullString) {
this.nullString = nullString;
}
//打印该实体类的所有属性值
public void showValue(){
System.out.println("特殊字符1:"+this.character1);
System.out.println("特殊字符2:"+this.character2);
System.out.println("内部Bean:"+this.getInnerBean().getUserName());
System.out.println("List集合 :"+this.list);
System.out.println("数组类型:"+this.array);
System.out.println("Set集合 :"+this.set);
System.out.println("Map集合:"+this.map);
System.out.println("Properties:"+this.properties);
System.out.println("空字符串:"+this.emptyString);
System.out.println("null字符串:"+this.nullString);
}
}
配置文件
<bean id="testentity" class="com.oupeng.pojo.TestEntity">
<!-- 特殊字符1 -->
<property name="character1">
<value><![CDATA[H&H]]></value>
</property>
<!-- 特殊字符2 -->
<property name="character2">
<value><![CDATA[H&H]]></value>
</property>
<!-- 内部Bean的配置 -->
<property name="innerBean">
<bean class="com.oupeng.pojo.User">
<property name="userName">
<value>关羽</value>
</property>
<property name="email">
<value>guanyu@shuguo.com</value>
</property>
</bean>
</property>
<!-- list集合 -->
<property name="list">
<list>
<value>关羽</value>
<value>张飞</value>
<value>马超</value>
<value>赵子龙</value>
<value>黄忠</value>
</list>
</property>
<!-- 数组array配置 -->
<property name="array">
<list>
<value>关兴</value>
<value>张苞</value>
<value>关平</value>
<value>马谡</value>
<value>姜维</value>
</list>
</property>
<!-- set配置 -->
<property name="set">
<set>
<value>周瑜</value>
<value>吕蒙</value>
<value>陆逊</value>
<value>黄盖</value>
<value>鲁肃</value>
</set>
</property>
<!-- map集合 -->
<property name="map">
<map>
<entry>
<key>
<value>子敬</value>
</key>
<value>鲁子敬</value>
</entry>
<entry>
<key>
<value>仲达</value>
</key>
<value>司马懿</value>
</entry>
</map>
</property>
<!--配置properties -->
<property name="properties">
<props>
<prop key="刘备">刘玄德</prop>
<prop key="曹操">曹孟德</prop>
</props>
</property>
<!-- 配置emptyString -->
<property name="emptyString">
<value></value>
</property>
<!-- 配置nullString -->
<property name="nullString">
<null />
</property>
</bean>
执行结果
业务逻辑类的默认构造方法
特殊字符1:H&H
特殊字符2:H&H
内部Bean:关羽
List集合 :[关羽, 张飞, 马超, 赵子龙, 黄忠]
数组类型:[Ljava.lang.String;@619713e5
Set集合 :[周瑜, 吕蒙, 陆逊, 黄盖, 鲁肃]
Map集合:{子敬=鲁子敬, 仲达=司马懿}
Properties:{刘备=刘玄德, 曹操=曹孟德}
空字符串:
null字符串:null