在实践spring aop的四种实现方式时,aop使用注解方式 这三种方式启动时都报同样的错误
问题相关代码,请勿粘贴截图
public interface UserService {
void add();
void del();
void update();
void select();
}
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加一个用户");
}
public void del() {
System.out.println("删除一个用户");
}
public void update() {
System.out.println("更新一个用户");
}
public void select() {
System.out.println("选择一个用户");
}
}
@Aspect
public class AnnotationPointCut {
@Before("execution(* chapter0402.service.impl.UserServiceImpl.*(..))")
public void before(){
System.out.println("方法执行之前");
}
@After("execution(* chapter0402.service.impl.UserServiceImpl.*(..))")
public void after(){
System.out.println("方法执行之后");
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/chapter04/Annotation.xml")
public class aopTest {
@Test
public void testAop(){
ApplicationContext context = new ClassPathXmlApplicationContext("chapter04/Annotation.xml");
UserService proxy = (UserService)context.getBean("userService");//获取注册的bean对象,实例对象变成bean对象,就是代理对象
proxy.add();
}
}
<?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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="chapter0402.service.impl.UserServiceImpl"/>
<bean id="annotationPointCut" class="chapter0403.diy.AnnotationPointCut"/>
<aop:aspectj-autoproxy/>
</beans>
public class MyMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("chapter04/Annotation.xml");
//通过目标的bean id,获得代理对象
UserService proxy = (UserService)context.getBean("userService");//获取注册的bean对象,实例对象变成bean对象,就是代理对象
proxy.add();
}
}
报错内容:
五月 05, 2022 4:43:53 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@27fa135a: startup date [Thu May 05 16:43:53 CST 2022]; root of context hierarchy
五月 05, 2022 4:43:53 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [chapter04/Annotation.xml]
五月 05, 2022 4:43:58 下午 org.springframework.context.support.ClassPathXmlApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils$MethodFilter.and(Lorg/springframework/util/ReflectionUtils$MethodFilter;)Lorg/springframework/util/ReflectionUtils$MethodFilter;
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils$MethodFilter.and(Lorg/springframework/util/ReflectionUtils$MethodFilter;)Lorg/springframework/util/ReflectionUtils$MethodFilter;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
我的解答思路和尝试过的方法 :
使用test测试类来测试和main方法来执行都是报同样的错误
我想要达到的结果:
方法执行之前
增加一个用户
方法执行之后