(6)spring的动态代理

本文深入探讨了Java动态代理的实现原理,通过自定义LogProxy类和使用Spring的AOP特性,展示了如何在不修改原有业务代码的基础上,为方法调用添加日志记录功能。文章详细介绍了如何利用注解(@LogInfo)和Spring的配置来实现动态代理,以及如何在Service层注入动态代理的DAO对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代理(Proxy)是一种设计模式,通过访问代理对象访问目标对象,好处:可以在实现目标对象功能的基础上,增加额外的功能,并且不修改已经写好的代码。

 

代理类:

public class LogProxy implements InvocationHandler{
private Object target;
	
	public static Object getInstance(Object target) {
		LogProxy LogProxy=new LogProxy();
		LogProxy.target=target;
		Object obj=Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), LogProxy);
		//第三个参数是实现了InvocationHandler的对象
		return obj;//返回代理对象
	}
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		// TODO 自动生成的方法存根】
		
		//只要一执行代理对象的方法,就会进入invoke方法
		if(method.isAnnotationPresent(LogInfo.class)) {//判断该方法是否有annotation存在
//(通过判断该方法的接口IUserDao)
			LogInfo li=method.getAnnotation(LogInfo.class);//获取Annotation
			Logger.info(new Date()+","+li.value());//获取Annotation的value
		}
		Object result=method.invoke(target, args);//执行的是目标对象target的方法
		return result;//执行该方法的返回值
	}

}

beans.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>
<context:component-scan base-package="spring"/>
<bean id="UserDynamicDao" class="spring.proxy.LogProxy" factory-method="getInstance">
<constructor-arg ref="UserDao"/>
</bean>
</beans>
<bean id="UserDynamicDao" class="spring.proxy.LogProxy" factory-method="getInstance">
<constructor-arg ref="UserDao"/>
</bean>

 

LogInfo(annotation):

@Retention(RetentionPolicy.RUNTIME)
public @interface LogInfo {
public String value() default "";
}

注意:(1)要加上@Retention(RetentionPolicy.RUNTIME)

 

IUserDao(加入annotation的value):

public interface IUserDao {
@LogInfo("add")
public void add(User user);
	
@LogInfo("delete")
public void delete(int id);
public User load(int id);
}

 

在service中注入动态dao:

@Resource(name="UserDynamicDao")
	public void setUserDao(IUserDao userDao) {
		UserDao = userDao;
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值