Java - Spring AOP on Eclipse? - java

I need to use AOP of Spring in Eclipse. So I tried to use '#Aspect' annotation, but it does not found it and just suggest me to create myself. As I understand Spring itself does not have this annotation? Because in documentation it shows that is uses aspectj. So I downloaded from http://www.eclipse.org/downloads/download.php?file=/tools/aspectj/aspectj-1.7.4.jar
Then added it as external jar from project properties. But still nothing changed. Did I miss something?
Update:
I also installed AJDT via plugin manager, but still it does not recognize '#Aspect' annotation. So what should I do so it would recognize it?

You are right, you need both the aspectj plugin and the spring aspect plugins.
First, make sure you have the proper maven dependencies in your project. It should be:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${version}</version>
</dependency>
And modify the versions by the ones you would like to use.
Then if you use a maven plugin for Eclipse like m2e it should find the dependencies.
Hope that helps

Related

Role of spring-aspects jar in AOP?

I have following dependencies using in my pom
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${version.spring}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${version.spring}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${version.aspectj}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${version.aspectj}</version>
</dependency>
I ready couple of tutorial on net but no one says to include spring-aspects dependency. I am not sure what role does it way in applying aspects.
Can someone explain why we need spring-aspects ?
Also as per mine understanding spring load time weaving is based on proxy . Javagent does not play any role here. Right ?
It increases Spring AOP functionalities I'd say one of the most common use is to add #Configurable tag that usually is used (but not necessarily )to allow DAO to access the Data Access Layer.
This JAR is a small library of AspectJ that is included in Spring.

adding spring resources to eclipse

I'm struggling with adding spring framework libaries to my eclipse (with maven plug-in m2eclipse)
How can I achieve it in easiest way and why is it so complicated for a newbe user?
It's really frustrating that I can't move on with thing that simple like this.
Main goal is add spring libaries to my pom.xml file in depedencies tab in my dynamic web project in eclipse. Pom.xml is generated thanks to maven plug-in.
First of all I moved to the Eclipse Marketplace and installed Spring Tool Suite for Eclipse Kepler 4.3 and the result is nothing - still can't add libaries. Second attempt was installing the same suite for my whole windows, nothing worked so far.
Sample screenshot (all I can add is this):
Where is spring-web, spring-context, spring-webmvc etc. ? For me it's night and I don't have fresh eye on it but what am i missing here?
I have been using the Spring Framework and Java within Eclipse for a while now. And to be completely honest, the UI for pom.xml completely sucks. Just avoid the Eclipse UI for Maven and manipulate the raw XML. It is very intuitive and powerful.
So if you want to add a dependency, start using mvnrepository. From there you can get all the dependency snippets you need.
For 'spring-web' insert
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
In the <dependencies></dependencies> section. And you are set.
The same can be done for context and webmvc.
Just for convenience here is spring-webmvc:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
and here is spring-context
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>

Some doubts about Spring AOP example

I am leargnin AOP in Spring Framework studying on this tutorial: http://www.tutorialspoint.com/spring/schema_based_aop_appoach.htm
Differently from the previous tutorial I am not adding manually the needed jars file but I am using Maven.
Initially I have added this dependencies in my pom.xml (in addition to those relating spring-core, spring-bean, spring-context, spring-context-support Spring modules)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
But, in this way don't work and raises me the following exception:
Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
Reading online I have found the solution: I have to add these two dependencies in my pom.xml:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
So now I have two doubt:
Why I have to add this org.aspectj.aspectjtools dependencies if I have yet org.springframework.spring-aop? (Also...I noticed that I could delete the org.springframework.spring-aop, this is not used)
What is the difference between them?
Why I have to add cglib dependecies?
I know that I have to use cglib when I use annotations like #Configuration and #Bean...but why I need this dependencies in this case that have not these annotation?
Tnx
Andrea
You could have used aopalliance instead of cglib, and aspectjrt & aspectjweaver dependencies in your pom.xml. What I use and suggest is compile team weaving of your target code using Maven aspectj-maven-plugin.
As to your questions, Spring AOP does not itself instrument your target code. It uses AspectJ behind the scenes to do that. CGLib is used to generate Dynamic Proxies etc. If you need to dynamically generate an interface and then apply it to your advices then, CGLib can assist. Hibernate heavily uses CGLib

Java EE Provided dependencies in Hudson / Jenkins

I'm trying to build a Maven based Java EE project on Jenkins, but I'm getting compilation errors. The reason seems to be that the Java EE dependencies that are marked as provided in the POM logically enough aren't downloaded when the project is built.
How can I set up the POM so that the build works in Jenkins, but the EE dependencies aren't included in the WAR file?
My thanks in advance for any input you can provide.
That's strange, AFAIK the dependencies with scope "provided" are simply not placed in the built file, they should however be downloaded. Are you sure your Maven is correctly configured to download dependencies - maybe there's a proxy that's not configured.
Not sure if its the best solution, but you can add EE dependencies with scope "provided", like the example:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.27</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
Maybe there is a plugin who provides all of them to you, but I'm not sure about that.
Hope that helps

Maven error when resolving dependency

I am new to Maven and am trying to set up one of my first POMs. My application will cache using EhCache. Going to Maven Central Repo (link here) I copy-n-pasted the <dependency> tag and copy it into my pom.xml like so:
...many dependencies above this point
<dependency>
<scope>compile</scope>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<scope>compile</scope>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<scope>compile</scope>
<groupId>jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>3.5.3</version>
</dependency>
...many dependencies below this point
When I save the changes, Eclipse builds the workspace and gives me an error on the opening <dependency> tag for EhCache 2.5:
Missing artifact net.sf.ehcache:ehcache:jar:2.5.0
So I figured that perhaps v.2.5.0 has something wrong with it, and repeated the same for 2.4.7 (the last 2.4.x release before 2.5.0). Same deal.
Since I'm so new to Maven, I don't even know where to begin looking. I tried Project >> Clean and even restarted Eclipse to see if it was just a typical Eclipse "quirk". Nope.
I am thinking:
Could EhCache be publishing bad JARs to the Maven repo?
Could Maven Repo have something wrong with it?
Could this be due to something else configured wrong in my pom.xml?
Could this be a "JAR hell" issue where I have a conflict somewhere on my dependency graph?
How would SO start tackling this problem? Thanks in advance!
It is usually safer to refer to search.maven.org. Dependency from there:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.5.0</version>
<type>pom</type>
</dependency>
Mind type pom. From module's pom:
This is a pom artifact to pull in ehcache-core and ehcache-terracotta
for clustering. Make sure to set 'type' to 'pom' in your dependency.
Aparently when someone does not need terracotta, ehcache-core will do perfectly fine as other answer states.
They use ehcache-core in the official documentation. Maven Central does not have a jar artifact for ehcache 2.5 which explains your error message.
Using ehcache-core changes the dependency to:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.5.0</version>
</dependency>
Which successfully downloads on my machine (ehcache does not).
I dropped this into IntelliJ and it found it. I suspect there is something wrong with your settings. Try creating a project with only this dependency in it. If it fails to download, I would check your setting. e.g. .m2/settings.xml Are you using a Nexus server or maven proxy/cache?
BTW: A simpler way to search for JARs is to use http://mvnrepository.com/ which will find all the available versions and show you the XML you need to add.

Categories

Resources