Spring AOP基于xml配置实例
SpringAOP里的几个术语,什么切面,切点之类的,官方的说明太抽象。为了更好地理解记忆,这里几下我自己的通俗的理解。
切面:就是日记类,什么前置通知后置通知(这些都是所谓的Advice)的具体方法都是写在这个日记类里的,所以把这些抽象出来的日记类就是一个切面(横切关注点)。
切点:就是某个具体的业务逻辑类,比如xxxService,也就是具体的DAO,Service之类的。
目录层级:

AOP相关的几个类就是com.aop.xmltype这个报下的4个类。
ICalculatorxml.java
package com.aop.xmltype; /**
* 加减乘除接口,用于AOP测试
*
* @author Wei
*
*/
public interface ICalculatorxml {
/**
* 加法
*
* @param a
* @param b
* @return a+b
*/
public int doAdd(int a, int b); /**
* 减法
*
* @param a
* @param b
* @return a-b
*/
public int doSub(int a, int b); /**
* 乘法
*
* @param a
* @param b
* @return a*b
*/
public int doMul(int a, int b); /**
* 除法
*
* @param a
* @param b
* @return a/b
*/
public int doDiv(int a, int b); /**
* 求绝对值
*
* @param a
* @param b
* @return |a-b|
*/
public int doAbs(int a, int b);
}
CalculatorImplxml.java
package com.aop.xmltype;
public class CalculatorImplxml implements ICalculatorxml {
/*
* { System.out.println("CalculatorImpl...{} ..."); }
*/
@Override
public int doAdd(int a, int b) {
int rtn = a + b;
System.out.println(a + "+" + b + "=" + rtn);
return rtn;
}
@Override
public int doSub(int a, int b) {
// TODO Auto-generated method stub
return a - b;
}
@Override
public int doMul(int a, int b) {
// TODO Auto-generated method stub
return a * b;
}
@Override
public int doDiv(int a, int b) {
if (b == 0) {
return -1;
}
return a / b;
}
@Override
public int doAbs(int a, int b) {
return Math.abs(a - b);
}
}
MyAspectxml.java
package com.aop.xmltype;
public class MyAspectxml {
public void logBefore() {
// String className = j.getClass().getName();
System.out.println("MyAspectxml logBefore(),开始计算...,");
}
public void logAfter() {
System.out.println("已经计算结束...");
}
}
MyAOPTest.java
package com.aop.xmltype;
import org.junit.Test;
import com.util.Pub;
public class MyAOPTest {
ICalculatorxml cal;
@Test
public void testAop() {
cal = new CalculatorImplxml();
cal.doAdd(13, 99);
}
public static void main(String[] args) {
ICalculatorxml cal = (ICalculatorxml) Pub.getBeanCtx().getBean("calImplxml");
System.out.println("-----------------分割符号----------\n\n\n\n");
cal.doAdd(13, 99);
}
}
执行结果:

AOP相关部分的配置,完整的xml配置在最后。
bean.xml
<!-- 切点的bean -->
<bean class="com.aop.xmltype.CalculatorImplxml" id="calImplxml"></bean>
<!-- 切面的bean -->
<bean class="com.aop.xmltype.MyAspectxml" id="myaspxml"></bean>
<!-- aop xmlType,用xml的形式配置AOP前置通知 -->
<aop:config>
<!--aop:pointcut 其实放在这儿也可以 -->
<!-- <aop:pointcut expression="execution (* com.aop.xmltype.CalculatorImplxml.*(..))"
id="pointcut1" /> --> <!-- 配置切面和通知 ,aop:aspect标签需要通过ref指定配置好的bean,id随便配置或者不配置,id的值可以随意起 -->
<aop:aspect id="myaspxml" ref="myaspxml" order="2">
<!-- 配置切点,即 要被记日记的对象, aop:pointcut 放在这儿也可以 ,切点不需要根对应的bean相关联,
只要expression指定的方法所在的类被Spring扫描得到就行,即只要所在的类配置了bean就可以 -->
<aop:pointcut expression="execution (* com.aop.xmltype.CalculatorImplxml.*(..))"
id="pointcut1" />
<!-- 切面里的具体的用于记录的方法就是一个通知,需要用通过pointcut-ref来指定具体的切点, -->
<aop:before method="logBefore" pointcut-ref="pointcut1" />
<aop:after method="logAfter" pointcut-ref="pointcut1" /> </aop:aspect> </aop:config>

完整的beans.xml的配置如:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="moocAppctx" class="imooc_spring.test.aware.MoocApplicationContext"
init-method="hhhh">
</bean> <!-- 引入db.properties -->
<context:property-placeholder location="classpath:db.properties" /> <!-- 配置C3P0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driverName}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.pwd}"></property>
</bean> <!-- 配置 Spring 的 org.springframework.jdbc.core.JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <bean id="moocBeanNameAware" class="imooc_spring.test.aware.MoocBeanNameAware"></bean> <!-- 测试 SpEL: 可以为属性进行动态的赋值(了解) -->
<bean id="girl" class="com.helloworld.User">
<property name="userName" value="周迅"></property>
</bean> <!-- <bean id="boy" class="com.helloworld.User" init-method="init" destroy-method="destroy">
<property name="userName" value="高胜远"></property> <property name="wifeName"
value="#{girl.userName}"></property> </bean> --> <bean id="girl2" class="com.helloworld.User2">
<property name="userName" value="Talor Swift"></property>
</bean> <!-- autowired测试,自动装配测试 -->
<bean id="people" class="test.spring.autowired.Person" scope="prototype"
autowire="byName">
<property name="name" value="小明"></property>
<!-- <property name="cat" ref="cat222"></property> -->
<!-- <property name="cat" ref="cat1"></property> -->
</bean> <bean id="cat" class="test.spring.autowired.Cat" scope="prototype">
<property name="name" value="波斯猫"></property>
</bean>
<!-- <bean id="cat222" class="test.spring.autowired.Cat"> <property name="name"
value="我是小喵喵"></property> </bean> --> <bean id="people2" class="test.spring.autowired.Person" scope="prototype"
autowire="byName">
<property name="name" value="小明"></property>
<property name="cat" ref="cat222"></property>
</bean> <bean id="cat222" class="test.spring.autowired.Cat" scope="prototype">
<property name="name" value="波斯猫"></property>
</bean> <!--context:component-scan 指定 扫描的包 -->
<!--可以通过 resource-pattern 指定扫描的资源, resource-pattern="myrepository/*.class"
的含义: 只扫描 base-package 对应包下的 目录为 myrepository 的所有java Bean -->
<!-- <context:component-scan base-package="imooc_spring.test.anotation"
resource-pattern="myrepository/*.class"></context:component-scan> --> <!-- context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"
子节点指定排除哪些注解 context:include-filter type="annotation" 需要结合context:component-scan
标签的 use-default-filters="false"来使用 context:exclude-filter type="assignable"
这个expression指的是自己写的类,意思排除哪些类 expression="imooc_spring.test.anotation.TestObj" -->
<context:component-scan base-package="imooc_spring.test.anotation">
<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"
/> --> <!-- <context:exclude-filter type="assignable" expression="imooc_spring.test.anotation.TestObj"
/> --> </context:component-scan>
<context:component-scan base-package="com.aop"></context:component-scan> <!-- aop测试,需要引入aop命名空间 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- aop annotationType, --> <!-- 切点的bean -->
<bean class="com.aop.xmltype.CalculatorImplxml" id="calImplxml"></bean>
<!-- 切面的bean -->
<bean class="com.aop.xmltype.MyAspectxml" id="myaspxml"></bean>
<!-- aop xmlType,用xml的形式配置AOP前置通知 -->
<aop:config>
<!--aop:pointcut 其实放在这儿也可以 -->
<!-- <aop:pointcut expression="execution (* com.aop.xmltype.CalculatorImplxml.*(..))"
id="pointcut1" /> --> <!-- 配置切面和通知 ,aop:aspect标签需要通过ref指定配置好的bean,id随便配置或者不配置,id的值可以随意起 -->
<aop:aspect id="myaspxml" ref="myaspxml" order="2">
<!-- 配置切点,即 要被记日记的对象, aop:pointcut 放在这儿也可以 ,切点不需要根对应的bean相关联,
只要expression指定的方法所在的类被Spring扫描得到就行,即只要所在的类配置了bean就可以 -->
<aop:pointcut expression="execution (* com.aop.xmltype.CalculatorImplxml.*(..))"
id="pointcut1" />
<!-- 切面里的具体的用于记录的方法就是一个通知,需要用通过pointcut-ref来指定具体的切点, -->
<aop:before method="logBefore" pointcut-ref="pointcut1" />
<aop:after method="logAfter" pointcut-ref="pointcut1" /> </aop:aspect> </aop:config> </beans>
项目名为:imooc_spring.rar,在我的qq微云上。
Spring AOP基于xml配置实例的更多相关文章
- [刘阳Java]_Spring AOP基于XML配置介绍_第9讲
基于注解配置的Spring AOP固然简单,但是这节我们会给大家介绍基于XML配置的AOP是如何应用的.为什么这么说了,因为后面我们还会介绍到Spring对Dao操作的事务管理(基于AOP的XML文件 ...
- Spring AOP-xml配置
在spring AOP(一)中介绍了AOP的基本概念和几个术语,现在学习一下在XML中如何配置AOP. 在XML中AOP的配置元素有以下几种: AOP配置元素 描述 <aop:config> ...
- spring-第十八篇之spring AOP基于XML配置文件的管理方式
1.在XML配置文件中配置切面.切入点.增强处理.spring-1.5之前只能使用XML Schema方式配置切面.切入点.增强处理. spring配置文件中,所有的切面.切入点.增强处理都必须定义在 ...
- Spring AOP之xml 配置实现
首先这个配置模式估计现在已经不用了,因为我在我们公司的项目里面并没有看到这么配置AOP相关的东西.不过,这个就和学习spring的控制反转(IOC)和依赖注入(DI)一样,刚刚开始的时候,都是从简单的 ...
- Spring Aop(七)——基于XML配置的Spring Aop
转发:https://www.iteye.com/blog/elim-2396043 7 基于XML配置的Spring AOP 基于XML配置的Spring AOP需要引入AOP配置的Schema,然 ...
- 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP
上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...
- Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较
本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...
- spring的基于xml的AOP配置案例和切入点表达式的一些写法
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用
Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...
随机推荐
- 组合框里添加复选框的方法(使用勾选的假象,用图片代替而已,并非QT原生支持)
组合框可以看作是列表框和文本框的组合,因其占据的空间少,使用操作方便,常被界面设计人员用于界面开发设计中,在有限个输入的条件下,组合框常用来代替文本框,这样从用户使用角度来看,更趋人性化,所见即所得. ...
- 使用PyQt来编写第一个Python GUI程序
原文:使用PyQt来编写第一个Python GUI程序 本文由 伯乐在线 - Lane 翻译,Daetalus 校稿.未经许可,禁止转载!英文出处:pythonforengineers.com.欢迎加 ...
- 命令行方式运行yii2程序
测试环境,yii 2.0.3版本 web访问方式,控制器放在controllers目录下 ,浏览器访问如下地址 http://127.0.0.1/index.php?r=[controller-nam ...
- Android-自己定义显示价格的PriceView
转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/44418883 先看一下我们要做的效果: 价格分成了3部分.前面是一个¥,中间 ...
- gdbserver 安卓apk
gdbserver 调试程序 底层调用c/c++ 动态库, 动态库带调试选项 查看手机IP 192.168.1.177 包所调用的c/c++ 库是在/data/data/包名/lib/ 下 1.将安 ...
- BootStrap 智能表单系列 四 表单布局介绍
表单的布局分为自动布局和自定义布局两种: 自动布局就是根据配置项中第二级配置项中数组的长度来自动使用不同的bootstrap栅格,通过设置autoLayout为true可以实现自动布局 自动以布局就是 ...
- js 触摸事件
js触摸事件 应用在移动端 webkit内核都支持. 触摸事件api https://dvcs.w3.org/hg/webevents/raw-file/tip/touchevents.html 事件 ...
- 数组有没有 length()这个方法? String 有没有 length()这 个方法?
1.数组中有length属性. 2.String有lenth()方法.
- Sql日期时间格式转换 备用
sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...
- C#通过WebBrowser快速扒站思路积累大量着陆页列表
现在工作方向已经越来越倾向于项目产品运营相关的东西.对线上运营也有了一定程度的了解. 配合一些技术性的操作,能极大的便利工作中的各种高难度任务,快速提升自我,积累丰富的经验和资源. 以近期制作LP为例 ...