SSM-Spring-18:Spring中aspectJ的XML版

 

 

 

------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

 

 

 

aspectJ的xml版是开发中最常用的:

下面直接已案例入手,毕竟繁琐的日子不多了

 

案例:两个接口,俩个实现类,一个实现增强的普通类

  ISomeService接口:

 

package cn.dawn.day20aspectjxml;

/**
 * Created by Dawn on 2018/3/8.
 */
public interface ISomeService {
    public void insert();
    public void delete();
    public void select();
    public void update();
}

 

  SomeServiceImpl类,上方类的实现类:

 

package cn.dawn.day20aspectjxml;


/**
 * Created by Dawn on 2018/3/8.
 */
public class SomeServiceImpl implements ISomeService {

    public void insert() {
        System.out.println("insert ok");
    }

    public void delete() {
        System.out.println("delete ok");

    }

    public void select() {
        System.out.println("select ok");

    }

    public void update() {
        System.out.println("update ok");
    }
}

 

  IBookService接口

 

package cn.dawn.day20aspectjxml;

/**
 * Created by Dawn on 2018/3/12.
 */
public interface IBookService {
    public void selectAllBooks();
}

 

  BookServiceImpl类,上面那个接口的实现类

 

package cn.dawn.day20aspectjxml;

/**
 * Created by Dawn on 2018/3/12.
 */
public class BookServiceImpl implements IBookService {
    public void selectAllBooks() {
        System.out.println("selectbooks ok");

        int a=5/0;
        System.out.println(a);
    }
}

 

  实现增强的普通类:

 

package cn.dawn.day20aspectjxml;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * Created by Dawn on 2018/3/12.
 */
public class MyAspectJ {
    /*最后增强,无论是否出现异常都会执行*/
    public void myAfter(){
        System.out.println("最终增强");
    }
    /*后置增强*/
    public void myAfterReturning(){
        System.out.println("后置增强");
    }
    /*前置增强*/
    public void myBefore(){
        System.out.println("前置增强");
    }
    /*前置增强,这种写法可以一会调用出切点表达式,在console打印出来:当然xml文件中另外需要配置一道*/
    public void myBefore1(JoinPoint jp){
        System.out.println("前置通知方法jp = " + jp);
    }
    /*异常增强*/
    public void myAfterThrowing(){
        System.out.println("异常增强");
    }
    /*环绕增强*/
    public void myAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕增强前");
        pjp.proceed();
        System.out.println("环绕增强后");
    }

}

 

  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:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--要增强的对象-->
    <bean id="service" class="cn.dawn.day20aspectjxml.SomeServiceImpl"></bean>
    <bean id="bookservice" class="cn.dawn.day20aspectjxml.BookServiceImpl"></bean>
    <!--增强的内容-->
    <bean id="MyAspectJ" class="cn.dawn.day20aspectjxml.MyAspectJ"></bean>
    <!--aspectjxml的自动代理-->
    <aop:config>
        <aop:pointcut id="mypointcut1" expression="execution(* *..day20aspectjxml.*.select(..))"></aop:pointcut>
        <aop:pointcut id="mypointcut2" expression="execution(* *..day20aspectjxml.*.update(..))"></aop:pointcut>
        <aop:pointcut id="mypointcut3" expression="execution(* *..day20aspectjxml.*.select*(..))"></aop:pointcut>
        <aop:pointcut id="mypointcut4" expression="execution(* *..day20aspectjxml.*.delete(..))"></aop:pointcut>
        <aop:aspect ref="MyAspectJ">
            <aop:before method="myBefore" pointcut-ref="mypointcut2"></aop:before>
            <aop:before method="myBefore1(org.aspectj.lang.JoinPoint)" pointcut-ref="mypointcut2"></aop:before>
            <aop:after method="myAfter" pointcut-ref="mypointcut1"></aop:after>
            <aop:around method="myAround(org.aspectj.lang.ProceedingJoinPoint)" pointcut-ref="mypointcut4"></aop:around>
            <aop:after-throwing method="myAfterThrowing" pointcut-ref="mypointcut3"></aop:after-throwing>
            <aop:after-returning method="myAfterReturning" pointcut-ref="mypointcut1"></aop:after-returning>
        </aop:aspect>
    </aop:config>

</beans>

 

  这儿的method方法中加了(参数)会报红,不需理会,一会执行没有错误就行

  写法的格式就是这样

 

  单测:

 

package cn.dawn.day20aspectjxml;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Dawn on 2018/3/3.
 */
public class test20180312 {
    @Test
    /*aop代理工厂bean异常增强*/
    public void t01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day20aspectjxml.xml");
        ISomeService service = (ISomeService) context.getBean("service");
        IBookService bookservice = (IBookService) context.getBean("bookservice");

        try {
            bookservice.selectAllBooks();
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("================观察==================");
        service.update();
        System.out.println("================观察==================");
        service.delete();
        System.out.println("================观察==================");
        service.select();
        System.out.println("================观察==================");
    }
}

 

 

  繁琐的东西结束了

 

  我记得我的老师说过一句话,java核心,java能不死,能辉煌这么多年的原因就是Spring,学会Spring可以赚3000,如果你搞java不会Spring,你连3000都赚不到

  其中IOC(控制反转)值1000,AOP(面向切面编程)值2000,至此,3000块的东西讲的差不多了,aop也结束了,

  下面我还会继续更新博客,Spring的事物,JDBCTemplate,以及整合MyBatis,然后此Spring部分也就差不多完结了,以后有时间再补充更多关于Spring的知识点

 

posted @ 2018-03-12 19:00  晨曦Dawn  阅读(146)  评论(0编辑  收藏  举报