spring 如何使用cglib 代理
-
Spring框架提供了多种代理方式,其中一种就是使用CGLib动态代理。下面我将简要介绍Spring如何使用CGLib代理。
- 添加依赖
首先,在你的项目中添加CGLib的依赖。在Spring的Maven工程中,你可以在pom.xml文件中添加以下依赖配置:
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.0</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.4.0</version> </dependency> </dependencies>- 创建目标类
然后,你需要创建一个需要被代理的目标类。该类可以是一个普通的POJO类,不需要实现任何接口。例如,我们创建一个名为TargetClass的目标类:
public class TargetClass { public void doSomething() { // 执行一些操作 } }- 创建代理类
接下来,你需要创建一个代理类,这个类将使用CGLib来为目标类生成代理对象。可以使用Spring提供的MethodInterceptor接口来实现这个代理类。以下是一个简单的示例:
import org.springframework.cglib.proxy.MethodInterceptor; import org.springframework.cglib.proxy.MethodProxy; import java.lang.reflect.Method; public class TargetClassInterceptor implements MethodInterceptor { @Override public Object intercept(Object obj, Method method, Object[1年前 - 添加依赖
-
Spring框架可以使用CGLIB代理来实现动态代理。CGLIB是一个开源的字节码生成库,它可以在运行时生成字节码,并将它们加载到JVM中。使用CGLIB代理,我们可以绕过接口的限制,对类进行代理。
下面是使用Spring框架进行CGLIB代理的步骤:
- 引入依赖:
需要在项目的pom.xml文件中引入CGLIB依赖。
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.0.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.8.RELEASE</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.3.0</version> </dependency>- 创建一个目标类:
创建一个普通的Java类作为目标类,该类用于被代理。
public class MyTargetClass { public void doSomething() { System.out.println("Doing something..."); } }- 创建一个切面类:
创建一个切面类,用于在目标方法执行前后添加增强逻辑。
public class MyAspect { public void myAdvice() { System.out.println("Before method execution..."); } }- 配置Spring配置文件:
在Spring配置文件中配置代理相关的Bean。
<bean id="myTargetClass" class="com.example.MyTargetClass" /> <bean id="myAspect" class="com.example.MyAspect" /> <bean id="myProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="myTargetClass" /> <property name="interfaces" value="com.example.MyInterface" /> <property name="proxyTargetClass" value="true" /> <property name="interceptorNames"> <list> <value>myAspect</value> </list> </property> </bean>- 使用代理:
在代码中使用代理对象。
public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); MyInterface myProxy = (MyInterface) context.getBean("myProxy"); myProxy.doSomething(); } }通过以上步骤,我们就可以使用Spring框架来使用CGLIB代理。注意,在配置Spring的ProxyFactoryBean时,需要将
proxyTargetClass属性设置为true,以明确使用CGLIB代理而不是JDK动态代理。1年前 - 引入依赖:
-
CGLIB是一个强大的基于字节码生成的代理库,在Spring框架中也用到了CGLIB来实现代理。使用CGLIB代理主要有两种方式:通过自动代理和手动代理。
一、通过自动代理
Spring框架提供了几个自动代理的方式,其中之一是使用@Aspect注解和@EnableAspectJAutoProxy注解。以下是使用CGLIB进行自动代理的步骤:
- 导入相关的Maven依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>需要注意的是,如果使用Spring Boot,则不需要手动添加该依赖项。
- 创建一个切面类,使用@Aspect注解表示该类为一个切面,并且使用@Before、@After等注解来定义切面的行为。
@Aspect @Component public class MyAspect { @Before("execution(* com.example.service.*.*(..))") public void beforeAdvice() { System.out.println("Before method execution"); } @After("execution(* com.example.service.*.*(..))") public void afterAdvice() { System.out.println("After method execution"); } }在上述代码中,@Before注解表示在目标方法执行之前执行切面逻辑,@After注解表示在目标方法执行之后执行切面逻辑。
- 在启动类上添加@EnableAspectJAutoProxy注解,开启自动代理功能。
@SpringBootApplication @EnableAspectJAutoProxy(proxyTargetClass = true) public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }在注解中,proxyTargetClass = true表示使用CGLIB进行代理。如果设置为false,则会使用JDK动态代理。
二、通过手动代理
如果希望手动创建CGLIB代理对象,可以使用org.springframework.cglib.proxy.Enhancer类。以下是使用CGLIB进行手动代理的步骤:
- 导入相关的Maven依赖:
<dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.3.0</version> </dependency>- 创建一个目标类和一个方法,在方法中实现具体的逻辑。
public class MyService { public void doSomething() { System.out.println("Doing something"); } }- 创建一个实现了MethodInterceptor接口的代理类,该类需要实现intercept方法来定义代理逻辑。
public class MyMethodInterceptor implements MethodInterceptor { public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println("Before method execution"); Object result = proxy.invokeSuper(obj, args); System.out.println("After method execution"); return result; } }- 在调用方法的地方创建代理对象。
public class Main { public static void main(String[] args) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(MyService.class); enhancer.setCallback(new MyMethodInterceptor()); MyService proxy = (MyService) enhancer.create(); proxy.doSomething(); } }在上述代码中,Enhancer类将目标类设置为父类,然后将代理逻辑设置为回调对象。最后通过enhancer.create()方法创建代理对象。
这就是使用CGLIB代理的两种常用方法:通过自动代理和手动代理。无论哪种方法,CGLIB都是Spring框架中非常有用的一个功能,可以帮助开发人员实现AOP等一系列功能。
1年前