1.前置增强接口实现类
package aop;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import org.apache.log4j.Logger;
import org.springframework.aop.MethodBeforeAdvice;
public class LoggerBefore implements MethodBeforeAdvice{
private static final Logger log=Logger.getLogger(LoggerBefore.class);
@Override
public void before(Method mehtod, Object[] argList, Object obj)
throws Throwable {
System.out.println("================进入前置增强=====================");
log.info("调用了:"+obj.toString()+"的方法:"+mehtod.getName()+",传参:"+Arrays.toString(argList));
}
}
2.后置增强接口实现类
package aop;
import java.lang.reflect.Method;
import org.apache.log4j.Logger;
import org.springframework.aop.AfterReturningAdvice;
public class AfterReturning implements AfterReturningAdvice{
private static final Logger log=Logger.getLogger(AfterReturning.class);
@Override
public void afterReturning(Object result, Method method, Object[] arg2,
Object target) throws Throwable {
System.out.println("================进入后置增强=====================");
log.info("调用了"+target.toString()+"的方法:"+method.getName()+",返回结果:"+result.toString());
}
}
3.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>
<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>
4.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;
}
}
5.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());
}
}