Spring入门Blog[七、Spring Aop的理解和简单实现]

来源:互联网 发布:word2010 mac 破解版 编辑:程序博客网 时间:2024/05/18 05:00

转自:http://blog.csdn.net/zhang6622056/article/details/7700017

1、AOP概念

所说的面向切面编程其实就是在处理一系列业务逻辑的时候这一系列动作看成一个动作集合。比如连接数据库来说:

加载驱动-----获取class--------获取连接对象-------访问数据库------查询---------操作结果

对于上面的这一系列动作我们把其中的虚线看成是一个个的切面。然后我们在虚线的位置上加入一些逻辑。哪怕是日志,这也就成就了在不知不觉中将逻辑处理加入到了相应的位置上。而形成了所谓的面向切面编程!



下面通过@Before演示Aop织入到方法之前执行一些逻辑

AOP的应用:Xml配置  这里的xml配置引入了aop的xsd语法约束。另外加入的aop的自动代理标签。请看注释<?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.spring" />   <!-- 自动产生代理,内部实现是用AspectJ实现。可以使用aspectj的注解产生代理 -->   <aop:aspectj-autoproxy /></beans>

加入Jar包:

1、编写被注入的方法。必须得是spring容器管理的对象

[java] view plaincopy
  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、织入方法的配置:

package com.spring.aop;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;//声明切面@Aspect//加入Spring管理容器@Component("LogInterceptor")public class LogInterceptor {//指定织入的方法。@Before("execution(public void com.spring.service.UserServiceImpl.HelloWorld())")public void BeforeMethod(){System.out.println("method start!");}}测试aop的织入:public class SpringTest {@Testpublic void test01() {BeanFactory applicationContext = new ClassPathXmlApplicationContext("beans.xml");UserService user = (UserService) applicationContext.getBean("userService");user.HelloWorld();}}

注意:

在编写过程中的织入点语法上指定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:织入


2 0
原创粉丝点击