一、编写通知类
前置通知:
package com.springtest.advice;
import java.lang.reflect.Method;
import org.apache.log4j.Logger;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeLogAdvice implements MethodBeforeAdvice {
private Logger logger = Logger.getLogger(BeforeLogAdvice.class);
@Override
public void before(Method method, Object[] arg1, Object target) throws Throwable {
String className = target.getClass().getName();
String methodName = method.getName();
String logToText = "这是"+className+"类的"+methodName+"方法的置前通知";
logger.info(logToText);
}
}
后置通知:
package com.springtest.advice;
import java.lang.reflect.Method;
import org.apache.log4j.Logger;
import org.springframework.aop.AfterReturningAdvice;
public class LogAdvice implements AfterReturningAdvice {
private Logger logger = Logger.getLogger(LogAdvice.class);
@Override
public void afterReturning(Object object, Method method, Object[] args, Object target) throws Throwable {
String className = target.getClass().getName();
String methodName = method.getName();
String logToText = "这是"+className+"类的"+methodName+"方法的置后通知";
logger.info(logToText);
}
}
环绕通知:
package com.springtest.advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger;
public class AroundAdvice implements MethodInterceptor {
private Logger logger = Logger.getLogger(AroundAdvice.class);
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
long beginTime = System.currentTimeMillis();
methodInvocation.proceed();
long endTime = System.currentTimeMillis();
String targetMethodName = methodInvocation.getMethod().getName();
String logInfoText = "环绕通知:"+targetMethodName+"方法调用前时间"+beginTime+"毫秒,调用后时间"+endTime+"毫秒";
logger.info(logInfoText);
return null;
}
}
二、编写applocationContext
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="print" class="com.springtest.dao.Print"></bean>
<bean id="userDao" class="com.springtest.dao.UserDaoImpl"></bean>
<bean id="userBiz" class="com.springtest.dao.UserBizImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="logAdvice" class="com.springtest.advice.LogAdvice"></bean>
<bean id="beforeLogAdvice" class="com.springtest.advice.BeforeLogAdvice"></bean>
<bean id="aroundAdvice" class="com.springtest.advice.AroundAdvice"></bean>
<bean id="ub" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>com.springtest.dao.UserBiz</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>logAdvice</value>
<value>beforeLogAdvice</value>
<value>aroundAdvice</value>
</list>
</property>
<property name="target" ref="userBiz" />
</bean>
</beans>
三、编写 UserDao和UserDaoImpl
package com.springtest.dao;
public interface UserDao {
public void addUser();
}
package com.springtest.dao;
public class UserDaoImpl implements UserDao {
@Override
public void addUser() {
System.out.println("增加用户!!!");
}
}
四、编写UserBiz和UserBizImpl
package com.springtest.dao;
public interface UserBiz {
public void addUser();
}
package com.springtest.dao;
public class UserBizImpl implements UserBiz {
private UserDao userDao;
@Override
public void addUser() {
this.userDao.addUser();
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
五、编写测试类
package com.springtest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.springtest.dao.UserBiz;
public class TestAop {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserBiz userBiz = (UserBiz) applicationContext.getBean("ub");
userBiz.addUser();
}
}
输出结果: