当前位置:编程学习 > JAVA >>

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

1、AOP概念

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

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

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

 

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

[html] 
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] 
import org.springframework.stereotype.Component; 
import com.spring.dao.UserDao; 
 
@Component("userService") 
public class UserServiceImpl implements UserService{ 
    private UserDao userDao; 
     
    public void setUserDao(UserDao userDao) { 
        this.userDao = userDao; 
    } 
     
    //在下面方法前面加逻辑 
    public void HelloWorld(){ 
        System.out.println("helloworld"); 
    } 

2、织入方法的配置:


[java] 
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 { 
    @Test 
    public 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:织入
作者:zhang6622056

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,