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):事务是一个原子操作,由一系列动作组成.事务的原子性确保动作要么全部完成,要么完 ...
随机推荐
- (效果五)js获取客户端ip地址及浏览器信息
在前端开发的时候,有时候为了测试需要得到访问客户的ip地址.虽说是后端来做的,但是我们前端也可以完成. 先说下获取用户ip地址,包括像ipv4,ipv6,掩码等内容,但是大部分都要根据浏览器的支持情况 ...
- (八)java运算符
算数运算符 + - * / % ++ -- class Ysf { public static void main(String[] args) { System.out.println(5/2);/ ...
- Java连接Mysql的基本用法
Java连接数据库(以MySQL为例)2007-04-05 02:23 这篇文章主要以MySQL为例讲下Java如何连接到数据库的. 当然,首先要安装有JDK(一般是JDK1.5. ...
- HohoCoder 1184 : 连通性二·边的双连通分量(+原理证明)
1184 : 连通性二·边的双连通分量 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在基本的网络搭建完成后,学校为了方便管理还需要对所有的服务器进行编组,网络所的老师 ...
- CentOS虚拟机中安装VMWare Tools
1.单击VMWare的[虚拟机]菜单,选择[安装VMWare Tools]命令 2.接着CentOS系统会自动挂载VMWare Tools,并自动打开,如果没有打开可以自己去图形界面打开VMWare ...
- dubbo-demo安装运行指南
步骤步骤:1.安装JDK:2.安装Tomcat:3.安装Zookeeper:4.安装Dubbo: 修改Consumer配置文件
- ror中间一些单复数的规则
基于rails generate生成的东西里面,有一些单复数的规则刚开始很不理解,觉得很复杂,容易弄错,特此记录 model 实际是对于数据库数据的对象化,只体现单个对象,比如模型user有name和 ...
- MyBatis 一对一(OneToOne)__SELECT
1.创建SQL脚本: CREATE TABLE t_person( id int(3) not null auto_increment, name varchar(20) default null ...
- PyCharm中按住Alt键,可以选择一个指定列表,然后对这个数列进行操作,比如删除,增加等等
- nios 使用count binary 例程 只是led不闪
系统id有问题的总结: 1, 复位是否正确.(特别使用拨码开关的) 2, 硬件连接是否有问题.(SDRAM的时序约束可以有,也可以没有) 3, 引脚分配是否正确.(SDRAM的dqm就错过一次) 4, ...