构造注入

语法:

<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[]]>标记或把字符串替换为实体引用。

xml中预定义的实体引用
符号 实体引用 符号 实体引用
< &lt; ' &apos;
> &gt; " &quot;
& &amp;    

注入集合类型的属性

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>

Ioc和Aop扩展--多种方式实现依赖注入(构造注入,p命名空间注入,集合类型注入,注入null和注入空值)的更多相关文章

  1. IOC和AOP使用扩展 多种方式实现依赖注入

    多种方式实现依赖注入 1.Spring 使用setter访问器实现对属性的赋值, 2.Spring 构造constructor方法赋值, 3.接口注入 4.Spring P命名空间注入直接量 sett ...

  2. Spring的Ioc和AOP扩展

    多种方式实现依赖注入: 这里唯一需要说明的是如果要使用P命名空间实现属性注入,需要添加命名空间的声明: 如我的xml里红色字体: <?xml version="1.0" en ...

  3. Spring学习总结(三)——Spring实现AOP的多种方式

    AOP(Aspect Oriented Programming)面向切面编程,通过预编译方式和运行期动态代理实现程序功能的横向多模块统一控制的一种技术.AOP是OOP的补充,是Spring框架中的一个 ...

  4. Spring实现AOP的多种方式

    转载自:https://www.cnblogs.com/best/p/5736422.html:加了一些自己的注释: AOP(Aspect Oriented Programming)面向切面编程,通过 ...

  5. Spring——多种方式实现依赖注入

    在Spring的XML配置中,只有一种声明bean的方式:使用<bean>元素并指定class属性.Spring会从这里获取必要的信息来创建bean. 但是,在XML中声明DI时,会有多种 ...

  6. IoC和AOP扩展

    一.构造注入 二.使用p命名空间注入属性值 三.注入不同数据类型 <?xml version="1.0" encoding="UTF-8"?> &l ...

  7. Spring多种方式实现依赖注入

    平常的Java开发中,程序员在某个类中需要依赖其它类的方法. 通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理. Spring提出了依赖注入的思想,即依赖类不由 ...

  8. 多种方式实现依赖注入及使用注解定义bean

    构造注入 如何给构造方法中的参数注入方法呢如下 首先bean代码如下 package cn.pojo; public class Greeting { /** * 说的话 */ private Str ...

  9. IoC和AOP使用扩展。。。

    实现依赖的多种方式. 1.理解构造注入. 2.掌握使用p命名空间实现属性注入. 3.理解不同的数据类型的注入方式. 4.如何通过构造注入为业务类注入所依赖的数据访问层对象,实现保存用户数据功能. 5. ...

随机推荐

  1. Mysql事件学习

    出自:http://blog.chinaunix.net/uid-20639775-id-3323098.html Mysql事件学习 在系统管理或者数据库管理中,经常要周期性的执行某一个命令或者SQ ...

  2. div相对浏览器移动

    <%    String path = request.getContextPath();%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...

  3. SAP、BW 权限控制设置

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. XML详解:第一部分

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. NPOI大数据分批写入同个Excel

    实现过程: 要导出来的数据库数据量很大,一次取出来压力有点大,故分批取出来,导入到同一个Excel. 因为Excel2003版最大行数是65536行,Excel2007开始的版本最大行数是104857 ...

  6. ios uiview封装动画(摘录)

    iOS开发UI篇—核心动画(UIView封装动画) 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画 ...

  7. 基于jQuery的简易瀑布流实现

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. C#形参,实参,值传递参数,引用传递参数,输出参数,参数数组的学习

    1)形参 形参顾名思义就是形式上的参数,不是实际的参数,它代替实际传入方法的值.在方法体代码中代表了值本身参与运算.形参定义于参数中,它不同于方法体内局部变量,因为是一个变量,在它的作用域内不允许存在 ...

  9. iOS - Swift Dictionary 字典

    前言 public struct Dictionary<Key : Hashable, Value> : CollectionType, DictionaryLiteralConverti ...

  10. 从网页(WEB)登录SAP

    以下这篇文章写得很详细,照着做就可以了: http://www.doc88.com/p-293361232332.html   设好后, 默认的端口是80$$, 其中$$是安装SAP时的instanc ...