Spring面向切面的简单示例(基于XML文件配置)
来源:互联网 发布:推荐淘宝男装店铺 编辑:程序博客网 时间:2024/06/07 00:09
1. 建立工程,导入Spring AOP所需的jar包。
2. Spring配置文件applicationContext.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" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><!-- 自动扫描指定包及其子包下的所有Bean类 --><context:component-scan base-package="com.huey" /><aop:config><!-- 定义一个切入点 --><aop:pointcut id="myPointcut" expression="execution(* com.huey.service.*.*(..))"/><!-- 定义切面,ref属性指定所引用的普通Bean作为切面Bean --><aop:aspect id="logAspect" ref="logAspect"><!-- 定义前置通知 --><aop:before pointcut-ref="myPointcut" method="doBefore"/><!-- 定义后置通知 --><aop:after-returning pointcut-ref="myPointcut" method="doAfterReturning" returning="rvt"/><!-- 定义异常通知 --><aop:after-throwing pointcut-ref="myPointcut" method="doAfterThrowing" throwing="ex"/><!-- 定义最终通知 --><aop:after pointcut-ref="myPointcut" method="doAfter"/><!-- 定义环绕通知 --><aop:around pointcut-ref="myPointcut" method="doAround"/></aop:aspect></aop:config></beans>
3. 定义切面Bean:
package com.huey.aspect;import org.aspectj.lang.ProceedingJoinPoint;import org.springframework.stereotype.Component;/** * 切面类 * @author huey2672 * */@Component("logAspect")public class LogAspect {/** * 前置通知 */public void doBefore() {System.out.println("Before Advice");}/** * 后置通知 */public void doAfterReturning(Object rvt) {System.out.println("AfterReturning Advice");System.out.println("目标方法的返回值:" + rvt);}/** * 异常通知 */public void doAfterThrowing(Throwable ex) {System.out.println("AfterReturning Advice");System.out.println("目标方法中抛出的异常:" + ex);}/** * 最终通知 */public void doAfter() {System.out.println("After Advice");}/** * 环绕通知 */public Object doAround(ProceedingJoinPoint jp) throws Throwable {System.out.println("Around Advice 在目标方法之前织入");// 获取目标方法的参数,可以对其进行修改Object[] args = jp.getArgs();// 执行目标方法,并得到返回值,可以对其进行修改Object rvt = jp.proceed(args);System.out.println("Around Advice 在目标方法之后织入");return rvt;}}
4. 目标类:
package com.huey.service;import org.springframework.stereotype.Component;/** * * @author Huey * */@Component("helloService")public class HelloService {public void sayHello(String name) {System.out.println("Hello " + name);}}
5. 测试用例:
package com.huey.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.huey.service.HelloService;/** * * @author huey2672 * */public class AopTest {private HelloService helloService;{// 创建Spring容器ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");// 通过容器获得EntityServ的实例helloService = appCtx.getBean("helloService", HelloService.class);}@Testpublic void testAop() throws Exception {helloService.sayHello("sugar");}}
6. 结果输出:
Around Advice 在目标方法之前织入Before AdviceHello sugarAround Advice 在目标方法之后织入After AdviceAfterReturning Advice目标方法的返回值:null
- Spring面向切面的简单示例(基于XML文件配置)
- Spring面向切面的简单示例(基于Annotation配置)
- Spring依赖注入的简单示例(基于XML文件配置)
- spring framework面向切面编程示例(xml配置):spring-aop
- Spring之旅、装配Bean、最小化Spring XML配置、面向切面的Spring、征服数据库、事务管理示例源代码
- 基于xml配置的Spring AOP切面的几点总结,以及简单的例子
- spring添加AOP切面-基于XML配置
- 第4章 面向切面的Spring--xml配置切面 笔记2
- spring framework面向切面编程示例(基于注解):spring-aop
- spring实战-基于注解的面向切面编程(AOP)
- Spring:使用JdbcTemplate的简单实例-基于XML文件配置
- Spring AOP(3)基于XML配置实现的示例
- Spring AOP(3)基于XML配置实现的示例
- Spring Aop切面基于Xml的学习
- spring学习笔记8--使用spring进行面向切面的(AOP)编程(2)XML配置方式
- Spring 面向切面,简单入门(AOP)
- Spring依赖注入的简单示例(基于注解配置)
- 基于spring注解切面的简单实现
- 滨州学院CSDN高校俱乐部第二次公开课——微信公众平台开发&SAE使用
- Unix的缺陷
- PKU Online Judge 1054:Cube (设置根节点)
- 往resin 服务器中发送post 报 POST requires content-length
- Linux 内核延时函数
- Spring面向切面的简单示例(基于XML文件配置)
- IE 浏览器的创新
- 全波整流电路
- 软件设计师--计算机系统--浮点数据存储
- Makefile教程
- Django 1.5.4 专题16 logging
- 静态路由 动态路由 默认路由 默认网关
- 【玩转cocos2d-x之七】场景类CCScene和布景类CCLayer
- linux打开文件的权限设置 数字含义