新手學習筆記Spring AOP全自動編程

業務類spring

package oyb.service;

public interface IUserService {
    public void add();
    public void delete();
}
package oyb.service;

public class UserServiceImpl implements IUserService {
    @Override
    public void add() {
        System.out.println("添加用戶。。。。");
    }

    @Override
    public void delete() {
        System.out.println("刪除用戶");
    }
}

切面類express

package oyb.aspect;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class MyAspect implements MethodInterceptor{

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        return methodInvocation.proceed();
    }
}

Spring配置文件框架

<?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:p="http://www.springframework.org/schema/p"
       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.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--配置UserService-->
    <bean id="userService" class="oyb.service.UserServiceImpl"></bean>

    <!--默認狀況下Spring的AOP生成的代理是JDK的Proxy實現的-->
    <!--配置切面對象-->
    <bean id="myAspect" class="oyb.aspect.MyAspect"></bean>

    <!--proxy-target-class爲true表示開啓cglib加強-->
    <aop:config proxy-target-class="true">

        <aop:pointcut id="myPointcut" expression="execution(* oyb.service.*.*(..))"></aop:pointcut>
        <!--通知 關聯 切入點-->
        <aop:advisor advice-ref="myAspect" pointcut-ref="myPointcut"></aop:advisor>
    </aop:config>

</beans>
package oyb.test;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import oyb.service.IUserService;

public class test {

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        IUserService userService = (IUserService) context.getBean("userService");
        userService.add();

    }
}

測試結果:ide

以前一直拋出Bean named 'myAspect' must be of type [org.aopalliance.aop.Advice],這個異常,檢查以後發現切面類MyAspect中沒有實現MethodInterceptor接口,因此會報這個錯誤。測試

這個簡單的案例當中,導入了com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar這個包spa

這裏簡單介紹一下AspectJ,AspectJ是一個基於Java語言的AOP框架,在Spring 2.0之後新增了對AspectJ切點表達式支持,在新版本的Spring當中,大部分都使用AspectJ的方式來進行開發。代理

 下面介紹一下AspectJ的幾種通知類型code

1.before:前置通知,主要應用到各類校驗。在方法執行前執行,若是通知拋出異常,方法將進行不了。component

2.afterReturning:後置通知,主要應用於常規數據處理。方法執行若是拋出異常,則也執行不了。在方法執行後執行,能夠得到方法的返回值。xml

3.around:環繞通知,

4.afterThrowing:拋出異常通知,方法拋出異常時執行,不然沒法執行

5.after:最終通知,方法執行完畢以後執行,不管是否拋出異常

例子:

1.導入相應的包(基於xml配置):

2.業務類

package oyb.service;

public interface IUserService {
    public void add();
    public void delete();
    public void update();
}
package oyb.service.impl;

import oyb.service.IUserService;

public class UserServiceImpl implements IUserService {
    @Override
    public void add() {
        System.out.println("添加用戶。。。");
    }

    @Override
    public void delete() {
        System.out.println("刪除用戶。。。");
    }

    @Override
    public void update() {
        System.out.println("更新用戶。。。");
    }
}

3.切面類

package oyb.aspect;

import org.aspectj.lang.JoinPoint;

public class MyAspect {

    public void myBefore(JoinPoint joinPoint){
        System.out.println("前置通知");
    }
    public void myReturning(JoinPoint joinPoint){
        System.out.println("後置通知");
    }
}

配置文件

<?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:p="http://www.springframework.org/schema/p"
       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.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="userService" class="oyb.service.impl.UserServiceImpl"></bean>

    <bean id="myAspect" class="oyb.aspect.MyAspect"></bean>

    <aop:config>
        <aop:aspect ref="myAspect">
            <aop:pointcut id="myPointcut" expression="execution(* oyb.service.impl.UserServiceImpl.*(..))"/>
            <!--前置通知-->
            <aop:before method="myBefore" pointcut-ref="myPointcut"></aop:before>
            <!--後置通知
           
            -->
            <aop:after-returning method="myReturning" pointcut-ref="myPointcut" ></aop:after-returning>
        </aop:aspect>
    </aop:config>

</beans>

測試類

package oyb.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import oyb.service.IUserService;

public class test {

    @Test
    public  void test(){

        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        IUserService userService = (IUserService) context.getBean("userService");
        userService.add();
    }
}

測試結果

 使用註解

Spring配置文件中

<?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:p="http://www.springframework.org/schema/p"
       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.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="oyb"></context:component-scan>

    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>



</beans>

業務類中添加註解如圖

切面類

package oyb.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {

    //聲明公共切入點
    @Pointcut("execution(* oyb.service.impl.UserServiceImpl.*(..))")
    public void myPointcut(){

    }
    @Before(value ="myPointcut()")
    public void myBefore(JoinPoint joinPoint){
        System.out.println("前置通知");
    }

    @AfterReturning(value = "myPointcut()")
    public void myReturning(JoinPoint joinPoint){

        System.out.println("後置通知");
    }
}

測試類

package oyb.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import oyb.service.IUserService;

public class test {

    @Test
    public  void test(){

        ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
        IUserService userService = (IUserService) context.getBean("userService");
        userService.add();
    }
}

測試結果如圖:

相關文章
相關標籤/搜索