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 ...
随机推荐
- LIRe 源代码分析 4:建立索引(DocumentBuilder)[以颜色布局为例]
===================================================== LIRe源代码分析系列文章列表: LIRe 源代码分析 1:整体结构 LIRe 源代码分析 ...
- Xcode自定义字体不能应用的原因
想给UILabel换一个自定义的字体,从字体册选择兰亭黑: 然后选择 在Finder中显示,找到字体文件为Lantinghei.ttc: 将其拷贝到项目中,在info.plist里添加字体支持key, ...
- 基于ARM-contexA9-蜂鸣器pwm驱动开发
上次,我们写过一个蜂鸣器叫的程序,但是那个程序仅仅只是驱动蜂鸣器,用电平1和0来驱动而已,跟驱动LED其实没什么两样.我们先来回顾一下蜂鸣器的硬件还有相关的寄存器吧: 还是和以前一样的步骤: 1.看电 ...
- FineReport性能调优的一些办法
FineReport性能调优的基本思路,就要对应用服务器的内存大小进行合理的设置. 一般服务器默认的内存配置都比较小,在较大型的应用项目中,这点内存是不够的,因此需要加工使其调大. 各应用服务器的内存 ...
- mybatis ---- 实现数据的增删改查
前面介绍了接口方式的编程,需要注意的是:在book.xml文件中,<mapper namespace="com.mybatis.dao.IBookDao"> ,命名空间 ...
- asp.net 调试与iis部署的问题
第一个问题:编译器错误信息: CS0016: 未能写入输出文件"c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET ...
- jquery 加法 乘法运算 精确计算函数
int类型相加不会出现问题,但小数点相加就会出现问题 //乘法函数 var accMul = function(arg1, arg2){ var m=0,s1=arg1.toString(),s2=a ...
- Python 可视化TVTK CubeSource管线初使用
CubeSource对象是长方体数据源对象.本次在安装成功TVTK库的基础上显示一个长方体对象.通过以下代码,我们设置一个长宽高分别为1.0,2.0,3.0的长方体数据源并通过管线显示出来. from ...
- 前端iFrame跨域问题
一.父域访问子域的元素 项目需求: iFrame是个聊天窗口,要求聊天窗口中点击图片图标,在父域将内容展示出来. 解决方法:(jQuery) 首先/要等iFrame加载完再执行函数!(代码如下) va ...
- % 与 format 进行字符串格式化
字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 1.百分号方式 %[(name)][flags][width].[precision]typecode (name) ...