0

我是 springs 新手,正在尝试使用 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...

4

2 回答 2

2

Aspects 应该被标记为 bean。

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

所以,你需要

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

并且不需要<aop:include>

于 2012-05-02T11:01:51.453 回答
0

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

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

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

于 2012-07-04T23:11:05.683 回答