JavaEE---------Spring之AOP实现两种方式

首先说明一下,两种方式是Annotation与XML

 


先看Annotation实现办法

在Spring配置文件中设置自动扫描和自动代理如下

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.bq" />
    <!-- 自动代理 -->
    <aop:aspectj-autoproxy />

</beans>

 

被委托类(不太规范的命名)

UserDAO.java

package com.bq;

import org.springframework.stereotype.Component;

@Component("userDAO")
public class UserDAO {
    
    public void add(){
        System.out.println("添加用户成功");
    }
    
    public void update(){
        System.out.println("更新用户成功");
    }
}

 

委托类

MyInterceptor.java

package com.bq;

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 MyInterceptor {
    /*
     * 设置一个切入点,注意execution的语法, 它的意思是在com.bq包及子包当有任何返回类型的任何方法的任何参数执行时调用切面
     */
    @Pointcut("execution(public * com.bq..*.*(..))")
    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");
    }
}

 

测试类Index.java

package com.bq;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Index {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("beans.xml");
        UserDAO userDAO = (UserDAO) cpx.getBean("userDAO");
        userDAO.add();
        userDAO.update();
    }
}

 

下面来看一下XML实现方式,这个比较常用

 

修改配置文件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.bq"/>
    <!-- 定义委托类即拦截器 -->
     <bean id="logInterceptor" class="com.bq.MyInterceptor"></bean>
     <!-- AOP配置标签 -->
    <aop:config>
        <!-- 标记一个切面,指定ref即拦截器对象 -->
        <aop:aspect id="logAspect" ref="logInterceptor">
            <!-- 切面上的两个方法,各自扫描各自的,它们 -->
            <aop:before method="before" pointcut="execution(public * com.bq..*.add(..))" />
            <aop:around method="aroundMethod" pointcut="execution(public * com.bq..*.update(..))"/>
        </aop:aspect>
    </aop:config>
</beans>

 

自定义委托类,不需要再要注解了

package com.bq;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyInterceptor {

    public void before() {
        System.out.println("method before");
    }

    public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("method around start");
        pjp.proceed();
        System.out.println("method around end");
    }
}

 

UserDAO.java还是一样

 

package com.bq;

import org.springframework.stereotype.Component;

@Component("userDAO")
public class UserDAO {
    
    public void add(){
        System.out.println("添加用户成功");
    }
    
    public void update(){
        System.out.println("更新用户成功");
    }
}

 

测试类也一样

Index.java

package com.bq;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Index {
    
    public static void main(String [] args){
        ClassPathXmlApplicationContext cpx=new ClassPathXmlApplicationContext("beans.xml");
        UserDAO userDAO=(UserDAO) cpx.getBean("userDAO");
        userDAO.add();
        userDAO.update();
    }
}

慢慢再学习,Spring这个东西很好很强大

 

posted @ 2013-08-07 20:57  强子~Developer  阅读(346)  评论(0编辑  收藏  举报