<?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:jdbc="http://www.springframework.org/schema/jdbc"
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.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置自动扫描 -->
<context:component-scan base-package="cn.zr.aoptest"/> <!--注解方式 配置aop自动代理 -->
<aop:aspectj-autoproxy/> <!-- xml方式 配置aop -->
<bean id="personDaoImpl" class="cn.zr.aoptest.dao.person.impl.PersonDaoImpl"></bean>
<bean id="personManager" class="cn.zr.aop.utils.PersonManager"></bean>
<aop:config>
<aop:pointcut
expression="execution(* cn.zr.aoptest.dao.person.impl.*.*(..))"
id="pointcut" />
<aop:aspect id="xmlaop" ref="personManager">
<aop:before method="beforeInfo" pointcut-ref="pointcut" />
<aop:after-returning method="afterReturnInfo" pointcut-ref="pointcut"/>
<aop:after method="afterInfo" pointcut-ref="pointcut"/>
<aop:after-throwing method="exceptionInfo" throwing="throwable" pointcut-ref="pointcut"/>
<aop:around method="aroundInfo" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config> </beans>
package cn.zr.aop.utils;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class PersonManager { public void beforeInfo(JoinPoint joinPoint){
System.out.println("获取信息...前");
//获取参数
Object[] objects = joinPoint.getArgs();
for (Object object : objects) {
System.out.println(object);
}
} public void afterReturnInfo(){
System.out.println("获取信息...后"); } public void afterInfo(){
System.out.println("获取信息...最终"); } public void exceptionInfo(Throwable throwable){
System.out.println("出现异常:"+throwable); } public Object aroundInfo(ProceedingJoinPoint pjp){ Object obj = null;
System.out.println("=== 环绕前 ===");
try {
obj = pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("=== 环绕后 ===");
return obj;
}
}
package cn.zr.aoptest.dao.person.impl;

public class PersonDaoImpl {

    public void getInfo() {
int num = 10/0;
System.out.println("获取信息");
} }
package cn.zr.aoptest.dao.person.impl;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Component
@Aspect
public class TransactionManager {
// 设置切点
@Pointcut("execution(* cn.zr.aoptest.Userdao.impl.UserDaoImpl.*(..))")
public void methodPointcut() {
} // 前置通知
@Before(value=("execution(* cn.zr.aoptest.Userdao.impl.UserDaoImpl.*(..))"))
public void beginTransactionManager(JoinPoint jp){
System.out.println("...开始事务...");
Object[] objs = jp.getArgs();
for (Object object : objs) {
System.out.println(object+"!!!");
}
} // 后置通知
@AfterReturning("methodPointcut()")
public void commitTrasactionManager(){
System.out.println("===提交事务===");
} // 最终通知(finally)
@After("methodPointcut()")
public void finallyManager(){ System.out.println("~~~无论是否异常,始终执行~~~");
} // 异常通知
//@AfterThrowing(pointcut = "controllerAspect()", throwing="e")
@AfterThrowing(value="methodPointcut()",throwing="ep")
public static void exceptionManager(Throwable ep){
System.out.println("<<<事务回滚>>>,exception:"+ep);
} // 环绕通知
@Around("methodPointcut()")
public Object aroundTrasactionManager(ProceedingJoinPoint joinPoint) {
Object obj = null;
System.out.println("---=围绕通知前=---");
try {
obj = joinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("***=围绕通知后=***");
return obj;
} }
package cn.zr.aoptest.Userdao.impl;

import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl { /**
* 添加用户操作
*/
public void addUser(String name){ System.out.println("我要进行添加用户操作"+name);
int count = 0;
int num = 100/count;
} }
package cn.zr.aoptest.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.zr.aoptest.Userdao.impl.UserDaoImpl;
import cn.zr.aoptest.dao.person.impl.PersonDaoImpl; public class BeanTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDaoImpl impl = (UserDaoImpl) ac.getBean(UserDaoImpl.class);
impl.addUser("lf"); PersonDaoImpl personDaoImpl = (PersonDaoImpl) ac.getBean("personDaoImpl");
personDaoImpl.getInfo(); } }

Java AOP 注解配置与xml配置的更多相关文章

  1. 死磕Spring之AOP篇 - Spring AOP注解驱动与XML配置

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  2. spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)

    1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...

  3. IDEA用maven创建springMVC项目和配置(XML配置和Java配置)

    1.DEA创建项目 新建一个maven project,并且选择webapp原型. 然后点击next 这里的GroupId和ArtifactID随意填写,但是ArtifactID最好和你的项目一名一样 ...

  4. Spring的注解配置与XML配置之间的比较

    注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作. 如:使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO ...

  5. Spring注解配置和xml配置优缺点比较

    Spring注解配置和xml配置优缺点比较 编辑 ​ 在昨天发布的文章<spring boot基于注解方式配置datasource>一文中凯哥简单的对xml配置和注解配置进行了比较.然后朋 ...

  6. Hibernate实现有两种配置,xml配置与注释配置

    hibernate实现有两种配置,xml配置与注释配置. (1):xml配置:hibernate.cfg.xml (放到src目录下)和实体配置类:xxx.hbm.xml(与实体为同一目录中) < ...

  7. hibernate实现有两种配置,xml配置与注释配置。<转>

    <注意:在配置时hibernate的下载的版本一定确保正确,因为不同版本导入的jar包可能不一样,所以会导致出现一些错误> hibernate实现有两种配置,xml配置与注释配置. (1) ...

  8. spring aop注解方式与xml方式配置

    注解方式 applicationContext.xml 加入下面配置 <!--Spring Aop 启用自动代理注解 --> <aop:aspectj-autoproxy proxy ...

  9. Spring 中的事务操作、注解、以及 XML 配置

    事务 事务全称叫数据库事务,是数据库并发控制时的基本单位,它是一个操作集合,这些操作要么不执行,要么都执行,不可分割.例如我们的转账这个业务,就需要进行数据库事务的处理. 转账中至少会涉及到两条 SQ ...

  10. Spring MVC 中使用AOP 进行事务管理--XML配置实现

    1.今天写一篇使用AOP进行事务管理的示例,关于事务首先需要了解以下几点 (1)事务的特性 原子性(Atomicity):事务是一个原子操作,由一系列动作组成.事务的原子性确保动作要么全部完成,要么完 ...

随机推荐

  1. python学习之基本类型

    #我的第一个python程序 print("hello world"); #多行字符串 print("""\ Usage: thingy [OPTIO ...

  2. HDU - 5088: Revenge of Nim II (问是否存在子集的异或为0)

    Nim is a mathematical game of strategy in which two players take turns removing objects from distinc ...

  3. ArcGIS Runtime SDK for WPF之测量距离和面积

    bu不多说,上代码 using System.Windows; using ESRI.ArcGIS.Client; using ESRI.ArcGIS.Client.Tasks; using ESRI ...

  4. verilog数组定义及其初始化

    这里的内存模型指的是内存的行为模型.Verilog中提供了两维数组来帮助我们建立内存的行为模型.具体来说,就是可以将内存宣称为一个reg类型的数组,这个数组中的任何一个单元都可以通过一个下标去访问.这 ...

  5. 【JVM】JVM参数说明和分析

    不管是YGC还是Full GC,GC过程中都会对导致程序运行中中断,正确的选择不同的GC策略, 调整JVM.GC的参数,可以极大的减少由于GC工作,而导致的程序运行中断方面的问题,进而适当的提高Jav ...

  6. vue指令总结

    new  Vue({          el: "#box",            // element(元素) 当前作用域          data:{ msg:" ...

  7. fn project 试用之后的几个问题的解答

    今天试用fnproject  之后自己有些思考,后面继续解决   1. 目前测试是强依赖 dockerhub 的,实际可能不是很方便 2. 如何与k8s .mesos.docker swarm  集成 ...

  8. Oracle备份与恢复案例

    注:以下内容来自<Oracle备份与恢复案例.doc>文档. Oracle备份与恢复案例 By Piner 一. 理解什么是数据库恢复 当我们使用一个数据库时,总希望数据库的内容是可靠的. ...

  9. idea的有些路径问题

    resource文件在xml的路径 http://www.cnblogs.com/zqr99/p/7642712.html resource文件在io流的读取 this.getClass().getR ...

  10. ubuntu下网卡eth1如何修改为eth0

    正常来说,Linux在识别网卡时第一张会是eth0,第二张才是eth1.有时候我们使用虚拟机克隆技术后网卡的信息就会改变,新克隆出来的虚拟主机网卡名字可能变为eth1.无论我们怎么修改都无法改变,这就 ...