weixin_41792857 2022-05-05 17:03 采纳率: 0%
浏览 34

aop使用注解方式,启动服务报错

在实践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方法来执行都是报同样的错误

我想要达到的结果:
方法执行之前
增加一个用户
方法执行之后

  • 写回答

1条回答 默认 最新

  • 吕布辕门 后端领域新星创作者 2022-05-05 17:11
    关注

    同样的问题,如有帮助,请采纳!
    https://blog.csdn.net/weixin_51757064/article/details/121756659

    img

    评论

报告相同问题?

问题事件

  • 创建了问题 5月5日

悬赏问题

  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题
  • ¥15 vs2019的js智能提示
  • ¥15 关于#开发语言#的问题:FDTD建模问题图中代码没有报错,但是模型却变透明了
  • ¥15 uniapp的h5项目写一个抽奖动画