SpringAop名词解释+基于xml的配置
1,AOP名词解释
2,AOP演示
(1)导包:
(2)准备目标对象
package com.songyan.service; import org.aspectj.lang.ProceedingJoinPoint; import sun.net.www.content.text.plain; /**
* 通知类
* @author sy
*
*/
public class MyAdvice {
//前置通知-->目标方法调用前调用
//后置通知(如果发生异常不在调用)-->目标方法调用后调用
//环绕通知-->目标方法调用前和后调用
//异常拦截通知-->出现异常调用
//后置通知(不论是否发生异常都会调用)-->目标方法调用后调用
/*-----------------------前置--------------------------*/
public void before()
{
System.out.println("before~~~~~~");
} /*-----------------------后置(如果发生异常不在调用)--------------------------*/
public void after()
{
System.out.println("after(如果发生异常不在调用)~~~~~~");
} /*-----------------------环绕--------------------------*/
public Object around(ProceedingJoinPoint joinPoint) throws Throwable
{
System.out.println("around(上)~~~~~~");
//调用目标代码
Object proceed=joinPoint.proceed();
System.out.println("around(下)~~~~~~");
return proceed;
} /*-----------------------异常拦截--------------------------*/
public void afterException()
{
System.out.println("afterException~~~~~~");
} /*-----------------------后置(不论是否发生异常都会调用)--------------------------*/
public void after_final()
{
System.out.println("after(不论是否发生异常都会调用)~~~~~~");
} }
(3)准备通知(事物管理的代码)
S1:导入AOP命名空间
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "> </beans>
S2:配置目标对象
<!--配置目标对象 -->
<bean name="userservice" class="com.songyan.service.UsrServiceImpl"></bean>
S3:配置通知对象
<!--配置通知对象 -->
<bean name="myadvice" class="com.songyan.service.MyAdvice"></bean>
S4:将通知织入目标对象
<!--将通知对象织入目标对象 -->
<aop:config>
<!--设置切入点 -->
<!--public void com.songyan.service.UsrServiceImpl.save()
void com.songyan.service.UsrServiceImpl.save()
* com.songyan.service.UsrServiceImpl.save()
* com.songyan.service.UsrServiceImpl.*()
* com.songyan.service.*ServiceImpl.*()
* com.songyan.service.*ServiceImpl.*(..)
* com.songyan.service..*ServiceImpl.*(..) -->
<aop:pointcut expression="* com.songyan.service..*ServiceImpl.*(..)"
id="pc" />
(4)配置进行织入(通知织入目标对象)
<!--给通知 设置切面 -->
<aop:aspect ref="myadvice">
<aop:before method="before" pointcut-ref="pc" />
<aop:after-returning method="after" pointcut-ref="pc" />
<aop:around method="around" pointcut-ref="pc" />
<aop:after-throwing method="afterException" pointcut-ref="pc" />
<aop:after method="after_final" pointcut-ref="pc" />
</aop:aspect>
</aop:config>
</beans>
完整配置信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<!--配置目标对象 -->
<bean name="userservice" class="com.songyan.service.UsrServiceImpl"></bean>
<!--配置通知对象 -->
<bean name="myadvice" class="com.songyan.service.MyAdvice"></bean>
<!--将通知对象织入目标对象 -->
<aop:config>
<!--设置切入点 -->
<!--public void com.songyan.service.UsrServiceImpl.save() void com.songyan.service.UsrServiceImpl.save()
* com.songyan.service.UsrServiceImpl.save() * com.songyan.service.UsrServiceImpl.*()
* com.songyan.service.*ServiceImpl.*() * com.songyan.service.*ServiceImpl.*(..)
* com.songyan.service..*ServiceImpl.*(..) -->
<aop:pointcut expression="execution(* com.songyan.service.*ServiceImpl.*(..))" id="pc" />
<!--给通知 设置切面 -->
<aop:aspect ref="myadvice">
<aop:before method="before" pointcut-ref="pc" />
<aop:after-returning method="after" pointcut-ref="pc" />
<aop:around method="around" pointcut-ref="pc" />
<aop:after-throwing method="afterException" pointcut-ref="pc" />
<aop:after method="after_final" pointcut-ref="pc" />
</aop:aspect>
</aop:config>
</beans>
(4)测试
package com.songyan.service; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Demo {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/service/beans1.xml");
UserService userService=(UserService)applicationContext.getBean("userservice");
userService.delete();
}
}
将delete里面的代码修改:
public void before()
{
System.out.println("before~~~~~~");
}
这样在运行的时候就会发生异常,
after-returning 定义的方法就不会运行
after-throwing 定义的方法就会运行
after 不管什么情况都会运行
SpringAop名词解释+基于xml的配置的更多相关文章
- Spring学习--基于 XML 的配置声明切面
正常情况下 , 基于注解的生命要优先于基于 XML 的声明. 通过 AspectJ 注解 , 切面可以与 AspectJ 兼容 , 而基于 XML 的配置则是 Spring 专有的.由于 Aspect ...
- (spring-第2回【IoC基础篇】)Spring的Schema,基于XML的配置
要深入了解Spring机制,首先需要知道Spring是怎样在IoC容器中装配Bean的.而了解这一点的前提是,要搞清楚Spring基于Schema的Xml配置方案. 在深入了解之前,必须要先明白几个标 ...
- Spring框架入门之基于xml文件配置bean详解
关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...
- 解释基于XML Schema方式的切面实现?
在这种情况下,切面由常规类以及基于XML的配置实现.
- 解释基于 XML Schema 方式的切面实现?
在这种情况下,切面由常规类以及基于 XML 的配置实现.
- MyBatis框架基于XML的配置
什么是MyBatis? 答:它是一个持久层框架 说的太简单了吗?那让我们来看一下官方的文档描述: MyBatis有什么作用呢? 1.持久层的零实现 2.可以自动将数据封装到对象里面不需要手工编写映射的 ...
- spring-AOP框架(基于AspectJ注解配置AOP)
基于AspectJ注解配置AOP 1.加入jar包: 要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectJ类库:aopalliance.jar.aspectj.w ...
- Spring学习记录(十三)---基于xml文件配置AOP
上一篇讲了用注解配置AOP,现在讲用xml怎么配置AOP 其实逻辑是一样的,只是用xml的方法,要把这种逻辑写出来,告诉spring框架去执行. 例子:这里的例子和上一篇的例子一样.换成xml方式 / ...
- idea的spring整合基于xml文件配置的mybatis报Invalid bound statement (not found): com.music.dao.MusicDao.findAll的问题
一. 题主当时就是自己尝试整合spring和mybatis的时候遇到了这个问题,当时题主只看到了用注解的方式配置的dao层,题主用的是xml文件配置的形式, 而且坑爹的是题主的两个文件的路径写的也不一 ...
随机推荐
- ASP.NET Core 2.1 源码学习之 Options[2]:IOptions 【转】
原文链接:https://www.cnblogs.com/RainingNight/p/strongly-typed-options-ioptions-in-asp-net-core.html 在 上 ...
- 设计模式之模板方法模式 templateMethod
代码实现 public abstract class BankTemplateMethod { //具体方法 public void takeNumber(){ System.out.println( ...
- ThreadLocal 学习
JDK 1.2版本就已经提供了java.lang.ThreadLocal.其为多线程程序的并发问题提供了一种新的思路.使用该工具类可以简洁地编写出优美的多线程程序. 当使用ThreadLocal维护变 ...
- kibana的查询语法
kibana的查询语法是 字段Fields:关键词
- Linux下nginx支持.htaccess文件实现伪静态的方法!
在Google上搜索的资料很多人都说nginx目前不支持.htaccess文件,我按照nginx的规则试验了一下,结果发现nginx是完全支持.htaccess文件的! 方法如下: 1. 在需要使用. ...
- hdu 1195 Open the Lock (BFS)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 2017 多校2 hdu 6053 TrickGCD
2017 多校2 hdu 6053 TrickGCD 题目: You are given an array \(A\) , and Zhu wants to know there are how ma ...
- transition transform animate的使用
注:代码显示效果可以自行粘贴复制查看 transition(过渡),主要是关注property的变化主要有四个属性transition-property.transition-durantion.tr ...
- php实现二维码
封装函数 function verifyImage($len=3){ //session_start(); $scr="abcdefghijklmnoqprstuvwxyzABCDEFJHI ...
- 各版本Sql Server下载地址全
SQL Server 2014简体中文企业版 文件名:cn_sql_server_2014_enterprise_edition 32位下载地址:ed2k://|file|cn_sql_server_ ...