<?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. bzoj 3195 奇怪的道路

    Written with StackEdit. Description 小宇从历史书上了解到一个古老的文明.这个文明在各个方面高度发达,交通方面也不例外.考古学家已经知道,这个文明在全盛时期有\(n\ ...

  2. TypeError: parse() got an unexpected keyword argument 'transport_encoding' 安装tensor后报错

    TypeError: parse() got an unexpected keyword argument 'transport_encoding' 巨蛋疼,出这个问题后,老夫真是醉了,mmp,最后在 ...

  3. GlassFish的安装与使用(Windows)

    前言 Glassfish是一款由Sun公司开发的(现由甲骨文公司赞助)开源的免费的应用服务器,它既是EJB容器也是WEB容器.Glassfish支持最新版的Java EE标准. Glassfish与T ...

  4. 剑指offer-第六章面试中的各项能力(数字在排序数组中出现的次数)

    题目:统计一个数字在排序数组中出现的次数. 思路:采用二分查找,找到该数字在数组中第一次出现的位置,然后再找到组后一个出现的位置.两者做减法运算再加1.时间复杂度为O(logn) Java代码: // ...

  5. 【sqlite】基础知识

    最近做一个数控系统的项目,winCE嵌入式操作系统+.Net Compact Framework环境+VS2008开发平台,开发的设备程序部署到winCE系统下的设备中运行.. 个年头,SQLite也 ...

  6. selenium - xpath - 定位

    前言: XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. 看这里介绍:w3school 首先来看一下xpath常用的语法: 一.xpath ...

  7. expected_conditions 库的使用方法

    from selenium.webdriver.support import expected_conditions as EC 例子一: 例子二:(判断元素存在文本"糯米")

  8. 【转】Jmeter在命令行运行技巧

    For non-interactive testing, you may choose to run JMeter without the GUI. To do so, use the followi ...

  9. 【转】Jmeter常见问题

    说明:这些问答是从网上转载的,自己修改了其中的一些内容,如果大家兴趣,可以将大家在使用Jmeter的时候碰到的问题写下来,我们一起补充到这个问答里面,共同努力完善jmeter的资料. 1.  JMet ...

  10. mysql实战优化之五: 更新/插入优化 sql优化

    通常情况下,当访问某张表的时候,读取者首先必须获取该表的锁,如果有写入操作到达,那么写入者一直等待读取者完成操作(查询开始之后就不能中断,因此允许读取者完成操作).当读取者完成对表的操作的时候,锁就会 ...