前面介绍了下Spring中的切面技术。如今说下採用注解的方式进行切面

首先肯定和之前的一样。须要一个自己主动代理的注解类 AnnotationAwareAspectJAutoProxyCreator

配置文件里的代码:

<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="person" class="cn.aop.aspectj2.Person"></bean>
<!-- 自己主动代理注解 <span style="font-family: Arial, Helvetica, sans-serif;">自己主动去查找带有注解的类和方法,和之前的那个自己主动代理类差点儿相同,原理大概是遍历全部的带注解的类,然后进行一一解析。。</span>
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean>
-->
<!-- 注解自己主动标签,自己主动去查找带有注解的类和方法 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 注解的切面 须要自己写的切面了===切点+ 通知 在该类中相对之前,比較简单些-->
<bean class="cn.aop.aspectj2.MyAdvisor"></bean> </beans>

在配置文件里,我们不难发现,最基本都是 一个被代理的对象。自己主动代理类,切面(能够是自己写的类,这个注解就是自己写的,当然在之前的那个就是用aop里面的)。我们须要写一个自己的切面类

切面类

package cn.aop.aspectj2;

import org.aspectj.lang.JoinPoint;
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; @Aspect
public class MyAdvisor {
private final String CUT="execution(* cn.aop.aspectj2.*.*(..))"; /*
* 在核心代码之前运行
*/
@Before(CUT)
public void advice(){
System.out.println("单独之前拦拦");
} /*
* 在核心代码之后运行,正常退出或者异常都会调用这个
*/
@After(CUT)
public void advice2(){
System.out.println("单独之后拦拦");
} /*
* around通知最好不用(能够用before+after来取代),由于參数依赖---但也有优点。
* 就是获得被拦截对象和对应方法的信息
*/
@Around(CUT)
public void around(ProceedingJoinPoint p) throws Throwable{
System.out.println("这是围绕前 "+p.getKind()+","+p.getTarget());
p.proceed();
System.out.println("围绕后");
} /*
* 是针对某一个函数
* 在前面函数运行完毕之后,这个才会运行,
* 可是方法中当出现异常之后,这个就不会运行的,正常退出才会运行
*/
@AfterReturning(CUT)
public void afterR(JoinPoint jp ){
System.out.println("全部之后"+jp.getKind());
} @AfterReturning(CUT)
public void after2(){
System.out.println("全部之后222");
} /*
* 这个在出现异常之后调用。前提是==------前面的代码没有抓住这个异常,
* 前面抓异常之后。这里是不会调用的,一般在日志里面用的比較多。
*
*/
@AfterThrowing(CUT)
public void throwex() throws Exception{
System.out.println("出异常了");
} }

person类

package cn.aop.aspectj2;

public class Person {

	public void say(){

//			Integer.parseInt("aa");//測试是否出异常
System.out.println("...这是say..");
} public void run(){
System.out.println("这是person中的 run方法");
} }

測试类

public class Demo2 {

	@Test
public void test(){//相同的从容器中拿到配置文件,取出对象
ApplicationContext app = new ClassPathXmlApplicationContext("cn/aop/aspectj2/aspectj2.xml");
Person p=app.getBean(Person.class);
p.run();
p.say();
}
}

javaEE之------Spring-----》 AspectJ注解的更多相关文章

  1. SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP

    AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP之所以能得到广泛应用,主要是因为它将应用系统拆分分了2个部分 ...

  2. SSM-Spring-17:Spring中aspectJ注解版

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- AspectJ AspectJ是一个面向切面的框架,它扩展了Java语言,定义了AOP 语法,能够在编译期提供 ...

  3. Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探

    由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...

  4. Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

    本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...

  5. spring AOP 之二:@AspectJ注解的3种配置

    @AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...

  6. Spring使用AspectJ注解和XML配置实现AOP

    本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...

  7. JAVA 常用注解( JDK, Spring, AspectJ )

    JDK自带注解   @Override   表示当前方法覆盖了父类的方法   @Deprecation   表示方法已经过时,方法上有横线,使用时会有警告   @SuppviseWarnings    ...

  8. Spring学习(十八)----- Spring AOP+AspectJ注解实例

    我们将向你展示如何将AspectJ注解集成到Spring AOP框架.在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法. 常见AspectJ的注解: @Before – 方法 ...

  9. Spring学习--用 ASpectJ 注解实现 AOP

    用 AspectJ 注解声明切面: 要在 Spring 中声明 AspectJ 切面 , 只需要在 IOC 容器中将切面声明为 bean 实例.当在 Spring IOC 容器中初始化 AsjectJ ...

随机推荐

  1. Laravel API 允许跨域访问

    服务器A请求服务器B的接口,那么一般会出现跨域问题.全解跨域请求处理办法 XMLHttpRequest cannot load http://api.console.vms3.com/api/user ...

  2. caioj 1072 动态规划入门(二维一边推5:最长公共子序列 LCSS加强版)

    在51nod刷到过同样的题,直接秒杀 见https://blog.csdn.net/qq_34416123/article/details/81697683 #include<cstdio> ...

  3. 题解 P2195 【HXY造公园】

    天哪这道题竟然只有一篇题解! emm,首先读题看完两个操作就已经有很明确的思路了,显然是并查集+树的直径 一波解决. 并查集不多说了,如果不了解的可以看这里. 树的直径的思路很朴实,就是两边DFS(B ...

  4. zookeeper_相关命令 以及 API

    (区分大小写) 启动ZooKeeper服务        进入主目录下的 /bin 文件夹. zkServer.sh start.  需要每个节点运行启动命令 客户端启动          zkCli ...

  5. oracle 10g/11g RAC 启停归档模式

     oracle 10g rac 启停归档模式    假设Oracle数据库执行在归档模式,当进行数据库维护时,可能须要暂停数据库的归档,在完毕维护后,再又一次启动归档模式. 通过下面步骤能够从归档 ...

  6. vue12 循环添加重复数据

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. windows “文件大小”与“占用空间”、文件系统与文件拷贝

    0. 文件大小与占用空间 "文件大小"和"占用空间"的差别 首先需要明确的是,"文件大小"代表着文件的真实大小(文件内容实际包含的全部字节数 ...

  8. 有关R6034错误的思考

    作者:朱金灿 来源:http://blog.csdn.net/clever101 我们有时会遇到R6034错误,工程明明编译通过,但是运行时却出现: 网上的解决办法很多,但是有效的不多,特别是对阐述这 ...

  9. Android Unknown failure (Failure - not installed for 0)

    File->Settings->Build,Execuion,Deployment->Instant Run然后把Enable Instant Run to...那一项的勾去掉就好了

  10. Python正则表达式初识(二)

    前几天给大家分享了Python正则表达式初识(一),介绍了正则表达式中的三个特殊字符“^”.“.”和“*”,感兴趣的伙伴可以戳进去看看,今天小编继续给大家分享Python正则表达式相关特殊字符知识点. ...