spring切面:注解:环绕增强

1.AroundLogger2注解环绕增强类

package aop;

import java.util.Arrays;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class AroundLogger2 {
	private static final Logger log = Logger.getLogger(AroundLogger.class);

	@Around("execution(* biz.impl.*.*(..))")
	public Object around(ProceedingJoinPoint jp) {
		try {
			System.out.println("进入前置增强###");
			log.info("调用的方法:" + jp.getSignature().getName() + ",传递参数:"
					+ Arrays.toString(jp.getArgs()));

			Object result = jp.proceed();
			System.out.println("进入后置增强###");
			log.info("调用的方法:" + jp.getSignature().getName() + ",传递参数:"
					+ Arrays.toString(jp.getArgs()) + ",返回参数:" + result);
			return result;
		} catch (Throwable e) {
			System.out.println("出现异常");
			log.info("调用的方法:" + jp.getSignature().getName() + ",传递参数:"
					+ Arrays.toString(jp.getArgs()) + ",异常信息:" + e);
			e.printStackTrace();
		}
		return null;
	}
}

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>  -->
<bean class="aop.AroundLogger2"></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" />
    </bean>
</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类

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import entity.User;

import biz.UserBiz;
import biz.impl.UserBizImpl;

import ioc.HelloWorld;

public class TestMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		//加载spring容器,解析配置文件
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
         /*HelloWorld helloWorld=(HelloWorld) ac.getBean("helloWorld");
         System.out.println("你好:"+helloWorld.getName()+","+helloWorld.getSet().size()+","+helloWorld.getMap().get("u1"));*/
		 UserBiz userBiz=(UserBiz) ac.getBean("userBiz");
		 User u= userBiz.getUser(101);
		 System.out.println(u.getUname()+","+u.getAge());
	}

}

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值