Aspectj 概念:

1. joinpoint:切入点, 比如@Before, @After, @Around

2. Pointcut:切入点集合, 比如 @Pointcut("execution(public * com.bjsxt.service..*.*(..))")

  public void myMethod(){};  //切入点集合的名字

3. Aspect:切面逻辑, 切面类, 比如LogInterceptor之前加入的@Aspect

4. Advice: 加入点的建议, 类似于Aspect的逻辑, 相当于@Before, 加在切入点的建议.

5. Target: 被代理对象, 逻辑织入哪里...

6. Weave: 织入

AOP的Annotation方式:

1. 加上对应的xsd文件sprin-aop.xsd

2. beans.xml <aop:aspectj-autoproxy/>

3. 此时就可以解析对应的annotation了

4. 建立我们的拦截类

5. 用@Aspect注解这个类

6. 用@Before来注解方法

7. 写明白切入点(execution...)

8. 让spring对我们的拦截器类进行管理 @Component

常见的annotation:

1. @Pointcut

2. @Before

3. @AfterReturning

4. @AfterThrowing

5. @After

6. @Around

beans.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>
<aop:aspectj-autoproxy />
</beans>

LogInterceptor.java: 注意@Component

package com.bjsxt.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Aspect
@Component
public class LogInterceptor {
@Before("execution(public * com.bjsxt.dao..*.*(..)))")
public void before() {
System.out.println("method before");
}
@AfterReturning("execution(public * com.bjsxt.dao..*.*(..)))")
public void AfterReturning() {
System.out.println("method after returning");
}
}

  

简化成下面写法:

package com.bjsxt.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Aspect
@Component
public class LogInterceptor {
@Pointcut("execution(public * com.bjsxt.dao..*.*(..))")
public void myMethod(){};

@Before("myMethod()")
public void before() {
System.out.println("method before");
}
@AfterReturning("myMethod()")
public void AfterReturning() {
System.out.println("method after Returning");
}
}

  

UserServiceTest.java:

package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.model.User; //Dependency Injection
//Inverse of Control
public class UserServiceTest { @Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService)ctx.getBean("userService");
System.out.println(service.getClass());
service.add(new User());
ctx.destroy();
}
}

UserService.java:

package com.bjsxt.service;
import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User; @Component("userService")
public class UserService { private UserDAO userDAO; public void init() {
System.out.println("init");
} public void add(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
} @Resource(name="u")
public void setUserDAO( UserDAO userDAO) {
this.userDAO = userDAO;
}
public void destroy() {
System.out.println("destroy");
}
}

UserDAOImpl.java:

package com.bjsxt.dao.impl;

import org.springframework.stereotype.Component;

import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User; @Component("u")
public class UserDAOImpl implements UserDAO { public void save(User user) {
//Hibernate
//JDBC
//XML
//NetWork
System.out.println("user saved!");
//throw new RuntimeException("exeption!");
} }

UserDAO.java:

package com.bjsxt.dao;
import com.bjsxt.model.User;
public interface UserDAO {
public void save(User user);
}

User.java:

package com.bjsxt.dao;
import com.bjsxt.model.User; public interface UserDAO {
public void save(User user);
}

 

结果:

class com.bjsxt.service.UserService
method before
user saved!
method after Returning

  

LogInterceptor.java的around用法:

package com.bjsxt.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Aspect
@Component
public class LogInterceptor {
@Pointcut("execution(public * com.bjsxt.dao..*.*(..))")
public void myMethod(){}; @Before("myMethod()")
public void before() {
System.out.println("method before");
} @Around("myMethod()")
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("method around start");
pjp.proceed();
System.out.println("method around end");
} }

beans.xml: XML catalog里引入 aop的 xsd

<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>
<aop:aspectj-autoproxy />
</beans>

UserServiceTest.java:

package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.model.User; //Dependency Injection
//Inverse of Control
public class UserServiceTest { @Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService)ctx.getBean("userService");
System.out.println(service.getClass());
service.add(new User());
service.delete();
ctx.destroy(); } }

UserService.java:

package com.bjsxt.service;
import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User; @Component("userService")
public class UserService { private UserDAO userDAO; public void init() {
System.out.println("init");
} public void add(User user) {
userDAO.save(user);
}
public void delete() {
userDAO.delete();
}
public UserDAO getUserDAO() {
return userDAO;
} @Resource(name="u")
public void setUserDAO( UserDAO userDAO) {
this.userDAO = userDAO;
}
public void destroy() {
System.out.println("destroy");
}
}

UserDAOImpl.java:

package com.bjsxt.dao.impl;

import org.springframework.stereotype.Component;

import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User; @Component("u")
public class UserDAOImpl implements UserDAO { public void save(User user) {
System.out.println("user saved!");
//throw new RuntimeException("exeption!");
}
public void delete() {
System.out.println("user delete!");
//throw new RuntimeException("exeption!");
}
}

UserDAO.java  :

package com.bjsxt.dao;
import com.bjsxt.model.User;
public interface UserDAO {
public void save(User user);
public void delete();
}

  

结果:

class com.bjsxt.service.UserService$$EnhancerByCGLIB$$b6531e3e
method around start
method before
user saved!
method around end
method around start
method before
user delete!
method around end

  

 

改成service的话, 因为没有实现接口, 所以需要 引入一个包: cglib-nodep-2.1_3.jar

package com.bjsxt.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Aspect
@Component
public class LogInterceptor {
@Pointcut("execution(public * com.bjsxt.service..*.add(..))")
public void myMethod(){}; @Before("myMethod()")
public void before() {
System.out.println("method before");
} @Around("myMethod()")
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("method around start");
pjp.proceed();
System.out.println("method around end");
} }

结果:

class com.bjsxt.service.UserService$$EnhancerByCGLIB$$9160c20d
method around start
method before
user saved!
method around end

  

  

  

  

  

Sping--AOP--Annotation的更多相关文章

  1. 基于注解的Sping AOP详解

    一.创建基础业务 package com.kang.sping.aop.service; import org.springframework.stereotype.Service; //使用注解@S ...

  2. AOP annotation

    1.xml文件 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http ...

  3. Sping AOP

    Sping AOP 1.什么是AOP 面向切面编程(AOP) 是 面向对象编程的补充(OOP) 传统的业务处理代码中,通常会惊醒事务处理.日志处理等操作.虽然可以使用OOP的组合或继承来实现代码重用, ...

  4. [置顶] 使用sping AOP 操作日志管理

    记录后台操作人员的登陆.退出.进入了哪个界面.增加.删除.修改等操作 在数据库中建立一张SYSLOG表,使用Sping 的AOP实现日志管理,在Sping.xml中配置 <!-- Spring ...

  5. 使用AOP+Annotation实现操作日志记录

    先创建注解 OperInfo @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @ ...

  6. Sping AOP初级——入门及简单应用

    在上一篇<关于日志打印的几点建议以及非最佳实践>的末尾提到了日志打印更为高级的一种方式——利用Spring AOP.在打印日志时,通常都会在业务逻辑代码中插入日志打印的语句,这实际上是和业 ...

  7. 基于XML配置的Sping AOP详解

    一.编写基本处理方法 package com.kang.sping.xml.aop; public class Math{ //加 public int add(int n1,int n2){ int ...

  8. sping aop 源码分析(-)-- 代理对象的创建过程分析

    测试项目已上传到码云,可以下载:https://gitee.com/yangxioahui/aopdemo.git 具体如下: public interface Calc { Integer add( ...

  9. 尚学堂Spring视频教程(六):AOP Annotation

    此处省略N个字.... 直接看下面 推荐链接: Spring Aop实例之AspectJ注解配置

  10. Sping AOP Capabilities and Goals

    Spring AOP是用纯的java实现的.不需要任何个性的实现过程.Spring AOP不需要控制类加载器,并且它适用于Servlet容器或者应用服务器. Spring AOP当前只支持方法执行的连 ...

随机推荐

  1. PHP数学函数试题

    1.求绝对值的函数是什么? 2.在任意进制之间转换数字的函数是什么? 3.二进制转换为十进制,十进制转换为二进制,十六进制转换为十进制,十进制转换为十六进制,八进制转换为十进制,十进制转换为八进制的函 ...

  2. 1*Json对象声明简单,复合,对象数组

    //简单JSON对象 function btn1_click() { var json = { "id": 1001, "name": "张三&quo ...

  3. Mybatis 一对一,一对多,多对一,多对多的理解

    First (一对一) 首先我来说下一对一的理解,就是一个班主任只属于一个班级,一个班级也只能有一个班主任.好吧这就是对于一对一的理解 怎么来实现呢? 这里我介绍了两种方式: 一种是:使用嵌套结果映射 ...

  4. 8.1 sikuli报错: 提示没有对应的javaw

    对于sikuli,需要安装32位的jdk且不能高于1.7的版本. 对于64位系统的C盘,Program Files文件夹是64位的,Program File(x86)文件夹是32位的 需要安装一个32 ...

  5. mybatis 的一点问题

    写法1:   public User queryUserByUsername(String username); 写法2:   public User queryUserByUsername(@Par ...

  6. JAVA-基本知识

    1.JAVA跨平台 其实就是在每个平台上要安装对应该操作系统的JVM,JVM负责解析执行,即实现了跨平台.JVM是操作系统与java程序之间的桥梁. 2.JRE:java运行环境,包含JVM+核心类库 ...

  7. 转:Selenium中的几种等待方式,需特别注意implicitlyWait的用法

    最近在项目过程中使用selenium 判断元素是否存在的时候 遇到一个很坑爹的问题, 用以下方法执行的时候每次都会等待很长一段时间,原因是因为对selenium实现方法了解不足导致一直找不到解决方法. ...

  8. Java8学习笔记----Lambda表达式 (转)

    Java8学习笔记----Lambda表达式 天锦 2014-03-24 16:43:30 发表于:ATA之家       本文主要记录自己学习Java8的历程,方便大家一起探讨和自己的备忘.因为本人 ...

  9. leetcode371

    我这道题目真的是划水的,因为弄了很长时间发现,我可能对于位操作不是特别喜欢吧. 确实为了最求速度,位操作确实快一些. 单独从题目意思来说,用别的方式实现加法,我觉得吧,真的有点醉了...就这样. 下面 ...

  10. L5,no wrong numbers

    expressions: up to now,到现在为止 a great many,数量很大 in a way,在某种意义上说   words: burn,vt燃烧,vi烧毁,n灼烧 obtain,v ...