2

私はAOPが初めてです。「Spring in Action」という本を読んでいます。AOP と例の章があります。XMLで作成しましたが、すべてうまくいきます。アスペクトの注釈付き構成の例があります。本のようにワンインワンを作ったのですが、うまくいきません。助けてください。

エラー:

    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'guitar' defined in class path resource [springIdolBeansAnnotation.xml]: 
    Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 c
    an't find referenced pointcut performance

springIdolBeansAnnotation.xml という名前の Bean を含むファイル:

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

    <bean id="guitar" class="com.springinaction.springidol.Guitar"/>
    <aop:aspectj-autoproxy/>
    <bean id="kenny2" class="com.springinaction.springidol.Instrumentalist">
        <property name="song" value="Jingle B"/>
        <property name="instrument" ref="guitar"/>
    </bean>
    <bean id="audience" class="com.springinaction.springidol.Audience"/>
</beans>

ファイル ギター:

package com.springinaction.springidol;

public class Guitar implements Instrument {

    @Override
    public void play() {
        System.out.println("Guitar playing");
    }

}

ファイル 楽器奏者:

package com.springinaction.springidol;

public class Instrumentalist implements Performer {

    private String song;

    private Instrument instrument;

    public Instrumentalist(){}

    @Override
    public void perform() throws Exception {
        System.out.println("Playing "+song + " : ");
        instrument.play();
    }

    public String getSong() {
        return song;
    }

    public void setSong(String song) {
        this.song = song;
    }

    public String screamSong(){
        return song;
    }

    public void setInstrument(Instrument instrument) {
        this.instrument = instrument;
    }

}

ファイル オーディエンス (アスペクト):

package com.springinaction.springidol;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Audience {
    @Pointcut("execution(* com.springinaction.springidol.Performer.perform(..))")
    public void performance(){}

    @Before("performance()")
    public void takeSeats(){
        System.out.println("The audience is takig their seats");
    }

    @Before("performance()")
    public void turnOffCellPhones(){
        System.out.println("The audience is turning off their cellphones");
    }

    @AfterReturning("performance()")
    public void applaud(){
        System.out.println("CLAP CLAP CLAP CLAP CLAP");
    }

    @AfterThrowing("performance()")
    public void demandRefund(){
        System.out.println("Boo! We want our money back");
    }

    @Around("performance()")
    public void watchPerformance(ProceedingJoinPoint joinpoint){
        try {
            System.out.println("Theaudienceistakingtheirseats.");
            System.out.println("Theaudienceisturningofftheircellphones");
            long start=System.currentTimeMillis();
            joinpoint.proceed();
            long end=System.currentTimeMillis();
            System.out.println("CLAP CLAP CLAP CLAP CLAP");
            System.out.println("Theperformancetook"+(end-start)
            + "milliseconds.");
        } catch(Throwable t){
            System.out.println("Boo!Wewantourmoneyback!");
        }
    }
}

メインファイル:

package com.springinaction.springidol;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringIdolMain {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("springIdolBeansAnnotation.xml");
        Performer performer = (Performer) ctx.getBean("kenny2");
        try {
            performer.perform();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
4

3 に答える 3

4

はい、古い瓶が原因である可能性があります。spring 3.x jar と confiduration を使用していると仮定します。最新のjarを使用してみてください

数日前に同様の問題がありましたorg.springframework.beans.factory.BeanCreationException: Error creating bean

私の場合、それは古い瓶の問題でした私の問題

最新の jar を使用してアプリを実行してみてください:

aopalliance-1.0.jar
asm-3.3.1.jar
aspectj-1.7.1.jar
aspectjrt-1.7.0.jar
aspectjweaver-1.7.0.jar
cglib-2.2.2.jar
于 2012-12-19T19:34:18.310 に答える
0

参照:Spring AOP、構成を確認してください@Pointcut

于 2012-09-20T11:28:31.930 に答える
0

1.7 jar の依存関係を追加すると、問題が解決しました。

    <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>
于 2013-11-16T06:40:13.367 に答える