Ioc和Aop扩展--多种方式实现依赖注入(构造注入,p命名空间注入,集合类型注入,注入null和注入空值)
构造注入
语法:
<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>
Ioc和Aop扩展--多种方式实现依赖注入(构造注入,p命名空间注入,集合类型注入,注入null和注入空值)的更多相关文章
- IOC和AOP使用扩展 多种方式实现依赖注入
多种方式实现依赖注入 1.Spring 使用setter访问器实现对属性的赋值, 2.Spring 构造constructor方法赋值, 3.接口注入 4.Spring P命名空间注入直接量 sett ...
- Spring的Ioc和AOP扩展
多种方式实现依赖注入: 这里唯一需要说明的是如果要使用P命名空间实现属性注入,需要添加命名空间的声明: 如我的xml里红色字体: <?xml version="1.0" en ...
- Spring学习总结(三)——Spring实现AOP的多种方式
AOP(Aspect Oriented Programming)面向切面编程,通过预编译方式和运行期动态代理实现程序功能的横向多模块统一控制的一种技术.AOP是OOP的补充,是Spring框架中的一个 ...
- Spring实现AOP的多种方式
转载自:https://www.cnblogs.com/best/p/5736422.html:加了一些自己的注释: AOP(Aspect Oriented Programming)面向切面编程,通过 ...
- Spring——多种方式实现依赖注入
在Spring的XML配置中,只有一种声明bean的方式:使用<bean>元素并指定class属性.Spring会从这里获取必要的信息来创建bean. 但是,在XML中声明DI时,会有多种 ...
- IoC和AOP扩展
一.构造注入 二.使用p命名空间注入属性值 三.注入不同数据类型 <?xml version="1.0" encoding="UTF-8"?> &l ...
- Spring多种方式实现依赖注入
平常的Java开发中,程序员在某个类中需要依赖其它类的方法. 通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理. Spring提出了依赖注入的思想,即依赖类不由 ...
- 多种方式实现依赖注入及使用注解定义bean
构造注入 如何给构造方法中的参数注入方法呢如下 首先bean代码如下 package cn.pojo; public class Greeting { /** * 说的话 */ private Str ...
- IoC和AOP使用扩展。。。
实现依赖的多种方式. 1.理解构造注入. 2.掌握使用p命名空间实现属性注入. 3.理解不同的数据类型的注入方式. 4.如何通过构造注入为业务类注入所依赖的数据访问层对象,实现保存用户数据功能. 5. ...
随机推荐
- MS16-016 提权EXP
测试环境 win7 32 位未打任何补丁 1.使用net user 指令 测试得到结果没有权限新建用户 2.查看系统用户 3.复制EXP到win7 下 使用命令打开 添加新用户再查看用户列表
- SqlServer 一些操作
//查询一个表中的某字段为条件修改另一个表的内容 update [VehicleInsuranceAgentConfiguration] set Keyword2=PC.ServerConfig fr ...
- C#正则表达式编程(二):Regex类用法
上一篇讲述了在C#中有关正则表达式的类之间的关系,以及它们的方法,这一篇主要是将Regex这个类的用法的,关于Match及MatchCollection类会在下一篇讲到.对于正则表达式的应用,基本上可 ...
- ubuntu 安装JDK
下载JDK6安装包,我的为32位系统所以选择jdk-6u35-linux-i586.bin 下载地址:http://www.oracle.com/technetwork/java/javase/dow ...
- <<易货>>项目Postmortem结果
设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 一开始想做的事情还是太多,没有形成整个app的核心功能,浪费了很多时间. 是否有充足的时间来做计划? 有 ...
- SAP MM Consignment 寄售库存
转自:http://blog.csdn.net/futurewind/article/details/3985200 寄售,定义就是供应商的货物放在自己的库存中,使用的时候可以转到自己的库存,不用了就 ...
- 硬盘坏道 检测/修复 Windows
1. 主要参看了:http://jingyan.baidu.com/article/2c8c281dfd93df0008252a9b.html 2. 2.1.hdtunepro.zip 是在 http ...
- Linux输入子系统(转)
Linux输入子系统(Input Subsystem) 1.1.input子系统概述 输入设备(如按键,键盘,触摸屏,鼠标等)是典型的字符设备,其一般的工作机制是低层在按键,触摸等动作发生时产生一个中 ...
- hdu4717The Moving Points(三分)
链接 需要特判一下n=1的时候 精度调太低会超时 #include <iostream> #include<cstdio> #include<cstring> #i ...
- 面向对象的static关键字(类中的static关键字)
转自:http://blog.csdn.net/xiayefanxing/article/details/7382192 http://www.cnblogs.com/SelaSelah/archiv ...