Spring Aop的理解和简单实现

1、AOP概念
所说的面向切面编程其实就是在处理一系列业务逻辑的时候这一系列动作看成一个动作集合。比如连接数据库来说:
加载驱动-----获取class--------获取连接对象-------访问数据库------查询---------操作结果
对于上面的这一系列动作我们把其中的虚线看成是一个个的切面。然后我们在虚线的位置上加入一些逻辑。哪怕是日志,这也就成就了在不知不觉中将逻辑处理加入到了相应的位置上。而形成了所谓的面向切面编程!
 
 
下面通过@Before演示Aop织入到方法之前执行一些逻辑
 
[html] view plain copy
 
  1. AOP的应用:  
  2. Xml配置  这里的xml配置引入了aop的xsd语法约束。另外加入的aop的自动代理标签。请看注释  
  3. <?xml version="1.0" encoding="UTF-8"?>  
  4. <beans xmlns="http://www.springframework.org/schema/beans";  
  5.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";  
  6.        xmlns:context="http://www.springframework.org/schema/context";  
  7.        xmlns:aop="http://www.springframework.org/schema/aop";  
  8.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"; >  
  14.     <context:annotation-config/>  
  15.     <!-- 配置容器资源扫描的包 -->  
  16.    <context:component-scan base-package="com.spring" />  
  17.     <!-- 自动产生代理,内部实现是用AspectJ实现。可以使用aspectj的注解产生代理 -->  
  18.     <aop:aspectj-autoproxy />  
  19. </beans>  
 
 
 
加入Jar包:
1、编写被注入的方法。必须得是spring容器管理的对象
 
[java] view plain copy
 
  1. import org.springframework.stereotype.Component;  
  2. import com.spring.dao.UserDao;  
  3.   
  4. @Component("userService")  
  5. public class UserServiceImpl implements UserService{  
  6.     private UserDao userDao;  
  7.       
  8.     public void setUserDao(UserDao userDao) {  
  9.         this.userDao = userDao;  
  10.     }  
  11.       
  12.     //在下面方法前面加逻辑  
  13.     public void HelloWorld(){  
  14.         System.out.println("helloworld");  
  15.     }  
  16. }  
2、织入方法的配置:
 
[java] view plain copy
 
  1. package com.spring.aop;  
  2.   
  3. import org.aspectj.lang.annotation.Aspect;  
  4. import org.aspectj.lang.annotation.Before;  
  5. import org.springframework.stereotype.Component;  
  6.   
  7. //声明切面  
  8. @Aspect  
  9. //加入Spring管理容器  
  10. @Component("LogInterceptor")  
  11. public class LogInterceptor {  
  12.     //指定织入的方法。  
  13.     @Before("execution(public void com.spring.service.UserServiceImpl.HelloWorld())")  
  14.     public void BeforeMethod(){  
  15.         System.out.println("method start!");  
  16.     }  
  17. }  
  18.   
  19. 测试aop的织入:  
  20. public class SpringTest {  
  21.     @Test  
  22.     public void test01() {  
  23.         BeanFactory applicationContext = new ClassPathXmlApplicationContext(  
  24.                 "beans.xml");  
  25.         UserService user = (UserService) applicationContext.getBean("userService");  
  26.         user.HelloWorld();  
  27.     }  
  28. }  
 
 
 
注意:
在编写过程中的织入点语法上指定before织入哪个方法的前面。而括号中的这个被指定的织入点最好是实现一个接口。如果不实现接口的话就会报异常为:

Cannot proxy target class because CGLIB2 is not available.Add CGLIB to the class path or specify proxy interfaces

这个异常的解决办法为:
加入cglib.jar。因为被织入对象的方法单位如果没有实现接口的话它就需要cglib.jar的支持。
 
AOP基本概念:
JoinPoint:切入点、可以理解为上面案例的HelloWorld方法之前就为一个Joinpoint
Pointcut:切入点的集合,可以通过织入点语法定义N个切入点加入逻辑处理。
Aspect:切面,指的是切面的类。也就是上面声明Aspect的逻辑集合
Advise:指的是切面上的逻辑比如@Before、@After
Target:被代理对象.上面的案例指的是UserServiceImpl
Weave:织入
posted @ 2017-11-02 14:30  疯狂的小码农  阅读(937)  评论(0编辑  收藏  举报