如何使用编织与aspectJ在编译时在春季项目



我们使用Spring,我们使用Spring AOP。由于使用ProxySpring AOP的性质,我们在调用内部调用时尝试扭曲join point时达到了它的限制。即如果A正在调用

,则B执行的方面将不运行
public void A(){
   B()
}
public void B(){
}

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

这是好的工作。但是,问题是让它与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的Spring autowired bean副本为null

如何让aspectj与maven一起工作

在编译时使用AspectJ,并确保spring自动连接魔术将工作

根据AspectJ文档的aspectOf Chapter。为了让一些模块知道这个方面是一个应该使用aspectOf的方面。Springfeature

 <bean id="a" class="com.someinterface.A" factory-method="aspectOf"></bean>
这将导致上面的A是Spring Bean,作为奖励,Spring将知道这是其他代码的aspect。这足以让Spring在方面内使用Autowire 魔术

说明使用aspectOf需要xml configuration。我试图得到相同的结果与@Configurable,但它没有工作。如果有人有一些信息,这将是伟大的。:)

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

设置spring扫描@Aspect,使其成为春豆

<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;
    }

最新更新