12、基于XML方式的AOP
2016-05-22 17:45
351 查看
创建Bean
//首先创建业务接口 package com.codestd.springstudy.aop.xml.service; public interface UserService { public void login(); public void logout() throws Exception; } //创建实现类 package com.codestd.springstudy.aop.xml.service; public class UserServiceImpl implements UserService { @Override public void login() { System.out.println("User login"); } @Override public void logout() throws Exception { System.out.println("User logout"); throw new Exception("Exception test"); } } //创建切面类 package com.codestd.springstudy.aop.xml; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; public class AspectBean { public void doAfter(JoinPoint jp){ System.out.println("do after"); } public void doBefore(JoinPoint jp){ System.out.println("do after"); } public Object doAround(ProceedingJoinPoint jp) throws Throwable{ System.out.println("do Around"); Object obj = jp.proceed(); return obj; } public void doThrowing(JoinPoint jp,Exception e){ System.out.println(e.getClass().getName()+"|->|"+e.getMessage()); } }
配置AOP
下面使用XML配置AOP<bean id="userService" class="com.codestd.springstudy.aop.xml.service.UserServiceImpl" /> <bean id="aspectBean" class="com.codestd.springstudy.aop.xml.AspectBean" /> <aop:config> <aop:aspect id="aspect" ref="aspectBean"> <aop:pointcut expression="execution(* com.codestd.springstudy.aop.xml.service.*.*(..))" id="servicePointcut"/> <aop:after method="doAfter" pointcut-ref="servicePointcut" /> <aop:before method="doBefore" pointcut-ref="servicePointcut"/> <aop:around method="doAround" pointcut-ref="servicePointcut"/> <aop:after-throwing method="doThrowing" pointcut-ref="servicePointcut" throwing="e"/> </aop:aspect> </aop:config>
doAround方法要有返回值,并且类型是Object,接收的参数类型为
ProceedingJoinPoint。
aop:after-throwing中需要使用
throwing指定异常的参数名,
doThrowing(JoinPoint jp,Exception e)中的
e。
测试
测试方法如下package com.codestd.springstudy.aop.xml.service; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:aop/applicationContext.xml"}) public class UserServiceImplTest { @Autowired private UserService userService; @Test public void test() throws Exception { this.userService.login(); this.userService.logout(); } }
测试结果如下
do after do Around User login do after do after do Around User logout do after java.lang.Exception|->|Exception test
使用XML方式配置引入
<aop:config> <aop:aspect> <aop:declare-parents types-matching="com.codestd.springstudy.aop.xml.service.UserService+" implement-interface="com.codestd.springstudy.aop.introductions.Flyable" default-impl="com.codestd.springstudy.aop.introductions.DefaultFlyable"/> </aop:aspect> </aop:config>
测试类如下
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:aop/applicationContext.xml"}) public class UserServiceImplTest { @Autowired private Flyable flyable; @Test public void test() throws Exception { this.flyable.fly(); } }
相关文章推荐
- 11、AOP之引入
- shell中[[]]和[]的主要区别
- 8、AOP切入点详解
- centos7 怎么卸载 用源代码包安装的codeblocks
- Given final block not properly padded
- Tomcat 系统架构与设计模式,第 2 部分: 设计模式分析
- Linux学习之通配符
- 构建安全的apache+mysql+php的web服务器
- OpenResty上各种测试用例实操(1)
- (转)hadoop多文件格式输入
- Bluemix中的Apache Spark数据分析服务入门
- 如何自己搭建钓鱼网站检测系统
- Linux-常用文件夹及目录作用
- 在vop中重构transform节点
- Tomcat 系统架构与设计模式,第 1 部分: 工作原理
- centos6.5安装jdk,tomcat
- Red Hat Enterprise Linux 7.0 安装方法
- Tomcat体系架构
- Cooperation.GTST团队第二周项目总结
- CentOS 7.1编译安装PHP7