无法使用注释使用Spring Advice(@Before)

时间:2012-05-02 10:38:26

标签: spring aop spring-annotations

我是初次尝试使用java建议运行一个简单的java应用程序....

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-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 
    <aop:aspectj-autoproxy>
        <aop:include name="com.cts.two.Advices"/>
    </aop:aspectj-autoproxy>    
    <context:annotation-config/>
    <context:component-scan base-package="com.cts.two"></context:component-scan>
</beans>

建议类

package com.cts.two;

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 Advices implements Adv{    

    @Pointcut("execution(* com.cts.two.*.*(..))")
    public void advice(){

    }
    @Before("advice()")
    public void before(JoinPoint name) throws Throwable{
        System.out.println("inside advices");
        /*System.out.println(name.getClass() + " this is get class");
        System.out.println(name.getSignature().getName() + " this is the get signatue and get name");*/
    }
}

需要应用建议的类...我希望在下面提到的test()方法之前执行Advice类的before方法

package com.cts.two;

import org.springframework.stereotype.Component;
@Component

public class ClassA {
    private ClassB b= new ClassB();

    public void setB(ClassB b) {
        this.b = b;
    }
    public void test(){
        System.out.println("inside classA test");
        //b.test();
    }

}

方法/测试类/主类的调用者

package com.cts.two;

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

public class CallerAB {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "AllAnnotations.xml");
        ClassA calledA = (ClassA) context.getBean("classA");
        calledA.test();
    }

}

问题是当我直接运行代码时,会执行A类的测试方法,但建议不是...... 好心劝告.. 我错过了什么吗??? 还添加了AspectJ 1.6.12 jar ......

2 个答案:

答案 0 :(得分:2)

方面应该被称为bean。

@Aspect不会自动执行此操作,<aop:include>也不会自动执行此操作(它会对可用作方面的bean设置其他限制)。

所以,你需要

@Aspect
@Component
public class Advices implements Adv { ... }

并且不需要<aop:include>

答案 1 :(得分:0)

正如@axtavt的回答中所提到的,您需要添加@Component注释。但您还需要删除<aop:include>。你的弹簧布线xml应该是:

<aop:aspectj-autoproxy/>
<context:annotation-config/>
<context:component-scan base-package="com.cts.two"/>

spring AOP documentation中所述,name元素中的<aop:include>属性应该是bean名称,而不是类名。明确指定bean会覆盖Spring的自动检测并错误地指定它意味着根本没有使用方面。

相关问题