Vaadin with AOP annotation - @Before and @After sections not working

[i]

[/i]Hello,
I have already made tutorial
Aspect Annotation Tutorial

in simple Java Project and it worked fine
. Then I wanted to implement similar code in the same way
in Vaadin Project, but it failed
to execute methods from @Before and @After sections. I made steps as it follows:


1)
configure build-path: add
aspectjrt.jar
and
aspectjweaver.jar
,

2)
add
-javaagent:C:/aspectj1.7/lib/aspectjweaver.jar
to the command line ( Run Configurations… → In left panel – Tomcatv7.0 Server at localhost → Arguments → VM arguments)

3)
create an xml file
aop.xml
, this time in WebContent → META-WEB folder (not: in src/META-WEB folder, as I did in Java Project mentioned above)

As I am a begginer with programming in general, I have made only these"ingenious" observations:

  1. I got a Warning displayed in my aop.xml file (new thing), which says: “No grammar constraints (DTD or XML schema) detected for the document”. It didn’t say so in simple Java Project.
  2. I guess I missed some basic modifications that should have been made as I work with a web application (VAADIN PROJECT) to use AspectJ.

Please, be of some help!

My code:


package com.example.aspectsampler;

import com.vaadin.Application;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

public class AspectsamplerApplication extends Application {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void init() {
		Window mainWindow = new Window("Aspectsampler Application");
		Label label = new Label("Hello Vaadin user");
		mainWindow.addComponent(label);
		setMainWindow(mainWindow);

		System.out.println("Hello World from AspectsamplerApplication class");
	}
}
package com.example.aspectsampler;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class BasicAspect {

	/*
	 * Read as -- do this *before* any *call* to the function
	 * *java.io.PrintStream.println* that takes a *String* and returns *void*
	 * and the function is not called within any class under the package
	 * net.andrewewhite.aspects
	 */
	@Before("   call(void java.io.PrintStream.println(String)) ")
	public void beforePrintlnCall() {
		System.out.println("About to make call to print Hello World");
	}

	@After("    call(void java.io.PrintStream.println(String)) ")
	public void afterPrintlnCall() {
		System.out.println("Just made call to print Hello World");
	}

}

and xml file:

<!-- aop.xml -->
<aspectj>
    <aspects>
        <aspect name="net.andrewewhite.aspects.BasicAspect"/>
    </aspects>
</aspectj>

You’re trying to combine this AOP library with a Java EE container (which provides much of the same functionality already). Is there a reason you’re trying to use aspectj with a Java web application? You should leave Vaadin out of it first and try a simple web app with aspectj to see how they work together. It sounds complicated, but googling around may give you some help, for instance
this page
.

If you’re new to programming, you might want to start with simpler steps, like a Java web app tutorial to learn about Servlets, the web container, and web app lifecycle (though ideally you’d learn a bit about Java first!). The particular use case you’re trying is already built into Java web apps:
servlet filters
. There’s an
official Java EE tutorial
, but if you’re using an IDE I’ve often found that there are great tutorials on the web sites for the IDEs. My only problem with those is that you sometimes miss some steps about how everything fits together, and I’ve seen a million problems along the lines of “it works when I run it in Eclipse, but then fails when I try a different Tomcat server.”

If you want, you can let me know more about your programming background and I can find a couple things for you to try. I have a bunch of blogs about Vaadin and GlassFish (the full EE container, unlike Tomcat which is primarily for the web part of EE), but they may or may not be what you’re looking for.

Cheers,
Bobby

Thank you Bobby a lot for your answer and concern!

The background of my question and my work is to
implement a logging (of application processes) system
in a quite large application (vaadin application!). My idea was to use AspectJ addnotations so as to narrow down and cumulate all the code connected with logging process (I want to use Java Logging API) in a single class, not: implement them separatedly in every single class/method. I have read about aspectJ and liked the idea (as above), and it worked for me in simple Java class, but (as I explained) - it didn’t in web app.

I have some little experience in programming in Java in general (knowing code structure, using it) but I haven’t so far get to know a lot about web apps’
architecture
(for example servlet lifecycle is unfamiliar to me) - I think that is a problem why I cannot fix errors like this one I have now (moving simple Java code from Java application to web application and make it run), but really not have a loooot of time to get familiar with it these days (unless it is really necessary and inevitable for this particular task I have to do :smiley: ).

I still perceive
using aspectJ and Java Logging API
as not such a bad idea, is it???

Thank you once again for your help in advance! :slight_smile:
Marta
Skype marta.karass

In a web application there might be some complications related to class loading etc.
I have used AspectJ with Vaadin a long time ago, but it was partly configured by someone else so I cannot recall what was needed.

If you cannot get the aspects working at all, maybe first try another approach to weaving (e.g. compile time weaving) and try to get everything working. Then check what is needed to get load time weaving working with your application and container.

If you can get it to work, it’s great. :slight_smile: I just think you’re trying to do something pretty complicated, and it may be better to take small steps. A Java web app runs in a servlet container, and that container already handles the “crosscutting” concerns for you. I think the point of aspectJ is to help with these things when you’re
not
running in any kind of container (that’s just my impression).

Instead of trying to mix in an extra Java library, you might be able to use
what’s built into the servlet container
instead and save some effort. You could check out an
ee tutorial like this
(and there are a million books, blogs, etc, too). If you’re trying to run some logging code before/after client requests, for instance, you can do this already without aspectJ. There are all kinds of hooks in the container for code that runs at app startup/shutdown, when requests come in or a component like an EJB is called, etc.

Nothing wrong at all with using Java Logging API – that’s what I rely on!

Cheers,
Bobby

Hello,

I ended up using some simple solutions (leaving the idea of using aspectJ as I lack of experience), enhanced by my own code. It turned out not to “pollute” the code to such an extend I had thought it would :). In all, I came to know some new things so it was not such a waste of time - trying to do that.

Henri and Bobby - thank you A LOT for your help! :smiley:

Marta, that’s awesome to hear!

Cheers,
Bobby

Hi guys,

I have a TestWindow which extends Vaadin Window. In the window, there is a close button.
Without AOP annotation, the window can close properly by clicking the close button.

Now, I need to do some logic before the Window being called and closed.
I am going to use AOP to apply for all Windows in my application.

The following code is my Aspect. Actually, the debug log can be printed out in my log file.
However, when I click the “CLOSE” button in the window, the window can close normally.

@Aspect
public class WindowAdvice {
    private final static Logger log = Logger.getLogger(WindowAdvice.class);
    @Before("execution(* com.ui.collection..*Window.initComponent(..))")
    public void before(JoinPoint joinPoint){
        log.debug("Add the Before Method~");
        //Business Logic
    }
}

I wonder whether it is related to the AOP proxy object being created or not.

According to Vaadin Application Framework, UI Components exists in Vaadin Client Side Engine and Server-side UI Compoenet.
UI Components send the Close Window requests/event to both proxy and server object. However, Server object cannot send back the UI Changes to Client Side.
Could anyone explain to me? And any suggestion to solve the problem.

Thanks so much.