SarahhLee
SarahhLee

Reputation: 1

Spring AOP configured with XML doesn't work

I'm trying to create a Spring AOP program configured with XML.

But i got an error:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'performanceImpl' defined in class path resource [concert/concertConfig.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Cannot create inner bean '(inner bean)#7cbd213e' of type [org.springframework.aop.aspectj.AspectJMethodBeforeAdvice] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#7cbd213e': Cannot resolve reference to bean 'performance' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'performance': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.AspectJExpressionPointcut]: No default constructor found; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException

Looks like it couldn't create the Bean 'performanceImpl', i've checked my code a lot of times, but still have no idea about it. Wish someone could find the problem.

This is my code:

    package concert;
    public interface Performance {
        public void perform();
    }

    package concert;
    public class PerformanceImpl implements Performance {
        @Override
        public void perform() {
            System.out.println("Performing now!");
        }
    }

Aspect

package concert;
public class Audience{
public void silencePhone() {
    System.out.println("Silencing phone please!");
}

public void takeSeats() {
    System.out.println("Taking seats please!");
}

public void clap() {
    System.out.println("CLAP CLAP CLAP!");
}

public void demandRefund() {
    System.out.println("You can demand the refund.");
}
}

XML file

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

<bean id="performanceImpl" class="concert.PerformanceImpl"></bean>
<bean id="audience" class="concert.Audience"></bean>

<aop:config>
    <aop:aspect ref="audience">
        <aop:pointcut id="performance"
            expression="execution(** concert.Performance.perform(..))"/>

        <aop:before pointcut-ref="performance"
            method="silencePhone"/>

        <aop:before pointcut-ref="performance"
            method="takeSeats"/>

        <aop:after pointcut-ref="performance"
            method="clap"/>

        <aop:after-throwing pointcut-ref="performance"
            method="demandRefund"/>
    </aop:aspect>
</aop:config>

</beans>

JUnitTest

package concert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConcertTest {
@Test
public void test() {
    ApplicationContext cpx = new 
    ClassPathXmlApplicationContext("concert/concertConfig.xml");
    Performance performance = cpx.getBean("performanceImpl",
    PerformanceImpl.class);
    performance.perform();
}
}

Upvotes: 0

Views: 1450

Answers (1)

Hossein Mohammadi
Hossein Mohammadi

Reputation: 301

If you see the error message you will see:

Failed to instantiate [org.springframework.aop.aspectj.AspectJExpressionPointcut]: No default constructor found; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException

and that is because you did not add needed libraries into your project. If you are using maven in your project you need to add at least these dependencies:

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

considering the versions:

<properties>
    <spring.version>5.2.0.RELEASE</spring.version>
    <aspectj.version>1.9.5</aspectj.version>
</properties>

Upvotes: 0

Related Questions