Spring的Ioc和AOP扩展
多种方式实现依赖注入:
这里唯一需要说明的是如果要使用P命名空间实现属性注入,需要添加命名空间的声明:
如我的xml里红色字体:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/bean http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 设置注入(通过访问器注入) 1.使用灵活2.时效性不足 -->
<bean id="a" class="cn.cnti.ioc.A">
<property name="a1" value="a1 hello"></property>
<property name="a2">
<!-- 赋值为特殊字符时需要用 -->
<value><![CDATA[<><><><><><>]]></value>
</property>
<property name="a3" value="60"></property>
</bean>
<bean id="a1" class="cn.cnti.ioc.A">
<property name="a1">
<value>a1 hello</value>
</property>
<property name="a2">
<value>a2 hello</value>
</property>
<property name="a3">
<value>80</value>
</property>
</bean>
<!-- 通过构造注入 1.时效性好2.灵活性不足 -->
<bean id="a2" class="cn.cnti.ioc.A">
<constructor-arg index="0" value="a1 hello"></constructor-arg>
<constructor-arg index="1" value="a2 hello"></constructor-arg>
<constructor-arg index="2" value="98"></constructor-arg>
</bean>
<bean id="a3" class="cn.cnti.ioc.A">
<constructor-arg name="a1" value="a1 hello"></constructor-arg>
<constructor-arg name="a2" value="a2 hello"></constructor-arg>
<constructor-arg name="a3" value="99"></constructor-arg>
</bean>
<bean id="a4" class="cn.cnti.ioc.A">
<constructor-arg type="java.lang.String" value="a1 hello"></constructor-arg>
<constructor-arg type="java.lang.String" value="a2 hello"></constructor-arg>
<constructor-arg type="java.lang.Integer" value="100"></constructor-arg>
</bean>
<!-- 使用p命名空间的注入,首先需要添加p命名空间的声明 -->
<bean id="a5" class="cn.cnti.ioc.A" p:a1="a1 hello" p:a2="a2 hello"
p:a3="101" />
<!-- 注入集合类型的属性 -->
<bean id="a6" class="cn.cnti.ioc.A">
<!-- list集合 -->
<property name="list">
<list>
<value>hello1</value>
<value>hello2</value>
<value>hello3</value>
</list>
</property>
<property name="listB">
<list>
<ref bean="b" />
<ref bean="b" />
<ref bean="b" />
</list>
</property>
<!-- set集合 -->
<property name="set">
<set>
<value>he</value>
<value>hel</value>
<value>hello</value>
</set>
</property>
<property name="setB">
<set>
<ref bean="b" />
</set>
</property>
<!-- map集合 -->
<property name="map">
<map>
<entry>
<key>
<value>one</value>
</key>
<value>1</value>
</entry>
<entry>
<key>
<value>two</value>
</key>
<value>2</value>
</entry>
<entry>
<key>
<value>three</value>
</key>
<value>3</value>
</entry>
</map>
</property>
<property name="mapO">
<map>
<entry>
<key>
<value>one</value>
</key>
<ref bean="b" />
</entry>
<entry>
<key>
<value>two</value>
</key>
<ref bean="b" />
</entry>
<entry>
<key>
<value>three</value>
</key>
<ref bean="b" />
</entry>
</map>
</property>
</bean>
<bean id="b" class="cn.cnti.ioc.B" p:b="hello b"></bean>
<!-- map集合(集合里没有对象)的另一种简单用法 -->
<bean id="a7" class="cn.cnti.ioc.A">
<property name="map">
<props>
<prop key="one">he</prop>
<prop key="two">hel</prop>
<prop key="three">hello</prop>
</props>
</property>
</bean>
<!-- 使用内部bean -->
<bean id="a8" class="cn.cnti.ioc.A">
<property name="b">
<bean class="cn.cnti.ioc.B">
<property name="b" value="i am b"></property>
</bean>
</property>
</bean>
</beans>
我的实体类A:
package cn.cnti.ioc;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class A {
private String a1;
private String a2;
private Integer a3;
// 测试内部bean
private B b;
// 测试各种集合
List<String> list;
List<B> listB;
Set<String> set;
Set<B> setB;
Map<String, String> map;
Map<String, Object> mapO;
public A() {
}
public A(String a1, String a2, Integer a3) {
this.a1 = a1;
this.a2 = a2;
this.a3 = a3;
}
public String getA1() {
return a1;
}
public void setA1(String a1) {
this.a1 = a1;
}
public String getA2() {
return a2;
}
public void setA2(String a2) {
this.a2 = a2;
}
public Integer getA3() {
return a3;
}
public void setA3(Integer a3) {
this.a3 = a3;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public List<B> getListB() {
return listB;
}
public void setListB(List<B> listB) {
this.listB = listB;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Set<B> getSetB() {
return setB;
}
public void setSetB(Set<B> setB) {
this.setB = setB;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Map<String, Object> getMapO() {
return mapO;
}
public void setMapO(Map<String, Object> mapO) {
this.mapO = mapO;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
@Override
public String toString() {
return "A [a1=" + a1 + ", a2=" + a2 + ", a3=" + a3 + ", b=" + b
+ ", list=" + list + ", listB=" + listB + ", set=" + set
+ ", setB=" + setB + ", map=" + map + ", mapO=" + mapO + "]";
}
}
我的实体类B:
package cn.cnti.ioc;
public class B {
private String b;
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
@Override
public String toString() {
return "B [b=" + b + "]";
}
}
我的实体类C:
package cn.cnti.ioc;
public class C {
private String C;
public String getC() {
return C;
}
public void setC(String c) {
C = c;
}
@Override
public String toString() {
return "C [C=" + C + "]";
}
}
Spring的Ioc和AOP扩展的更多相关文章
- Spring的IOC和AOP之深剖
今天,既然讲到了Spring 的IOC和AOP,我们就必须要知道 Spring主要是两件事: 1.开发Bean:2.配置Bean.对于Spring框架来说,它要做的,就是根据配置文件来创建bean实例 ...
- Spring框架IOC和AOP介绍
说明:本文部分内容参考其他优秀博客后结合自己实战例子改编如下 Spring框架是个轻量级的Java EE框架.所谓轻量级,是指不依赖于容器就能运行的.Struts.Hibernate也是轻量级的. 轻 ...
- 展开说说,Spring Bean IOC、AOP 循环依赖
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 延迟满足能给你带来什么? 大学有四年时间,但几乎所有人都是临近毕业才发现找一份好工作 ...
- spring的IOC和AOP
spring的IOC和AOP 1.解释spring的ioc? 几种注入依赖的方式?spring的优点? IOC你就认为他是一个生产和管理bean的容器就行了,原来需要在调用类中new的东西,现在都是 ...
- Spring 的IOC和AOP总结
Spring 的IOC和AOP IOC 1.IOC 许多应用都是通过彼此间的相互合作来实现业务逻辑的,如类A要调用类B的方法,以前我们都是在类A中,通过自身new一个类B,然后在调用类B的方法,现在我 ...
- Spring中IOC和AOP的详细解释(转)
原文链接:Spring中IOC和AOP的详细解释 我们是在使用Spring框架的过程中,其实就是为了使用IOC,依赖注入,和AOP,面向切面编程,这两个是Spring的灵魂. 主要用到的设计模式有工厂 ...
- spring的IOC和AOP详细讲解
1.解释spring的ioc? 几种注入依赖的方式?spring的优点? IOC你就认为他是一个生产和管理bean的容器就行了,原来需要在调用类中new的东西,现在都是有这个IOC容器进行产生,同时, ...
- Spring 的IOC 和Aop
Spring 的IOC 和Aop
- # Spring 练习ioc 、aop
Spring 练习 通过学习spring的基础知识,了解了Spring为了降低Java开发的复杂性,采取了以下4种关键策略: 基于POJO的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: ...
随机推荐
- vs发布的程序不依赖运行时库msvcp100.dll
[摘要:msvcr100.dll:MS Visual C Runtime 100 msvcp100.dll:MS Visual CPp 100 vs建立的工程,运转时库的范例有MD(MDd)战MT ...
- Sass和compass 安装 和配合grunt实时显示 [Sass和compass学习笔记]
demo 下载http://vdisk.weibo.com/s/DOlfkrAWjkF/1401192855 为什么要学习Sass和compass ?提高站独立和代码产品化的绝密武器,尤其是程序化cs ...
- Python中输出格式化的字符串
在Python中,采用的格式化方式和C语言是一致的,用%实现,举例如下: >>> 'Hello, %s' % 'world' 'Hello, world' >>> ...
- CentOS7 编译安装 Nginx (实测 笔记 Centos 7.0 + nginx 1.6.2)
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7.0-1406-x86_64-DVD.iso 安装步骤: 1.准备 1.1 显示系统版 ...
- MySQL中INFORMATION_SCHEMA是什么?(2)
information_schema数据库表说明: SCHEMATA:提供了当前mysql实例中所有数据库的信息.是show databases的结果取之此表. TABLES:提供了关于数据库中的表的 ...
- java并发编程(六)Runnable和Thread实现多线程的区别
http://blog.csdn.net/ns_code/article/details/17161237
- osip2 代码分析
主要类型定义: 1.osip_t /** * Structure for osip handling. * In order to use osip, you have to manage at le ...
- ueditor编辑器使用
下载ueditor1_4_3_3-gbk-asp 解压后重命名为ueditor上传至网站 在需要编辑器的位置增加asp代码: <td style="PADDING-LEFT: 10px ...
- 20145220&20145209&20145309信息安全系统设计基础实验报告(3)
20145220&20145209&20145309信息安全系统设计基础实验报告(3) 实验报告链接: http://www.cnblogs.com/zym0728/p/6132243 ...
- iOS相关笔记
#协议[1] [2] @property (nonatomic, assign) id<EveryFrameDelegate> delegate; 表明,这个delegate是一个需要实现 ...