我在学习Spring aop动态代理的时候,了解到spring aop对没有实现任何接口的类,使用的是Cglib动态代理,因此我在原有的代码上去掉了接口实现(即注释了被切面类的接口实现),体验Cglib动态代理,以下是我的代码
测试类
package org.home717.test;
import org.home717.service.impl.Impl_UserService;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void Test(){
ConfigurableApplicationContext ioc = new ClassPathXmlApplicationContext("spring/aop_01.xml");
System.out.println(ioc.getBean(Impl_UserService.class));
}
}
切面类
package org.home717.util;
import org.springframework.stereotype.Component;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Component
@Aspect
public class LogUtil {
@Before("execution(public * org.home717..*.*(..))")
public void logBefore(){
System.out.println("[proxy] Before");
}
@After("execution(public * org.home717..*.*(..))")
public void logAfter(){
System.out.println("[proxy] After");
}
@AfterReturning("execution(public * org.home717..*.*(..))")
public void logAfterReturning(){
System.out.println("[proxy] AfterReturning");
}
@AfterThrowing("execution(public * org.home717..*.*(..))")
public void logAfterThrowing(){
System.out.println("[proxy] AfterThrowing");
}
}
目标类
package org.home717.service.impl;
import org.home717.service.I_UserService;
import org.springframework.stereotype.Service;
@Service
public class Impl_UserService{//implements I_UserService
public Impl_UserService(){
super();
}
public void get(){
System.out.println("获取了用户信息!");
}
}
但是当我使用Junit运行测试类的时候,出现了以下错误
以下是异常信息:
10月 15, 2021 10:26:20 上午 org.springframework.context.support.AbstractApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'impl_UserService' defined in file [E:\Code\CodeCool\eclipse-workspace\SpringAOP_02\target\classes\org\home717\service\impl\Impl_UserService.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class org.home717.service.impl.Impl_UserService: Common causes of this problem include using a final class or a non-visible class; nested exception is org.springframework.cglib.core.CodeGenerationException: java.lang.VerifyError-->Stack map does not match the one at exception handler 9
Exception Details:
Location:
org/home717/service/impl/Impl_UserService$$EnhancerBySpringCGLIB$$61673c07.<init>()V @9: athrow
Reason:
Current frame's flags are not assignable to stack map frame's.
Current Frame:
bci: @0
flags: { flagThisUninit }
locals: { uninitializedThis }
stack: { 'java/lang/RuntimeException' }
Stackmap Frame:
bci: @9
flags: { }
locals: { }
stack: { 'java/lang/Throwable' }
Bytecode:
0000000: 2a59 b701 27b8 0037 b1bf bb00 4b5a 5fb7
0000010: 004e bf
Exception Handler Table:
bci [0, 9] => handler: 9
bci [0, 9] => handler: 9
bci [0, 9] => handler: 10
Stackmap Table:
full_frame(@9,{},{Object[#73]})
same_locals_1_stack_item_frame(@10,Object[#73])
补充
maven xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>home717</groupId>
<artifactId>SpringAOP_01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- Spring -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.9</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.9</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.9</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.9</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.8.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
<scope>compile</scope>
</dependency>
<!-- commons logging -->
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>
<!-- Junit -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
spring 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"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<context:component-scan base-package="org.home717" />
<aop:aspectj-autoproxy />
</beans>