Académique Documents
Professionnel Documents
Culture Documents
More
Next Blog
Create Blog
Sign In
Java Programming
Tuesday, August 28, 2012
Blog Archive
2015
(2)
2012
(2)
August
(2)
This blog will discuss how to configure Netbeans with Spring AOP/AspectJ, without using the
AspectJ compiler. I wrote this blog
to document the steps that I went through to get Spring
AOP/AspectJ enabled with Netbeans without needing the AspectJ compiler. I used
load-time
weaving with AspectJ in the Spring framework. My original problem was that I built a JavaFX
application and I wanted to include a logging framework, but I did not want to write a ton of
This blog will provide you with what I did to enable this within Netbeans. It will not be step by step
instructions, but it should provide you with all the information you need to get it working in your
A) How to get AOP working in your existing application with System.out statements
B) How to get Log4J working with Spring AOP/AspectJ to write out to a log file. This will be
covered in my next post.
This blog will not cover (maybe I can cover it in another post)
2) AspectJ 1.7.0. I installed it on my machine, but I think you only need the following jar files.
http://www.eclipse.org/downloads/download.php?file=/tools/aspectj/aspectj-1.7.0.jar
3) Spring Framework 3.0.6 Release - this should already be included with Netbeans 7.1.
4) Log4j 1.2
http://logging.apache.org/log4j/1.2/download.html
7) To understand the "why" you will have to read through the various sources below after you
have read the post.
Sources:
This is a good source to use if you want more information regarding the Spring AOP framework
About Me
Sean
Williams
1) Open the properties of your project by right-clicking and selecting Properties and add the
following 3 libraries to your compile-time libraries. I will not cover log4j in this post, so you can
skip adding log4j for now or you can include it and you will be ready for the
next post.
a)
1b) Then click on "Run" in the Categories section and add the following statement in the VM
Options text box:
-javaagent:"C:\path\to\spring-instrument-3.0.6.RELEASE.jar"
Note the double quotes, which you will need if your path contains spaces. If you are using
Netbeans 7.1 then this should already be included on your machine. Find the path and include it
here
The easiest way to find this path is to click on the jar file in Netbeans
Then in your properties window click on the elipses button next to Source Root.
copy and paste this path into the VM Options: text box.
2) In your file that has the main method insert the following line of code. This will bootstrap the
Spring container using the beans.xml
file, which allows you to monitor your normal classes, which
were not created within the Spring Container.
package ey.io;
mf.addFilter();
}
}
3) create an beans.xml file in the same package as your class that contains your main method.
So, I created a beans.xml file in the ey.io package. your file should look like the one below.
4) Create a new package named META-INF and create another XML file within this package and
name it aop.xml. The contents of the file are shown below.
III) The <aspect name="ey.logging.MyLoggerDemo"/> statement is the fully qualified class name
of your Aspect.
Example: package.sub.ClassName
<aspects>
<!-- weave in just this aspect -->
<aspect name="ey.logging.MyLoggerDemo"/>
</aspects>
</aspectj>
5) Create the aspect in the same directory as listed in the aop.xml file
package ey.logging;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public
class MyLoggerDemo {
/**
* This is the method which I would like to execute
* after a selected method execution.
*/
@After("addFilter()")
public void afterAdvice() {
}
}
6) Create a class named MyFilter in ey.staxfilter. The contents are shown below.
package ey.staxfilter;
public class MyFilter {
public void addFilter() {
System.out.println("This is my filter...");
}
}
That is it! Run the application and you should see the following output:
Posted by
Sean Williams
at
5:06 PM
3 comments:
Karima Kaddouri December 9, 2012 at 8:57 AM
What if we don't have a main class ? what if we
are using struts framework with the MVC paradigm
and want to add the logging functionality
using AOP and Spring ?
Reply
Comment as:
Publish
Notify me
Preview
Newer Post
Home
Subscribe to:
Post Comments (Atom)