开发者

Using Aspects in a java application - when javac/ajc?

开发者 https://www.devze.com 2023-04-05 08:37 出处:网络
I think about using AspectJ in an existing project. I have several pure Java Eclipse projects and I like to create an AOP project.

I think about using AspectJ in an existing project.

I have several pure Java Eclipse projects and I like to create an AOP project.

I'm not quite sure about when ajc is needed and when optional. We use Ant (with javac) as our main build and I would like to avoid changing the build.

Is the following possible:

I have a AspectJ enabled Eclipse and create my aspect project. I create a jar from this and开发者_开发知识库 include this jar with the aspectj jar in the normal eclipse workspace with the other projects. The build includes my aspect jar and the aspectj jar as dependency jars with javac.

Is this enough for working with the aspects ? Or do every project of the application needs to be compiled with ajc ?

The main goal is to keep the current structure of Eclipse setup and build environment as as much as possible as it is now.

Or is this only possible with the annotation style ? (if so can someone link me some information about the weaver and how to do this at runtime ?)

Thank you


If you want to weave your aspect into your codebase, you must use AJC. If you only use javac, even with the annotation, your code won't be weaved by your aspects.

That being said, you don't have to add a lot to your ant build.

something like that should do the job:

<path id="ajclasspath">
    <path refid="classpath"/>
    <pathelement location="${scm.home}/ant_libs/aspectjrt.jar"/>
</path>
<iajc inpath="${classes.dir}" destDir="${classes.dir}" fork="true" maxmem="${aspectj.maxmem}">
    <argfiles refid="aspectj.argfiles.path"/>
    <classpath refid="ajclasspath"/>
</iajc>

In fact, you just build like normal and you add a step with the iajc taking your output dir of the javac compile as the inpath and you put the result in the same directory.

You can also take a jar as input to iajc and produce a jar with all your stuff weaved inside.

Edit: Or you can use runtime weaving, if your app is a web application, it is not too bad. If not, i do not recommand runtime weaving, since each time you will start your app, it might be a lot longer to start. I don't have a lot of experience with runtime weaving, but you can check it out. I know you need a aop.xml to define your aspects.

Regards

0

精彩评论

暂无评论...
验证码 换一张
取 消