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

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. Cocos2D中的ObjectAL简介

    Cocos2D包含ObjectAL音频库,可以回放音效和音乐. ObjectAL是一个建立在低级别OpenAL API上的库.OpenAL最擅长被用来播放短的音效(.wav,.caf,.aiff),并 ...

  2. 【Visual C++】游戏编程学习笔记之五:单一背景滚动

    本系列文章由@二货梦想家张程 所写,转载请注明出处. 本文章链接:http://blog.csdn.net/terence1212/article/details/44224963 作者:ZeeCod ...

  3. Git与远程reposiory的相关命令

    问题1:Git如何同步远程repository的分支(branch) 某天,小C同学问我,为啥VV.git仓库里面本来已经删除了branchA这个分支,但是我的mirror中还是有这个分支呢? 分析: ...

  4. 实战:通过ViewModel规范TableView界面开发

    TableView界面可以说是移动App中最常用的界面之一了,物品/消息列表.详情编辑.属性设置--几乎每个app都可以看到它的身影.如何优美地实现一个TableView界面,就成了iOS开发者的必备 ...

  5. 使用schemaExport自动生成表结构

    一.Hibernate原生状态 ? 1 2 3 4 5 Configuration cfg = new Configuration().configure();   SchemaExport expo ...

  6. window7如何配置修改环境变量

    http://jingyan.baidu.com/article/b24f6c82cba6dc86bfe5da9f.html

  7. angular4 ionic3 app

    对于angular系列来说,从2到4仅仅是版本号的变更,绝大部分都是兼容的.  如果按照规范编写代码,一般来说是没有问题的. 学习angular4     快速入门参考  https://www.an ...

  8. minimun depth of binary tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  9. java 如何使的float保留2位或者多位小数 (转载)

    转载自 http://blog.csdn.net/com_stu_zhang/article/details/7214565 方法1: float   f   =  34.232323;    Big ...

  10. 框架学习:ibatis框架的结构和分析

    由于最近一段时间比较忙碌,<框架学习>系列的文章一直在搁浅着,最近开始继续这个系列的文章更新. 在上篇文章中我们说到了hibernate框架,它是一种基于JDBC的主流持久化框架,是一个优 ...