spring学习----aop配置demo

来源:互联网 发布:json 汉字 编辑:程序博客网 时间:2024/06/16 09:08

注意

  aop:aspect   切面类是普通类即可  aop:advisor  切面类必须实现  advice接口   如:MethodBeforeAdvice    AfterReturningAdvice等

定义通知(实现Advice)

public class MyAfterAdvice implements AfterReturningAdvice {    @Override    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {        System.out.println("=========我是afterReturn通知方法=============");    }}
public class MyBeforeAdvice implements MethodBeforeAdvice{    @Override    public void before(Method method, Object[] objects, Object o) throws Throwable {        System.out.println("=========我是前置通知方法=============");    }}

普通通知(不实现Advice)

public class MyNoAdvice {    public void before() throws Throwable {        System.out.println("=============我的普通的before前置方法====================");    }    public void after() throws Throwable {        System.out.println("=============我的普通的After后置方法====================");    }}

业务类

public interface IBussinessService {    void bussiness();    void sayHello();}
public class BussinessServiceImpl implements IBussinessService{    @Override    public void bussiness() {        System.out.println("=========我是业务方法==========");    }    @Override    public void sayHello() {        System.out.println("=========我是say Hello==========");    }}

测试类

@Test    public void test1(){        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:beans.xml");        IBussinessService bussinessService =  applicationContext.getBean("bussinessService",IBussinessService.class);        bussinessService.bussiness();    }

配置文件

<!-- ==========================测试AOP============================= -->    <bean id="myBeforeAdvice" class="com.chenfei.advice.MyBeforeAdvice"></bean>    <bean id="myAfterAdvice" class="com.chenfei.advice.MyAfterAdvice"></bean>    <bean id="myNoAdvice" class="com.chenfei.advice.MyNoAdvice"/>    <bean id="bussinessService" class="com.chenfei.service.impl.BussinessServiceImpl"></bean>    <aop:config>        <aop:pointcut id="pointtest1" expression="execution(* com.chenfei.service.impl.*.*(..))" />        <aop:aspect ref="myNoAdvice">            <aop:before method="before" pointcut-ref="pointtest1"/>            <aop:after method="after" pointcut-ref="pointtest1"/>        </aop:aspect>    </aop:config>    <aop:config>        <aop:pointcut id="pointtest2" expression="execution(* com.chenfei.service.impl.*.*(..))" />        <aop:advisor pointcut-ref="pointtest2" advice-ref="myBeforeAdvice" />        <aop:advisor pointcut-ref="pointtest2" advice-ref="myAfterAdvice" />    </aop:config>

测试结果

这里写图片描述

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 三星关机键失灵怎么办 购机凭证丢了怎么办 滴滴没人接单怎么办 头发干怎么办才好 唯品会东西碎了怎么办 唯品会买东西有质量问题怎么办 来分期不能退货怎么办 商场不肯退衣服怎么办 唯品会衣服坏了怎么办 京东买电视拆机坏的怎么办 余额宝资金冻结怎么办? 余额宝金额冻结怎么办 adidas买了假货怎么办 余额宝冻结金额怎么办 买到不合格地板怎么办 买到翻新地板怎么办 食品里有头发怎么办 家具环保不达标怎么办 买到坏了的食物怎么办 空调检测不合格的怎么办 淘宝卖精仿遇到打假怎么办? 没有收到提货码怎么办 增值税发票超过180天怎么办 花呗借钱还不上怎么办 实体店里没人气怎么办 EMS快件无人签收怎么办 邮政快递丢失了怎么办 快递手机丢了怎么办 拍照签收没收到怎么办 拼多多开店货源怎么办 被购买鉴定投诉怎么办 淘宝上买东西错想用花呗怎么办 淘宝想用花呗买东西差一点怎么办 买了假货怎么办还用吗 免税店买完东西怎么办 淘宝恶意拍单怎么办 二手西服不想要怎么办 苹果8p费电快怎么办 赌博被骗没证据怎么办 异地车子办保险怎么办 淘宝买到旧货怎么办