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的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: ...
随机推荐
- uploadify前台上传文件,java后台处理的例子
1.先创建一个简单的web项目upload (如图1-1) 2.插件的准备 (1).去uploadify的官网下载一个uploadify插件,然后解压新建个js文件夹放进去(这个不强求,只要路径对了就 ...
- IOS_ios逆向工程-静态分析
返回博客列表 原 ios逆向工程-静态分析 余成海 发布时间: 2014/11/03 19:17 阅读: 11201 收藏: 17 点赞: 5 评论: 6 最近在学习IOS逆向工程,查看网络上的资料也 ...
- 有了门面,程序会更加体面!- pos软件基于三层架构 -09
续上篇) 大鸟说道:“实际上没有学过设计模式去理解三层架构会有失偏颇的,毕竟分层是更高一级别的模式,所谓的架构模式.不过在程序中,有意识的遵循设计原则,却也可以有效的做出好的设计.” ...
- 更换app开发者账号
在开源中国上面有一个答案,http://www.oschina.net/question/2307266_237220 下面是我的执行步骤 首先在iTunes Connect中找到要更换开发者账号的a ...
- Python os 标准库使用
os模块是python自带的一个核心模块,用于和操作系统对象进行交互. 1.导入模块获取帮助 >>> import os>>> help(os)>>&g ...
- hibernate学习(5)——一对多关系表达
一对多实现 1. 实现类 package com.alice.hibernate02.vo; import java.util.HashSet; import java.util.Set; publi ...
- 手动删除portal中托管服务。
在portal中将server作为托管联合服务器,然后发布了托管服务.若中间取消了托管联合服务器,再重新连接,那么会出现之前的托管服务无法删除的现象. 下文为怎样手动删除这些服务的方法,(不过貌似之后 ...
- centos 6.5安装node.js
1.检查是否安装gcc编译器 rpm -q gcc rpm -q gcc-c++ 2.如果没有安装则通过以下代码安装gcc编译器 yum -y install gcc-c++ kernel-devel ...
- TCP协议学习笔记(一)首部以及TCP的三次握手连接四次挥手断开
TCP协议是一种面向连接的.可靠的流协议. 流即不间断的数据结构.这样能够保证接收到数据顺序与发送相同.但是犹如数据间没有间隔,因此在TCP通信中,发送端应用可以在自己所要发送的消息中设置一个标示长度 ...
- oracle查询某个时间点的数据
1. select * from emps as of timestamp to_Date('2015-12-11 14:00:00','yyyy-mm-dd hh24:mi:ss'),SQL语句是查 ...