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=/;
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版的更多相关文章
- SSH(Spring Struts2 Hibernate)框架整合(xml版)
案例描述:使用SSH整合框架实现部门的添加功能 工程: Maven 数据库:Oracle 案例架构: 1.依赖jar包pom.xml <project xmlns="http://ma ...
- Spring(2)——Spring IoC 详解
Spring IoC 概述 IoC:Inverse of Control(控制反转) 读作"反转控制",更好理解,不是什么技术,而是一种设计思想,就是将原本在程序中手动创建对象的控 ...
- 源码跟读,Spring是如何解析和加载xml中配置的beans
Spring版本基于: 跟踪代码源码基于: https://github.com/deng-cc/KeepLearning commit id:c009ce47bd19e1faf9e07f12086c ...
- spring---aop(10)---Spring AOP中AspectJ
写在前面 在之前的文章中有写到,Spring在配置中,会存在大量的切面配置.然而在很多情况下,SpringAOP 所提供的切面类真的不是很够用,比如想拦截制定的注解方法,我们就必须扩展DefalutP ...
- spring AOP (使用AspectJ的xml方式 的aop实现) (7)
目录 一.定义计算器接口跟实现类 二.定义两个切面,日志切面和验证切面 三.在xml中配置切面 四.测试类 一.定义计算器接口跟实现类 public interface ArithmeticCalcu ...
- spring的AspectJ基于XML和注解(前置、后置、环绕、抛出异常、最终通知)
1.概念 (1)AspectJ是一个基于Java语言的AOP框架 (2)Spring2.0以后新增了对AspectJ切入点表达式的支持 (3)AspectJ是AspectJ1.5的新增功能,通过JDK ...
- Spring中加载xml配置文件的六种方式
Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog 因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...
- Spring Boot中如何扩展XML请求和响应的支持
在之前的所有Spring Boot教程中,我们都只提到和用到了针对HTML和JSON格式的请求与响应处理.那么对于XML格式的请求要如何快速的在Controller中包装成对象,以及如何以XML的格式 ...
- 【AOP】操作相关术语---【Spring】的【AOP】操作(基于aspectj的xml方式)
[AOP]操作相关术语 Joinpoint(连接点):类里面哪些方法可以被增强,这些方法称为连接点. Pointcut(切入点):在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面add ...
随机推荐
- Erlang Rebar 使用指南之三:Rebar和OTP程序约定和命令
Erlang Rebar 使用指南之三:Rebar和OTP程序约定和命令 全文目录: https://github.com/rebar/rebar/wiki 本章位置: https://github. ...
- 第十一章 图像之2D(2)
Android游戏开发群:290051794 Libgdx游戏开发框架交流群:261954621 作者:宋志辉 出处:http://blog.csdn.net/song19891121 本文版权归作 ...
- 带三方登录(qq,微信,微博)
实现QQ.微信.新浪微博和百度第三方登录(Android Studio) 前言: 对于大多数的APP都有第三方登录这个功能,自己也做过几次,最近又有一个新项目用到了第三方登录,所以特意总结了一下关于 ...
- c语言部分库函数,代码实现,以及细节理解
代码来自: http://blog.csdn.net/v_JULY_v //得9 分 //为了实现链式操作,将目的地址返回,加2 分! char * strcpy( char *strDest, co ...
- SQLSERVER 性能优化之Perfmon指标
Perfmon是Windows系统性能监视程序.用于监视CPU使用率.内存使用率.硬盘读写速度.网络速度等. Processor/%Privileged Time阀值:如果数值持续大于75%就表示存在 ...
- Cookie、sessionStorage、localStorage的区别
共同点:都是保存在浏览器端,且同源的.区别:cookie数据始终在同源的http请求中携带(即使不需要),即cookie在浏览器和服务器间来回传递.而sessionStorage和localStora ...
- IOS使用FMDB封装的数据库增删改查操作
// // DBHelper.h // LessonStoryBoard // // Created by 袁冬冬 on 15/10/29. // Copyright (c) 2015年 袁冬 ...
- JS(作用域和闭包)
1.对变量提升的理解 1.变量定义(上下文) 2.函数声明 2.说明 this 几种不同的使用场景 常见用法 1.作为构造函数执行 2.作为对象属性执行 3.作为普通函数执行(this === win ...
- valid palindrome(回文)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 读JVM相关的一些笔记
1.JVM的运行模式 vm一般有两种运行模式,client和server(JDK 7 后有第三种 Tiered server,后续会涉及到). client : 启动快,内存占用少,JIT编译器生成代 ...