Java AOP 注解配置与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: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配置的更多相关文章
- 死磕Spring之AOP篇 - Spring AOP注解驱动与XML配置
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)
1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...
- IDEA用maven创建springMVC项目和配置(XML配置和Java配置)
1.DEA创建项目 新建一个maven project,并且选择webapp原型. 然后点击next 这里的GroupId和ArtifactID随意填写,但是ArtifactID最好和你的项目一名一样 ...
- Spring的注解配置与XML配置之间的比较
注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作. 如:使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO ...
- Spring注解配置和xml配置优缺点比较
Spring注解配置和xml配置优缺点比较 编辑 在昨天发布的文章<spring boot基于注解方式配置datasource>一文中凯哥简单的对xml配置和注解配置进行了比较.然后朋 ...
- Hibernate实现有两种配置,xml配置与注释配置
hibernate实现有两种配置,xml配置与注释配置. (1):xml配置:hibernate.cfg.xml (放到src目录下)和实体配置类:xxx.hbm.xml(与实体为同一目录中) < ...
- hibernate实现有两种配置,xml配置与注释配置。<转>
<注意:在配置时hibernate的下载的版本一定确保正确,因为不同版本导入的jar包可能不一样,所以会导致出现一些错误> hibernate实现有两种配置,xml配置与注释配置. (1) ...
- spring aop注解方式与xml方式配置
注解方式 applicationContext.xml 加入下面配置 <!--Spring Aop 启用自动代理注解 --> <aop:aspectj-autoproxy proxy ...
- Spring 中的事务操作、注解、以及 XML 配置
事务 事务全称叫数据库事务,是数据库并发控制时的基本单位,它是一个操作集合,这些操作要么不执行,要么都执行,不可分割.例如我们的转账这个业务,就需要进行数据库事务的处理. 转账中至少会涉及到两条 SQ ...
- Spring MVC 中使用AOP 进行事务管理--XML配置实现
1.今天写一篇使用AOP进行事务管理的示例,关于事务首先需要了解以下几点 (1)事务的特性 原子性(Atomicity):事务是一个原子操作,由一系列动作组成.事务的原子性确保动作要么全部完成,要么完 ...
随机推荐
- HDU - 6314:Matrix (广义容斥)(占位)
Samwell Tarly is learning to draw a magical matrix to protect himself from the White Walkers. the ma ...
- HihoCoder 1158 : 质数相关 (最大独立集)
质数相关 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 两个数a和 b (a<b)被称为质数相关,是指a × p = b,这里p是一个质数.一个集合S被称为质数相关 ...
- Django之mysql表单操作
在Django之ORM模型中总结过django下mysql表的创建操作,接下来总结mysql表记录操作,包括表记录的增.删.改.查. 1. 添加表记录 class UserInfo(models.Mo ...
- NSNotificationCenter 通知中心传值
1.NSNotification 这个类可以理解为一个消息对象,其中有三个成员变量. 这个成员变量是这个消息对象的唯一标识,用于辨别消息对象. @property (readonly, copy) N ...
- 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom
传送门 题目大意:形成一个环的牛可以跳舞,几个环连在一起是个小组,求几个小组. 题解:tarjian缩点后,求缩的点包含的原来的点数大于1的个数. 代码: #include<iostream&g ...
- 解决----Word无法创建工作文件,请检查临时环境变量
用户在运行Word2003或打开Word2003文档时,可能会出现“Word无法创建工作文件,请检查临时环境变量”的错误提示,此问题主要是由于Word2003的用户设置出现损坏而造成的.网上针对此问题 ...
- Eclipse 进入前选择Workspace
如果选择了默认的Workspace会有一个问题. 打开一个workspace的时候,再次打开eclipse会报错,提示当前workspace正在被使用,然后让选择workspace. 最好的方法是每次 ...
- 关于bonecp和QuerRunner
之前一直以为boneCP和QueryRunner是绑定的,但是其实不是,后者来自于commons-dbUtils,BoneCP就是负责连接池. while preparing SQL: UPSERT ...
- android中MediaPlayer类的用法
用法直接看sample package com.turtle920.androidaudioprocess; import android.media.MediaPlayer; import andr ...
- 2、通过HBase API进行开发
一.将HBase的jar包及hbase-site.xml添加到IDE 1.到安装HBase集群的任意一台机器上找到HBase的安装目录,到lib目录下下载HBase需要的jar包,然后再到conf目录 ...