【问题标题】:How to use weaven with aspectJ in compiletime in spring project如何在spring项目的编译时使用带有aspectJ的编织
【发布时间】:2015-08-26 10:20:41
【问题描述】:

我们使用的是Spring,我们使用的是Spring AOP。由于Spring AOP 使用Proxy 的性质,我们在通话中在通话中扭曲join point 时达到了它的限制。 IE 如果正在调用 A,则 B 执行方面将不会运行

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

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

哪个工作好。但是,问题是让它与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>

编辑

与Spring autowired bean for @Aspect aspect is null 重复

info on how to get aspectj to work with maven

【问题讨论】:

    标签: spring aspectj spring-aop aspectj-maven-plugin


    【解决方案1】:

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

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

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

    这将导致上面的 A 成为 Spring Bean 并且作为奖励,Spring 将知道这是其他代码的 aspect。这足以让Spring 在方面中使用Autowire ma​​gic。

    注意使用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;
        }
    

    【讨论】:

      猜你喜欢
      • 2013-03-18
      • 2019-11-24
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 2012-11-21
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多