spring切面:注解:抛出异常增强
来源:互联网 发布:jdk 7u51 windows x32 编辑:程序博客网 时间:2024/06/05 21:05
用注解定义增强
在项目中添加spring AOP相关的jar文件;
使用注解定义前置增强和后置增强实现日志功能;
编写spring配置文件,织入注解定义的增强。
1.aop/UserLogger注释增强类
package aop;import java.util.Arrays;import org.apache.log4j.Logger;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.AfterReturning;@Aspectpublic class UserLogger { private static final Logger log = Logger.getLogger(UserLogger.class);@Before("execution(* biz.impl.*.*(..))")public void before(JoinPoint jp){ System.out.println("注解前置增强"); System.out.println("调用的方法:"+jp.getSignature()+",传递参数:"+Arrays.toString(jp.getArgs()));}@AfterReturning(pointcut="execution(* biz.impl.*.*(..))",returning="returnResult")public void afterReturning(JoinPoint jp,Object returnResult){ System.out.println("注解后置增强"); System.out.println("调用的方法:"+jp.getSignature()+",传递参数:"+Arrays.toString(jp.getArgs())+",返回参数:"+returnResult);}@AfterThrowing(pointcut="execution(* biz.impl.*.*(..))",throwing="e")public void afterThrowing(JoinPoint jp,RuntimeException e){ log.error("调用的方法:"+jp.getSignature()+",传递参数:"+Arrays.toString(jp.getArgs())+",方法发生异常:"+e);}}
2.applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!-- 将前置增强类配成bean,交给spring管理 <bean id="loggerBefore" class="aop.LoggerBefore"></bean> 将后置增强类配成bean,交给spring管理 <bean id="afterReturning" class="aop.AfterReturning"></bean> 配置切面(将增强类关联给某个条件范围内的方法) <bean id="errorLogger" class="aop.ErrorLogger"></bean> --> <bean id="aroundLogger" class="aop.AroundLogger"></bean> <bean class="aop.UserLogger"></bean> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- <aop:config> 配置切入点pointcut <aop:pointcut expression="execution(* biz.impl.*.*(..))" id="myPoint"/> <aop:advisor advice-ref="loggerBefore" pointcut-ref="myPoint"/> <aop:advisor advice-ref="afterReturning" pointcut-ref="myPoint"/> <aop:advisor advice-ref="errorLogger" pointcut-ref="myPoint"/> <aop:advisor advice-ref="aroundLogger" pointcut-ref="myPoint"/> </aop:config> --> <bean id="userBiz" class="biz.impl.UserBizImpl" p:userDao-ref="userDao"> <!-- <property name="userDao" ref="userDao2"></property> --> </bean> <bean id="userDao" class="dao.impl.UserDaoImpl" /> <bean id="userDao2" class="dao.impl.UserDaoImpl2" /></beans>
3.dao.impl/UserDaoImpl类
package dao.impl;import javax.management.RuntimeErrorException;import dao.UserDao;import entity.User;public class UserDaoImpl implements UserDao { @Override public User findUser() { System.out.println("===========Dao层查询User 1=============="); User u=new User("Tom",22); throw new RuntimeException("出现异常"); //return u; }}
4.Test方法
public static void main(String[] args) { //加载spring容器,解析配置文件 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); UserBiz userBiz=(UserBiz) ac.getBean("userBiz"); User u= userBiz.getUser(101); System.out.println(u.getUname()+","+u.getAge()); }
0 0
- spring切面:注解:抛出异常增强
- spring切面:注解:环绕增强
- spring切面:注解:最终增强
- spring切面:接口:异常增强
- spring切面:schema:前后异常增强
- Spring AOP 抛出增强捕获异常
- Spring自动创建异常抛出增强代理
- Spring-AOP 通过配置文件实现 异常抛出增强
- 关于spring中@Transactional注解抛出异常不回滚的现象
- spring中@Transactional注解抛出异常不回滚的现象
- spring切面:接口:环绕增强
- spring切面:注解:前后置增强(不带参数、函数详情)
- spring切面:注解:前后置增强(带参数、函数详情)
- Spring切面二使用注解
- 异常抛出增强使用方法及案例
- spring切面:接口:前后置增强
- spring 使用aop注解版会抛出奇怪的异常的解决方法
- spring aop, aspectj 注解声明切面
- spring切面:注解:环绕增强
- 设计模式之14 - 命令模式Command
- Java虚拟机学习(3): 类加载机制
- 寒假的c语言进阶
- rst
- spring切面:注解:抛出异常增强
- VTK修炼之道12:基本数据结构_如何把几何结构&拓扑结构加入到数据集
- Java虚拟机学习(4):JDK可视化监控工具
- phpcms中的数组转码
- maven+springMVC+hibernate工程搭建(四简单base层实现及demo)
- 设计模式之15 - 解释器模式Interpreter
- python 函数参数传值还是传引用
- 4.电话拨号器的实现
- Linux下配置防火墙