对对对~

Spring_AOP中AopTest出现java.lang.NoClassDefFoundError异常

 

 

 AopTest异常:

at test.AopTest.main(AopTest.java:17)
Caused by: java.lang.NoClassDefFoundError: com/noma/aop/service/impl/loginServiceImpl (wrong name: com/noma/aop/service/impl/LoginServiceImpl)
at java.lang.ClassLoader.defineClass1(Native Method)

代码:

applicationContext.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:aop="http://www.springframework.org/schema/aop"
       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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--1.配置包扫描-->
    <context:component-scan base-package="com.javasm.aop"></context:component-scan>
    <!-- 2.开启aspectj-autoproxy-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>
View Code

E:\4032\第三阶段\6-spring\code\下午\1\4-spring\src\com\javasm\aop\aspect\MyAspect.java:

package com.noma.aop.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @Author:wenjie
 * @Version:1.0
 * @Date:2022/2/22-22:24
 * @Since:jdk1.8
 * @Description:
 */
@Component
@Aspect
public class MyAspect {

    @Pointcut(value = "execution(* com.noma.aop.service.impl.UserServiceImpl.*(..))")
    public void userServicePointCut(){}

    @Pointcut(value = "execution(* com.noma.aop.service.impl.loginServiceImpl.*(..))")
    public void loginServicePointCut(){}

    @Pointcut(value = "execution(* com.noma.aop.service.impl.logServiceImpl.*(..))")
    public void logServicePointCut(){}

    //前置通知
    @Before("userServicePointCut()||loginServicePointCut()")
    public void before(){
        System.out.println("before.........");
    }

    //后置通知
    @AfterReturning("logServicePointCut()")
    public void afterReturn(){
        System.out.println("afterReturn .........");
    }

    //最终通知
    @After("logServicePointCut()")
    public void after(){
        System.out.println("after .........");
    }

    // 异常通知
    @AfterThrowing("userServicePointCut()")
    public void afterThrowing(){
        System.out.println("afterThrowing ....... ");
    }

    //环绕通知
    @Around("logServicePointCut()||loginServicePointCut()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕通知 开启事务。。。。。");
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("环绕通知 提交事务。。。。。");

    }
}
View Code 

AopTest代码:

package test;

import com.noma.aop.service.LogService;
import com.noma.aop.service.LoginService;
import com.noma.aop.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author:liulei
 * @Version:1.0
 * @Date:2022/2/22-15:11
 * @Since:jdk1.8
 * @Description:
 */
public class AopTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userServiceProxy = ac.getBean("userService", UserService.class);
        userServiceProxy.addUser();
        System.out.println("===============================");
        userServiceProxy.updateUser();

        System.out.println("+++++++++++++++++++++++++++++++++++++++++++");
        LoginService loginServiceProxy = ac.getBean("loginService", LoginService.class);

        loginServiceProxy.login();
        System.out.println("================");
        loginServiceProxy.logOut();
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++");
        LogService logServiceProxy = ac.getBean("logService", LogService.class);

        logServiceProxy.log();
    }
}
View Code

问题:

LoginService loginServiceProxy = ac.getBean("loginService", LoginService.class);
指定LoginService.class未找到

解决:
package test;

import com.javasm.aop.service.LogService;
import com.javasm.aop.service.LoginService;
import com.javasm.aop.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author:liulei
 * @Version:1.0
 * @Date:2022/2/22-15:11
 * @Since:jdk1.8
 * @Description:
 */
public class AopTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userServiceProxy = ac.getBean(UserService.class);
        userServiceProxy.addUser();
        System.out.println("===============================");
        userServiceProxy.updateUser();

        System.out.println("+++++++++++++++++++++++++++++++++++++++++++");
        LoginService loginServiceProxy = ac.getBean(LoginService.class);

        loginServiceProxy.login();
        System.out.println("================");
        loginServiceProxy.logOut();
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++");
        LogService logServiceProxy = ac.getBean(LogService.class);

        logServiceProxy.log();
    }
}
View Code

(具体原因还不知道 ~~~ 简单记录一下 ~~)

posted on 2022-02-22 23:04  NOMA~  阅读(109)  评论(0编辑  收藏  举报

导航