1.aop/AroundLogger环绕增强类
package aop;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger;
import net.sf.cglib.proxy.MethodProxy;
public class AroundLogger implements MethodInterceptor {
private static final Logger log = Logger.getLogger(AroundLogger.class);
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println("环绕——前置增强");
Object target = arg0.getThis();//获取被代理对象
Method method = arg0.getMethod();//获取被代理方法
Object[] args = arg0.getArguments();//获取参数列表
log.info("调用方法:"+method.getName() +",传参:"+ Arrays.toString(args));
try {
Object obj = arg0.proceed();//调用目标方法,获取目标方法返回值
log.info(obj);
return obj;
} catch (Throwable e) {
log.error("出现异常" + e);
}
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>
<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>
</beans>
3.dao.impl/UserImpl类
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;
}
}
3.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 {
public static void main(String[] args) {
//加载spring容器,解析配置文件
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
UserBiz userBiz=(UserBiz) ac.getBean("userBiz");
//getUser()传入参数,测试环绕增强获取参数列表
User u= userBiz.getUser(101);
System.out.println(u.getUname()+","+u.getAge());
}
}