`
ynp
  • 浏览: 428612 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring学习笔记--------AOP操作

阅读更多
* AOP操作可与通过注解和xml的方式实现;
* 有个问题,如果高手看到了可以考虑下:采用注解实现,用后置通知的时候,如果后置通知方法名叫after()的话,则先会打印最终通知,后打印后置通知!!!,用xml没有这个问题,很怪啊????
* 采用注解方式,切面也必须交给sping管理;

*配置实例

application.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"
		xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<bean id="productService" class="com.xx.dao.impl.ProductServiceBean"></bean> 
	<!-- 注解方式 -->
	<!-- 
	<aop:aspectj-autoproxy/>
	<bean id="myInterceptor" class="com.xx.aspect.MyInterceptorAnnotation"></bean> 
	 -->
	<!-- xml配置方式 -->
	<bean id="myInterceptor" class="com.xx.aspect.MyInterceptorXML"></bean> 
	<aop:config >
		<aop:aspect  ref="myInterceptor">
			<aop:pointcut id="myAop" expression="execution(java.lang.String com.xx.dao.impl.ProductServiceBean.*(..))"/>
			<aop:before pointcut-ref="myAop" method="before"/>
			<aop:after-throwing pointcut-ref="myAop" method="exception"/>
			<aop:after-returning  pointcut-ref="myAop" method="afterReturn"/>
			<aop:after pointcut-ref="myAop" method="after"/>
			<aop:around pointcut-ref="myAop" method="around"/>
		</aop:aspect>
	</aop:config>
</beans>


注解实现的MyInterceptorAnnotation------->

package com.xx.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
 * 切面类(annotation配置实现)
 * @author user
 *
 */
@Aspect
public class MyInterceptorAnnotation {
	@Pointcut("execution(* com.xx.dao.impl.ProductServiceBean.*(..))")
	private void anyMethod(){}//声明一个切入点
	
	@Before("anyMethod()")
	public void before(){
		System.out.println("这是前置通知");
	}
	
	@AfterReturning("anyMethod()")
	public void afterReturn(){
		System.out.println("这是后置通知");
	}
	
	@After("anyMethod()")
	public void after1(){ //如果改为after()的话,则先会打印最终通知,后打印后置通知!!!,用xml没有这个问题,很怪啊。
		System.out.println("这是最终通知");
	}
	
	@AfterThrowing("anyMethod()")
	public void exception(){
		System.out.println("这是例外通知");
	}
	
	@Around("anyMethod()")
	public Object around(ProceedingJoinPoint pjp) throws Throwable{
		System.out.println("进入环绕通知");
		Object result = pjp.proceed();
		System.out.println("退出环绕通知");
		return result;
	}
	
}


xml实现的MyInterceptorXML------->

package com.xx.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

/**
 * 切面类(XML配置实现)
 * @author user
 *
 */
public class MyInterceptorXML {
	
	public void before(){
		System.out.println("这是前置通知");
	}
	public void afterReturn(){
		System.out.println("这是后置通知");
	}
	public void exception(){
		System.out.println("这是例外通知");
	}
	public void after(){
		System.out.println("这是最终通知");
	}
	//环绕通知时固定写法
	public Object around(ProceedingJoinPoint pjp) throws Throwable{
		System.out.println("进入环绕通知");
		Object result = pjp.proceed();
		System.out.println("退出环绕通知");
		return result;
	}
}



结果------->

这是前置通知
进入环绕通知
这是update方法
这是后置通知
这是最终通知
退出环绕通知


分享到:
评论

相关推荐

    spring aop 学习笔记

    NULL 博文链接:https://microjava.iteye.com/blog/525796

    spring ioc aop mvc boot-学习笔记.docx

    自己学习spring课程的笔记。笔记都是根据尚硅谷的课程(spring ioc,spring aop,spring mvc,spring boot等)写的。 主要内容:spring ioc,spring aop,spring mvc,spring boot

    Spring-IOC-AOP.md

    Spring学习笔记,主要包括核心技术IOC和AOP

    Spring Aop 学习笔记

    Spring Aop 学习笔记

    Spring AOP学习笔记

    NULL 博文链接:https://linres.iteye.com/blog/281221

    SpringAop学习笔记以及实现Demo

    SpringAOP学习笔记以及四个可运行的Demo,涵盖经典代理模式、基于注解、基于xml配置这3方面的Demo

    spring-AOP面向切面编程所需jar包.zip

    该压缩包包含--学习笔记(05):轻松搞定Spring全家桶(初识篇)-面向切片编程AOP:--系列博客中新需要的三个jar包。包括spring-aspects-5.2.7.RELEASE.jar和spring-aop-5.2.7.RELEASE.jar和...

    springboot学习思维笔记.xmind

    springboot学习笔记 spring基础 Spring概述 Spring的简史 xml配置 注解配置 java配置 Spring概述 Spring的模块 核心容器CoreContainer Spring-Core Spring-Beans ...

    Spring学习笔记(15)----使用Spring的注解方式实现AOP

    NULL 博文链接:https://coolszy.iteye.com/blog/540465

    Spring学习笔记(16)----使用Spring配置文件实现AOP

    NULL 博文链接:https://coolszy.iteye.com/blog/541997

    javaSpring学习笔记

    在“Java Spring学习笔记”中,你将找到对Spring框架的全面介绍,包括IoC(控制反转)和DI(依赖注入)原理、AOP(面向切面编程)、Spring MVC、Spring Boot等核心内容。每个主题都结合了理论知识和实际示例,帮助你...

    Spring-Kapcb:Spring学习

    记录自己关于Spring的学习笔记还有Demo ,笔记均在Demo注释部分,后续会整理成MarkDown格式的文件发布在博客上。 如果觉得对您有帮助,您的Start就是对我最大的认同。 欢迎互相学习交流,如果项目中有问题的部分可以...

    Spring学习笔记(14)----使用CGLIB实现AOP功能

    NULL 博文链接:https://coolszy.iteye.com/blog/540457

    xmljava系统源码-spring-cloud-kuang:狂神springcloud笔记+源码+config

    SpringCloud笔记 1、前言 1.1、回顾 回顾之前的知识 JavaSE 数据库 前端 Servlet Http Mybatis Spring SpringMVC SpringBoot Dubbo、Zookeeper、分布式基础 Maven、Git Ajax、Json ... 串一下自己会的东西 数据库 ...

    Spring入门笔记.md

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ...

    Spring学习笔记

    Spring学习笔记Spring spring的配置 IOC 依赖注入 基于Xml的注入 基于注释的注入 Spring的自动注入和属性自动注入 AOP 静态代理 动态代理 使用spring实现AOP 基于Annotation实现AOP 基于XML实现AOP ...

    spring学习笔记

    spring从HelloWorld到ioc,aop,对JDBC,hibernate,struts1,struts2的支持笔记

    Spring的学习笔记

    第八课:Spring AOP配置选项 21 一、 AOP配置annotation方式 21 (一) 搭建annotation开发环境 21 (二) aspectJ类库 22 (三) AOP的annotation实例 22 (四) AspectJ的专业术语 23 (五) 织入点语法 23 (六) Advice 24 ...

    Spring技术内幕学习笔记.docx

    《Spring技术内幕》学习笔记1——IoC容器体系结构 《Spring技术内幕》学习笔记2——IoC定位Bean定义资源 《Spring技术内幕》学习笔记3——IoC容器载入Bean定义资源文件 ...《Spring技术内幕》学习笔记7——AOP基础

Global site tag (gtag.js) - Google Analytics