为什么Spring AOP不会在运行时编织外部jar? [英] Why Spring AOP is not weaving external jars at runtime?

查看:151
本文介绍了为什么Spring AOP不会在运行时编织外部jar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Spring 3的java应用程序构建。这个项目有另一个jar作为依赖。

I have a java application build upon Spring 3. This project has another jar as a dependency.

这个依赖包含一个 @org。 aspectj.lang.annotation.Aspect class(比方说, com.aspectprovider.aspects.MyAspect )。有一个 @Before 建议从实现接口 Foo 的类编织方法。类似于:

This dependency contains a @org.aspectj.lang.annotation.Aspect class (lets say, com.aspectprovider.aspects.MyAspect). There's a @Before advice to weave a method from classes that implements the interface Foo. Something like:

@Before("execution(* com.project.Foo.save(..))")

Foo 界面可以在项目内或在另一个罐子里。这个例子无关紧要。

The Foo interface can be inside the "project" or in another jar. It doesn't matter for this example.

我的项目包含实现 Foo 的类。当然,这些是我希望它编织的类。

My project contains classes that implements Foo. Those are the classes that I want it to be weaved, of course.

My Spring应用程序上下文配置文件( applicationContext.xml )包含以下行:

My Spring application context configuration file (applicationContext.xml) contains the line:

<aop:aspectj-autoproxy />

我还将方面声明为bean,并注入一些属性:

I also declare the aspect as a bean, and inject some properties:

<bean id="myAspect" class="com.aspectprovider.aspects.MyAspect"
  factory-method="aspectOf" >
  <property name="someproperty" value="somevalue" />
</bean>

通过记录我可以看到 MyAspect 是实例化并注入属性。但方法保存不被截获。这就是问题所在。

Trough logging I can see that MyAspect is instantiated and the properties are injected. But the method save is not intercepted. This is the problem.

如果我将方面类从jar复制到具有Spring的应用程序,它就可以工作。当这些方面包含在外部jar中时,方法save不会被截获。有什么线索吗?

If I copy the aspect classes from the jar to the application that has Spring, it works. When those aspects are contained in external jars, the method save is not intercepted. Any clues?

编辑:我如何调用Foo的保存方法:

//in a JSF managed bean
@Inject
private Foo myFoo;  //there's a implementation of Foo in a package that spring is looking at. So it is injected correctly.

public String someAction() {
    myFoo.save("something"); //the @Before advice is only called if the class containing the aspect is not in an external jar
}


//in a class with a main method
void main(String[] ars) {
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    //right after the previous line, I can see in the log that MyAspect is instantiated.
    Foo myFoo = ac.getBean(Foo.class);
    myFoo.save("something"); //the @Before advice is only called if the class containing the aspect is not in an external jar
}

基本上,我的 applicationContext.xml 包含以下行:

Basically, my applicationContext.xml has the following lines:

<context:annotation-config />
<context:component-scan base-package="com.project" />
<context:component-scan base-package="com.aspectprovider.aspects" />
<aop:aspectj-autoproxy />
<bean id="myAspect" class="com.aspectprovider.aspects.MyAspect" factory-method="aspectOf" >
    <property name="someproperty" value="somevalue" />
</bean>

我认为我不需要像

<context:component-scan  base-package="com.project">
    <context:include-filter type="aspectj" expression="com.aspectprovider.aspects.*" />
</context:component-scan>


推荐答案

我最终宣布了春天的方方面面applicationContext xml config并删除注释。

I've ended up declaring the aspects in the spring's applicationContext xml config and removing the annotations.

到目前为止工作的是为maven使用aspectj插件,但每次我在eclipse中更改一个类时,我都必须运行 $ mvn compile (因为eclipse不知道方面,并且正在编译没有它们的类),对于任何使用<$ c $的人来说这是一件非常糟糕的事情。 c> MyAspect 。

What was working so far was using the aspectj plugin for maven, but everytime I changed a class in eclipse, I had to run $ mvn compile (because eclipse doesn't know the aspects, and was compiling the classes without them), and that's an awful thing to say to anybody that will use MyAspect.

然后我创建了一个配置文件并记录:使用 MyAspect ,只需将此配置规则导入spring的上下文配置。

Then I just created a config file and documented: to use MyAspect, just import this config rules to your spring's context configuration.

这篇关于为什么Spring AOP不会在运行时编织外部jar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆