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的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: ...
随机推荐
- 搭建centos测试环境:window安装xshell,WinSCP 。 centos安装jdk tomcat
通过ssh实现远程访问linux系统: 由于xshell 连接centos,需要centos开启ssh服务.所以先启动SSH服务,没有ssh需要先安装. 1 . 查看SSH是否安装命令:rpm -qa ...
- php获取html纯文本,解决编辑器手动键入空格造成的无意义空白字符(空值问题)
在项目中,我们常常需要用到一些验证,不管是前台还是后台的,上传的问题时,需要内容不为空,但可视化编辑器的介入让手动敲入空格跳出了常规的检测.空格是一种排版的手段,但毫无内容只有空格就显得没有意义了,今 ...
- Bootstrap个人总结
Bootstrap框架 1.以栅栏式布局,分12列,16列,24列和32列,常用12列. 2.整个页面必须在container容器内部 3.移动端以 <meta name="viewp ...
- .NET Framework基础知识总结
之前给大家总结了java的面试几次技巧总结,同学们看了觉得还是不错,能够得到大家的认可,感觉还是挺不错的.现在又有同学来想小编索要.NET面试的总结了,好吧.谁让小编这么好呢!以下是.NET面试之框架 ...
- c# TimeSpan
转自:http://blog.163.com/y_p_xu/blog/static/17085710220116472030543/ /// <summary> /// 将时 ...
- PostgreSQL全文检索zhparser使用
本文引用自: http://blog.chinaunix.net/uid-20726500-id-4820580.html 防止文章丢失才进行复制 PostgreSQL支持全文检索,其内置的缺省的分词 ...
- 响应式布局 Bootstrap
github介绍 (1)简单灵活可用于架构流行的 用户界面和交互接口 的html.css.javascript工具集 (2)基于html5.css3的bootstrap,具有大量的诱人特征: 友好的学 ...
- QQ五笔词库转拼音词库小工具
参考文章<用QQ拼音打五笔>中提供的信息而制作的小工具,功能是将QQ五笔导出词库文件转换为QQ拼音自定义短语使用的.ini格式文件,这样就可以使用QQ拼音进行五笔拼音混输了. 混输效果不错 ...
- Hyper-V 2012 R2 故障转移群集
和终端用户相比,企业用户对于业务的连续性和可靠性更为在意.相对而言,企业一般不会将追逐单一硬件的性能排在第一位. 如何衡量业务是否持续可用,一般使用"x 个 9"这种方式来定义.如 ...
- js暂停的函数
// numberMillis 毫秒 function sleep(numberMillis) { var now = new Date(); var exitTime = now.getTime() ...