Spring 4初识aop

aop:面向切面编程,以前的编程都是从上往下顺序执行,我对于aop的理解,就是像一个圆柱形,横着切一刀,在他原来的地方的前,后,等多个位置,自己加载上另外的方法,使其更加强大。

其中有多个东西,jointpoint,切面切点等;

案例:spring创建的bean中,在xml文件进行配置。

切面:

public class advice {

    //切面
    private void dobefore(JoinPoint jp) {
        System.out.println(jp.getClass());
        System.out.println("dobefore....");
    }
    private void doafter() {
        // TODO Auto-generated method stub
        System.out.println("doafter....");

    }
}
bean:

public class aopServiceImpl implements aopService{

    @Override
    public void doing() {
            System.out.println("doing....");
    }

}
xml文件:

  	<aop:config>
  		<aop:aspect id="qiemian" ref="advice">
  		<aop:pointcut expression="execution(* com.cai.service.*.*(..))" id="qiedian"/>
  		<aop:before method="dobefore" pointcut-ref="qiedian"/>
   		</aop:aspect>
  	</aop:config>
  	
  	<bean id="advice" class="com.cai.aop.advice" ></bean>
  	
  	<bean id="aopService" class="com.cai.service.impl.aopServiceImpl"></bean>
execution表达式( 返回值 扫描的包 (参数--两个点为不限制参数个数等))

test:

public class aoptest {

    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        aopService aop=(aopService) ac.getBean("aopService");
        aop.doing();
//      test t1= (test) ac.getBean("mytest");
//       t1.mydo();
    }
}
在切面里定义切点,然后调用方法,意思就是com.cai.service.*.*里的类建出的springbean,都会执行前调用dobefore方法

直接创建类test,不实现接口的方式调用,失败了,没有调用dobefore方法,看来基层结构还是需要研究才能学懂。


前置通知,后置通知差不多

有个环绕通知:

public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
		System.out.println("添加学生前");
		Object retVal=pjp.proceed();
		System.out.println(retVal);
		System.out.println("添加学生后");
		return retVal;
	}

实现结果是:前置,环绕前置,过程,环绕后置,后置


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值