创建在类路径资源[applicationContext.xml]中定义的名称为'opBean'的bean时出错

时间:2018-11-03 06:01:10

标签: spring aspectj spring-aop

线程“主”中的异常org.springframework.beans.factory.BeanCreationException:创建在类路径资源[applicationContext.xml]中定义的名称为“ opBean”的bean时出错:bean的初始化失败;嵌套的异常是java.lang.NoClassDefFoundError:com / springlearn / operation(错误名称:com / springlearn / Operation)

Operation.java

package com.springlearn;

public class Operation {

    public void msg() {System.out.println("MSG Invoked");}

    public int m() {
        System.out.println("M invoked");
        return 2;
    }

    public int k() {
        System.out.println("K invoked");
        return 3;
    }

}

TrackOperation.java

package com.springlearn;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class TrackOperation {

    @Pointcut("execution(* operation .*(..))")
    public void k() {

    }

    @Before("k()")
    public void myadvice(JoinPoint jp) {
        System.out.println("Additional Concern");
    }


}

Test.java

package com.springlearn;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test 
{
    public static void main( String[] args )
    {
       ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
       Operation e=(Operation)context.getBean("opBean");
       System.out.println("Calling MSG");
       e.msg();
       System.out.println("Calling M");
       e.m();
       System.out.println("Calling K");
       e.k();
    }
}

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"   
       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="opBean" class="com.springlearn.Operation">   </bean>  
    <bean id="trackMyBean" class="com.springlearn.TrackOperation"></bean>  

    <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean>  

</beans>

pom.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>SpringLearn</groupId>
  <artifactId>springlearn</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springlearn</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>3.0.5.RELEASE</spring.version>
  </properties>
<dependencies>

        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
</dependency>
 <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.7.0</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.7.0</version>
    </dependency>
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>3.2.9</version>
</dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

1 个答案:

答案 0 :(得分:0)

确保在您的方面类中,目标对象类在切入点表达式下的拼写正确

@Pointcut("execution(* Operation.*(..))") 
//Operation class instead of operation case sensitivity