0

我们正在使用Spring和使用Spring AOP. 由于使用的性质,Spring AOP当我们在通话中进行随叫随到Proxy时,我们达到了它的限制join point。即 ,如果正在调用 A,则 B 执行方面将不会运行

public void A(){
   B()
}
public void B(){
}

为了解决这个问题,我们ApsectJ在编译时使用编织。

哪个工作好。但是,问题是让它与 ie 配合得很好,Spring Bean即让Autowired工作在方面类中。

Pom.xml Maven 插件

        <!-- AspectJ configuration -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.7</version>
            <configuration>
                <complianceLevel>1.8</complianceLevel>
                <source>1.8</source>
                <target>1.8</target>
                <showWeaveInfo>true</showWeaveInfo>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

编辑

@Aspect aspect的 Spring autowired bean 的副本为 null

有关如何让 aspectj 与 maven 一起工作的信息

4

1 回答 1

0

在编译时使用 AspectJ 并确保 spring autowired magic 可以工作

根据AspectJ文档aspectOf Chapter。为了让某个模块知道那个方面是一个人应该使用的东西的一个方面aspectOfSpringfeature

 <bean id="a" class="com.someinterface.A" factory-method="aspectOf"></bean>

这将导致上面的 A 成为 aSpring Bean并且作为奖励Spring将知道这是一个aspect其他代码。这足以Spring使用方面内部的Autowire 魔法。

注意使用aspectOf需要xml configuration. 我试图得到相同的结果,@Configurable但没有奏效。如果有人对此有一些信息,那就太好了。:)

奖励 - 使用Spring AOP方面的代理(在运行时)

设置spring扫描@Aspect并使其成为spring bean

<context:component-scan base-package="com.centure" >
     <context:include-filter type="annotation"     expression="org.aspectj.lang.annotation.Aspect"/> 
</context:component-scan>

在这种情况下,一切都会开箱即用

private SomeService service;
public SomeService getService() {
    return service;
}

@Autowired
public void setService(SomeService) {
    this.service = service;
}
@Aspect
public class myAspect {
    @Pointcut("execution(public * com.myinterface.save(..))")
         public void save() {
    }

@Around("myAspect () && args(thearg)")
public Object doBasicProfiling(ProceedingJoinPoint pjp, TheObject thearg)
         throws Throwable {

    Object retVal = null;
    try {
        retVal = pjp.proceed();
    } catch (Throwable e) {
        e.printStackTrace();
    }

    return retVal;
    }
于 2015-08-26T10:34:08.497 回答