基于代理类ProxyBean的AOP的实现
2015-12-22 19:53
369 查看
一、编写通知类
前置通知:
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();
}
四、编写UserBiz和UserBizImpl
package com.springtest.dao;
public interface UserBiz {
public void addUser();
}
五、编写测试类
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();
}
}
输出结果:
前置通知:
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();
}
}
输出结果:
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- yui3的AOP(面向切面编程)和OOP(面向对象编程)
- JavaScript AOP编程实例
- 使用AOP改善javascript代码
- 初识SmartJS - AOP三剑客
- 模拟Spring的简单实现
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- java使用动态代理来实现AOP(日志记录)的实例代码
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解
- java实现简单美女拼图游戏
- 详解Java的Spring框架中的事务管理方式
- 解析Java的Spring框架的BeanPostProcessor发布处理器
- Java开发框架spring实现自定义缓存标签