spring的aop实现案例

来源:互联网 发布:心动网络有名吗 编辑:程序博客网 时间:2024/04/20 07:17

1、首先开发业务逻辑接口(因为动态代理只能针对接口所实现)IXiangQinInterface.java

package com.springtest.service;//定义接口public interface IXiangQinInterface {public void meet();}
2、开发实现业务逻辑接口的类 BoyXiangQin.java

package com.springtest.service.impl;import com.springtest.service.IXiangQinInterface;public class BoyXiangQin implements IXiangQinInterface {private String name;@Overridepublic void meet() {System.out.println("姓名是【"+this.name+"】正在相亲");}public String getName() {return name;}public void setName(String name) {this.name = name;}}
3、编写切面类(通用服务类):几种增强 HunJieAspect.java

package com.springtest.util;import org.aspectj.lang.ProceedingJoinPoint;//通用服务类(切面类)public class HunJieAspect {//前置增强,意思就是在业务逻辑方法调用前所植入的通用服务方法//一般这个方法用在权限的检查public void before() {System.out.println("婚戒所开始进行相亲会员的资格审查");}//后置增强,一般就是在业务逻辑方法执行完毕之后所植入的通用方法//一般是用于资源的释放,如数据库连接对象的资源的释放public void after() {System.out.println("这次婚戒所所举办的相亲结束");}//异常处理,因为一旦业务逻辑出现异常,那么就调用该方法//一般主要就是用于日志记录异常public void afterException(Throwable th) {System.out.println("相亲出现了异常"+th.getMessage());}//返回值的增强,一般就是当执行一个业务逻辑方法的时候,有返回值的时候,那么就会植入该方法//一般可以用作日志记录public void afterReturn(Object result) {if(result==null) {System.out.println("相亲失败");}else {System.out.println("相亲成功");}}//环绕增强,就是在业务逻辑方法执行期间所植入的方法。一般用以事务处理。//一般可以用作日志记录public Object around(ProceedingJoinPoint joinpoint) throws Throwable{System.out.println("开始约会了!");Object result = joinpoint.proceed();return result;}}
4、在spring的配置文件里,编写切面类和业务逻辑类的bean application.xml
<!-- 配置切面类的bean --><bean id="myPointCut" class="com.springtest.util.HunJieAspect"></bean><!-- 配置业务逻辑类的bean对象 --><bean p:name="张三" id="boyXiangQin" class="com.springtest.service.impl.BoyXiangQin"></bean>
5、修改applicationContext.xml里的xml的验证,加入aop的命名空间

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:p="http://www.springframework.org/schema/p"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
6、配置切面及切入点,还有增强

<!-- 配置AOP --><!-- aop:config proxy-target-class="false"默认采用jdk的动态代理,只能针对接口的实现 --><!-- aop:config proxy-target-class="true"采用cglib的动态代理,可以直接针对普通的类来实现 --><aop:config><!-- 配置切面 --><aop:aspect id="myaspect" ref="myPointCut"><!-- 配置增强和表达式 --><!-- 配置切面表达式:表达式表示的是项目中哪些类或者哪些方法要被执行增强 --><aop:pointcut expression="execution(* com.springtest.service.impl.*.*(..))" id="mypointexpress"/><aop:before method="before" pointcut-ref="mypointexpress"/><aop:after method="after" pointcut-ref="mypointexpress"/><aop:around method="around" pointcut-ref="mypointexpress"/><!-- afterRunning增强 --><aop:after-returning method="afterReturn" returning="result" pointcut-ref="mypointexpress"/><!-- 配置after-throwing增强 --><aop:after-throwing method="afterException" throwing="th" pointcut-ref="mypointexpress"/></aop:aspect></aop:config>
7、编写测试类 TestUI.java
package com.springtest.service;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.springtest.service.impl.BoyXiangQin;public class TestUI {@Testpublic void testUI() {//首先要获取spring的配置ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");//动态代理只能针对接口实现IXiangQinInterface boyXiang = ctx.getBean("boyXiangQin",IXiangQinInterface.class);boyXiang.meet();}}
8、程序执行结果:

婚戒所开始进行相亲会员的资格审查
开始约会了!
姓名是【张三】正在相亲
相亲失败
这次婚戒所所举办的相亲结束




完整的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:p="http://www.springframework.org/schema/p"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><!-- 配置AOP --><!-- aop:config proxy-target-class="false"默认采用jdk的动态代理,只能针对接口的实现 --><!-- aop:config proxy-target-class="true"采用cglib的动态代理,可以直接针对普通的类来实现 --><aop:config><!-- 配置切面 --><aop:aspect id="myaspect" ref="myPointCut"><!-- 配置增强和表达式 --><!-- 配置切面表达式:表达式表示的是项目中哪些类或者哪些方法要被执行增强 --><aop:pointcut expression="execution(* com.springtest.service.impl.*.*(..))" id="mypointexpress"/><aop:before method="before" pointcut-ref="mypointexpress"/><aop:after method="after" pointcut-ref="mypointexpress"/><aop:around method="around" pointcut-ref="mypointexpress"/><!-- afterRunning增强 --><aop:after-returning method="afterReturn" returning="result" pointcut-ref="mypointexpress"/><!-- 配置after-throwing增强 --><aop:after-throwing method="afterException" throwing="th" pointcut-ref="mypointexpress"/></aop:aspect></aop:config><!-- 配置切面类的bean --><bean id="myPointCut" class="com.springtest.util.HunJieAspect"></bean><!-- 配置业务逻辑类的bean对象 --><bean p:name="张三" id="boyXiangQin" class="com.springtest.service.impl.BoyXiangQin"></bean></beans>








阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 交易猫买家申请了仲裁我的钱怎么办 雷达石英手表表里面有小灰尘怎么办 电脑宽带用户名和密码忘记了怎么办 腾讯会员不让别人在异地登录怎么办 中毒后电脑文件后缀是lnk怎么办 wps逗号隔的空不一样大怎么办 电脑盘里的文件删不了怎么办 在淘宝买的qq账号被找回了怎么办 微信公众号的密码忘了怎么办 公众号安全助手密码忘了怎么办 微博账号存在发布违规信息怎么办 余额宝转出到银行卡被冻结怎么办 银行账户被冻结被转出钱怎么办 从余额宝转出的资金被冻结怎么办 微信账号卖了但是实名认证了怎么办 uc下载文档里的文档全没了怎么办 二手乐视没有账号和密码怎么办 华为账号密码忘了手机卡丢了怎么办 联想平板微信更新后不可兼容怎么办 小米手机刷完机账号密码忘了怎么办 红米手机的小米账号密码忘了怎么办 小米手机账号密码手机号忘了怎么办 小米手机忘了账号和密码怎么办 自己的小米账号密码忘了怎么办 小米手机丢了不记得小米账号怎么办 小米手环账号密码忘了怎么办 阴阳师一个区的账号找不到了怎么办 阴阳师手机账号代练登录了后怎么办 我的微信账号被盗更改密码了怎么办 vivo手机密保密码忘了怎么办 华为手机保密柜忘记密保问题怎么办 支付宝账号突然说没有了怎么办 快手号密码可能被盗登不上该怎么办 海岛奇兵小米版换手机了怎么办 申诉找回微信密码验证吗错误怎么办 微信密码忘了申诉不成功怎么办 微信密码忘了申诉不了怎么办 微信密码忘了申诉不回来怎么办 微信号密码忘了申诉失败怎么办 手机微信密码忘了无需申诉怎么办 安全守护2手机绑定密码错误怎么办