基于代理类ProxyBean的AOP的实现

一、编写通知类

前置通知:

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();
	}

}

输出结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值