<?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. css3单冒号和双冒号的区别

    css3中对于伪元素的使用,在项目开发中使用得当将会对代码的可读性又很大的提升.但是对于伪类大家或许都知道是一些选择器的使用,这里总结了关于伪元素中单冒号和双冒号的区别: 再官方定义中规定单冒号都为伪 ...

  2. Python之operator库

    operator库常用方法 Function Syntax add(a+b) a + b concat(seq1,seq2) seq1 + seq2 contains(seq, obj) obj in ...

  3. matlab中一些常用的函数

    stem函数h = stem(x,y); %绘制火柴梗图 ,stem的工作原理是,根据一个x对应一个y,绘制火柴梗图.

  4. ACM学习历程—HDU5701 中位数计数(中位数 && 计数排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5701 这是这次百度之星初赛2B的第六题.之前白山云做过类似的题,省赛完回来,我看了一下大概就有这样的思路:首先枚 ...

  5. uoj #190. 【集训队互测2016】消失的源代码 提交答案题

    Test 1: 发现是一个字母表的映射 把 \('a' \to 'z'\) 打进去找出映射就好了QAQ . Test 2: 求助 \(dalao\) 得知的点.. 答案是 : \(2016x^2 + ...

  6. Excel 2007 打开 UTF-8 编码 CSV 文件的乱码BUG

    http://blog.sina.com.cn/s/blog_6c3b65fd01018dgq.html 打开UTF-8编码的CSV方法: 1) 打开Excel 2007 2) 执行“数据”-> ...

  7. ASP.NET WebApi通过自定义ControllerSelector来自定义Controller的选择

    在web api中,我们可以通过给Cotroller类添加RoutePrefixAttribute来定义url与Controller之间的映射,但是有时候有一些特殊情况下,我们需要做一些特殊处理来将某 ...

  8. zabbix 3.0.2自定义脚本

    http://blog.51cto.com/xiao987334176/1769766 有一个通知队列,如果超过了一定的值,就需要报警一下 查询接口可以返回队列的数量,格式是json,data后面的数 ...

  9. 使用spring框架的JdbcTemplate实现对Oracle数据库的简单操作实例

    最近实现了一个小功能,针对Oracle数据库两张关联表进行查询和修改,因为比较简单,所以选择了spring框架里的JdbcTemplate.JdbcTemplate算是老古董了,是当年spring为了 ...

  10. 1、python基础速成

    基础模块 def prt(age,name):#函数定义 print("%s is %d 年龄 old"%(name,age)) if __name__=="__main ...