Vous êtes sur la page 1sur 9

Java Programming: Netbeans 7.

1 and Spring AOP framework

More

Next Blog

Create Blog
Sign In

Java Programming
Tuesday, August 28, 2012

Blog Archive

2015
(2)

Netbeans 7.1 and Spring AOP framework

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

log.info() statements and Logger.getLogger(SomeClass.class) in all of my


classes. I wanted to
use Spring AOP so that I could use one class
as a logger for my entire application and let Spring
do all the hard work of wraping around my classes and monitoring them. Additionally, I did not
want to convert all of my classes to SpringBeans; I wanted AOP to wrap around my existing
classes.

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

Netbeans 7.1 application.

This will cover two things in two separate posts:

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)

1) how to create an aspect with Spring AOP

2) how to install any applications (Netbeans, AspectJ, etc.)

3) downloading the Spring Framework in Netbeans

4) the "why" behind some of the files and statements


Requirements:

1) Netbeans 7.1 (www.netbeans.org)

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

5) I'm using a Windows XP machine.

6) I'm also using jdk1.7 update 5

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

8. Aspect Oriented Programming with Spring


http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/aop.html#aopintroduction

http://javaprogrammingsw.blogspot.in/2012/08/netbeans-71-and-spring-aop-framework.html[10/27/2015 1:02:01 PM]

Packaging a JavaFX application in an


EXE file with...
Netbeans 7.1 and Spring AOP framework

About Me

Sean
Williams

View my complete profile

Java Programming: Netbeans 7.1 and Spring AOP framework

Spring Framework Home


http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/

How to declare an aspect


http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/aop.html#aopataspectj
A) How to get Spring AOP/AspectJ working in your existing application to perform print
statements

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)

Your libraries should look similar to this:

http://javaprogrammingsw.blogspot.in/2012/08/netbeans-71-and-spring-aop-framework.html[10/27/2015 1:02:01 PM]

Java Programming: Netbeans 7.1 and Spring AOP framework

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

http://javaprogrammingsw.blogspot.in/2012/08/netbeans-71-and-spring-aop-framework.html[10/27/2015 1:02:01 PM]

Java Programming: Netbeans 7.1 and Spring AOP framework

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.

http://javaprogrammingsw.blogspot.in/2012/08/netbeans-71-and-spring-aop-framework.html[10/27/2015 1:02:01 PM]

Java Programming: Netbeans 7.1 and Spring AOP framework

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;

public class MyStreamFilter {

public static void main(String[] args) throws Exception {


new ClassPathXmlApplicationContext("beans.xml", MyStreamFilter.class);

MyFilter mf = new MyFilter();

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.

http://javaprogrammingsw.blogspot.in/2012/08/netbeans-71-and-spring-aop-framework.html[10/27/2015 1:02:01 PM]

Java Programming: Netbeans 7.1 and Spring AOP framework

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- this switches on the load-time weaving -->
<context:load-time-weaver/>
</beans>

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.

Here is what is important in this file:

I) The first <include withing="ey.staxfilter.*"> tells the AspectJ


which classes to monitor. So all the
classes in The ey.staxfilter package will be monitored by SpringAOP. In other words, if
my Aspect
has a pointcut that matches a method in one of these classes within this package, then my Aspect
will write to the log file or the standard output, which ever you configure.

II) The second <include within="ey.logging.*"/> is the location of


my Aspect, which we will discuss
next. If your Aspect is in the same package as the classes that you want to monitor then you do
not need the second statement.

III) The <aspect name="ey.logging.MyLoggerDemo"/> statement is the fully qualified class name
of your Aspect.

Example: package.sub.ClassName

<!DOCTYPE aspectj PUBLIC


"-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<weaver>
<!-- only weave classes in our application-specific packages -->
<include within="ey.staxfilter.*"/>
<include within="ey.logging.*"/>
</weaver>

http://javaprogrammingsw.blogspot.in/2012/08/netbeans-71-and-spring-aop-framework.html[10/27/2015 1:02:01 PM]

Java Programming: Netbeans 7.1 and Spring AOP framework

<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

<aspect name="ey.logging.MyLoggerDemo"/> element. There is nothing special, just create a


new java class file and name it whatever you like. I named my Aspect MyLoggerDemo.java. The
contents of the file are show below. I don't explain how to create pointcuts and advices (please
read the Spring AOP documentation)
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/aop.html#aopataspectj

package ey.logging;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public
class MyLoggerDemo {

/** Following is the definition for a pointcut to select


* all the methods available. So advice will be called
* for all the methods.
*/
@Pointcut("execution(* addFilter(..))")
private void addFilter(){}

/**
* This is the method which I would like to execute
* after a selected method execution.
*/
@After("addFilter()")
public void afterAdvice() {

System.out.println("My Logger is working...");

}
}

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:

http://javaprogrammingsw.blogspot.in/2012/08/netbeans-71-and-spring-aop-framework.html[10/27/2015 1:02:01 PM]

Java Programming: Netbeans 7.1 and Spring AOP framework

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

Alex Mason July 21, 2013 at 12:53 AM


This comment has been removed by the author.
Reply

Alex Mason July 21, 2013 at 12:55 AM


Hi,

Where is the next tutorial to actually do the logging?


Thanks
Reply

Comment as:

Publish

Notify me

Preview

Newer Post

Home

Subscribe to:
Post Comments (Atom)

Simple template. Powered by Blogger.

http://javaprogrammingsw.blogspot.in/2012/08/netbeans-71-and-spring-aop-framework.html[10/27/2015 1:02:01 PM]

Java Programming: Netbeans 7.1 and Spring AOP framework

http://javaprogrammingsw.blogspot.in/2012/08/netbeans-71-and-spring-aop-framework.html[10/27/2015 1:02:01 PM]