构造注入
语法:
<constructor-arg>
<ref bean=“bean的id”/>
</constructor-arg>
1.首先创建一个实体类,一定要有带参构造
public class UserEntity { private Integer id; private String name; private String pwd; private CardEntity myCard; public UserEntity() { System.out.println("UserEntity初始化============================"); } public UserEntity(Integer id, String name, String pwd, CardEntity myCard) { super(); this.id = id; this.name = name; this.pwd = pwd; this.myCard = myCard; } // 省略get,set }
2.创建dao
public interface UserEntityDao { public void save(UserEntity user); }
2.创建dao实现类
public class UserEntityDaoImpl implements UserEntityDao { @Override public void save(UserEntity user) { } }
3.创建biz
public interface UserEntityBiz { public void save(UserEntity user); }
4.创建biz实现类,植入一个dao对象
public class UserEntityBizImpl implements UserEntityBiz { private UserEntityDao userDao; public UserEntityBizImpl() { } public void UserEntityBizImpl(UserEntityDao userDao) { this.userDao = userDao; } @Override public void save(UserEntity user) { userDao.save(user); System.out.println("===保存成功===="); } //省略get,set }
5.配置xml
<bean id="card" class="cn.cnsdhzzl.entity.CardEntity"> <property name="id" value="123"></property> <property name="cardNumber" value="1111111110000"></property> </bean> <bean id="userDao" class="cn.cnsdhzzl.dao.impl.UserEntityDaoImpl" /> <bean id="userBiz" class="cn.cnsdhzzl.biz.impl.UserEntityBizImpl"> <constructor-arg> <ref bean="userDao"></ref> </constructor-arg> </bean> <bean id="userEntity" class="cn.cnsdhzzl.entity.UserEntity"> <constructor-arg index="0" type="java.lang.Integer" value="001" /> <constructor-arg index="1" type="java.lang.String" value="002用户" /> <constructor-arg index="2" type="java.lang.String" value="003用户密码" /> <constructor-arg index="3" ref="card" /> </bean>
6.测试
@Test /* * 构造注入 */ public void constructorSpring() { ApplicationContext ac = new ClassPathXmlApplicationContext( "applicationContext.xml"); UserEntityBizImpl biz = (UserEntityBizImpl) ac.getBean("userBiz"); UserEntity ue = (UserEntity) ac.getBean("userEntity"); biz.save(ue); System.out.println(ue.toString()); }
p命名空间注入
语法:
p:属性名=“属性值”
p:属性名-ref=“bean的id”
1.准备一个实体类
public class CardEntity { private Integer id; private String cardNumber; public CardEntity() { } public CardEntity(Integer id, String cardNumber) { this.id = id; this.cardNumber = cardNumber; } @Override public String toString() { return "CardEntity [id=" + id + ", cardNumber=" + cardNumber + "]"; } //省略get,set }
2.配置xml
<!-- 使用p空间实现属性注入 --> <bean id="card" class="cn.cnsdhzzl.entity.CardEntity" p:id="001" p:cardNumber="62256549361" />
3.测试
@Test /* * P命名空间注入 */ public void PInjection() { CardEntity card = (CardEntity) ac.getBean("card"); System.out.println(card.toString()); }
注:如果属性中包括了xml中的特殊字符(&、<、>、"、'),则注入时需要进行处理,通常可以采用两种方法:使用<![CDATA[]]>标记或把字符串替换为实体引用。
符号 | 实体引用 | 符号 | 实体引用 |
< | < | ' | ' |
> | > | " | " |
& | & |
。
注入集合类型的属性
1.list
<bean id="user" class="cn.cnsdhzzl.entity.UserEntity"> <property name="hobbies"> <list> <value>计算机</value> <value>运动</value> </list> </property> </bean>
2.set
<bean id="user" class="cn.cnsdhzzl.entity.UserEntity"> <property name="hobbies"> <set> <value>计算机</value> <value>运动</value> </set> </property> </bean>
3.map
<bean id="user" class="cn.cnsdhzzl.entity.UserEntity"> <property name="hobbies"> <map> <entry> <value>计算机</value> </entry> <entry> <value>运动</value> </entry> </map> </property> </bean>
4.props
<bean id="user" class="cn.cnsdhzzl.entity.UserEntity"> <property name="hobbies"> <props> <prop key="computer">计算机</prop> <prop key="motion">运动</prop> </props> </property> </bean>
5.注入null和空值
<!-- 注入空字符串 --> <bean id="user" class="cn.cnsdhzzl.entity.UserEntity"> <property name="hobbies"> <value></value> </property> </bean>
<!-- 注入null --> <bean id="user" class="cn.cnsdhzzl.entity.UserEntity"> <property name="hobbies"> <null></null> </property> </bean>