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

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=/;
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的知识点

SSM-Spring-18:Spring中aspectJ的XML版的更多相关文章

  1. SSH(Spring Struts2 Hibernate)框架整合(xml版)

    案例描述:使用SSH整合框架实现部门的添加功能 工程: Maven 数据库:Oracle 案例架构: 1.依赖jar包pom.xml <project xmlns="http://ma ...

  2. Spring(2)——Spring IoC 详解

    Spring IoC 概述 IoC:Inverse of Control(控制反转) 读作"反转控制",更好理解,不是什么技术,而是一种设计思想,就是将原本在程序中手动创建对象的控 ...

  3. 源码跟读,Spring是如何解析和加载xml中配置的beans

    Spring版本基于: 跟踪代码源码基于: https://github.com/deng-cc/KeepLearning commit id:c009ce47bd19e1faf9e07f12086c ...

  4. spring---aop(10)---Spring AOP中AspectJ

    写在前面 在之前的文章中有写到,Spring在配置中,会存在大量的切面配置.然而在很多情况下,SpringAOP 所提供的切面类真的不是很够用,比如想拦截制定的注解方法,我们就必须扩展DefalutP ...

  5. spring AOP (使用AspectJ的xml方式 的aop实现) (7)

    目录 一.定义计算器接口跟实现类 二.定义两个切面,日志切面和验证切面 三.在xml中配置切面 四.测试类 一.定义计算器接口跟实现类 public interface ArithmeticCalcu ...

  6. spring的AspectJ基于XML和注解(前置、后置、环绕、抛出异常、最终通知)

    1.概念 (1)AspectJ是一个基于Java语言的AOP框架 (2)Spring2.0以后新增了对AspectJ切入点表达式的支持 (3)AspectJ是AspectJ1.5的新增功能,通过JDK ...

  7. Spring中加载xml配置文件的六种方式

    Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog  因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...

  8. Spring Boot中如何扩展XML请求和响应的支持

    在之前的所有Spring Boot教程中,我们都只提到和用到了针对HTML和JSON格式的请求与响应处理.那么对于XML格式的请求要如何快速的在Controller中包装成对象,以及如何以XML的格式 ...

  9. 【AOP】操作相关术语---【Spring】的【AOP】操作(基于aspectj的xml方式)

    [AOP]操作相关术语 Joinpoint(连接点):类里面哪些方法可以被增强,这些方法称为连接点. Pointcut(切入点):在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面add ...

随机推荐

  1. TinySpring分析二

    step5 看完了前面的几步,到现在我们必然要想到的问题就是,数据要是放在xml中怎么读? 其实按照正常思维一步一步来,从xml中读数据和之前手工配进去并没有什么大的区别,只要读出来就OK了. 先看测 ...

  2. 如何在Android上编写高效的Java代码

    转自:http://www.ituring.com.cn/article/177180 作者/ Erik Hellman Factor10咨询公司资深移动开发顾问,曾任索尼公司Android团队首席架 ...

  3. iOS和OS X中的bundle

    bundle也可以称之为包(package). 它在iOS和OS X中实际为一个文件夹但却当成单独的文件来对待. 每一个app都有一个bundle,并且你可以通过在xxx.app图标上右击鼠标然后选择 ...

  4. android最火的开源项目

    原文地址:http://www.csdn.net/article/2013-05-21/2815370-Android-open-source-projects-finale 此前,CSDN移动频道推 ...

  5. android EventBus详解(二)

    上一节讲了EventBus的使用方法和实现的原理,下面说一下EventBus的Poster只对粘滞事件和invokeSubscriber()方法是怎么发送的. Subscribe流程 我们继续来看Ev ...

  6. mac OS X 10.10更新gcc 4.9.1后默认无法编译连接的问题

    MAC OS X10.10升级前使用的低版本的gcc(好像是4.7.x),正常编译可以完成,不过会出现警告: couldn't understand kern.osversion `14.0.0' 网 ...

  7. JS基础:闭包和作用域链

    简介 一个定义在函数内部的函数与包含它的外部函数构成了闭包,内部函数可以访问外部函数的变量,这些变量将一直保存在内存中,直到无法再引用这个内部函数. 例如: var a = 0; function o ...

  8. 升级Centos 7/6内核版本到4.12.4的方法

    一.查看那系统内核版本 二.升级内核 三.修改grub中默认的内核版本 四.重启系统并查看系统内核 公司打算上Docker服务,目前需要安装运行环境,Docker新的功能除了需要Centos 7系统之 ...

  9. C#WebService 出现No 'Access-Control-Allow-Origin' header is present on the requested resource

    C#WebService 出现No 'Access-Control-Allow-Origin' header is present on the requested resource 解决办法: 在c ...

  10. 前端打包工具——build release介绍

    前言 对于前端开发者来说,资源打包是日常过程中一个必不可少的过程:目前我们大多数时候使用grunt.gulp.webpack这三个工具来完成这个工作:但是有一个特点就是我们没创建一个项目都要对应的去编 ...