I created aspectJ class in seperate Maven project:
#Aspect
public class AspectE {
#Pointcut("execution(#EntryPoint * *.*(..))")
public void defineEntryPoint() {
}
#Before("defineEntryPoint()")
public void setThreadName(JoinPoint joinPoint) {
...
}
#After("defineEntryPoint()")
public void removeThreadName(JoinPoint joinPoint) {
...
}
}
Then in second project I annotated several methods and added to pom.xml:
<dependency>
<groupId>first-project</groupId>
<artifactId>first-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.0</version>
</dependency>
But still aspects aren't seen at all. Am I missing some steps? What should I do?
Did you take a look at this?
AspectJ compiler Maven Plugin - Usage
In order to weave correctly your code with your libraries, you should declare them within your dependencies AND within the aspectj weaver:
<dependencies>
<!-- Aspectj lib -->
<dependency>
<groupId>com.my.group</groupId>
<artifactId>my-aspect-lib</artifactId>
<version>1.0</version>
</dependency>
<!-- Other dependencies -->
</dependencies>
<build>
<!-- Specific build configuration -->
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>com.my.group</groupId>
<artifactId>my-aspect-lib</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
</plugin>
<!-- Other plugins configuration -->
</plugins>
</build>
<!-- Other settings -->
You have to weave the aspects with the code. This can be done in 2 ways:
Compile-time weaving, using the AspectJ compiler Maven plugin as Andrei suggested
Load-time weaving (LTW), using an agent or a custom class-loader
Load-time weaving is a bit more versatile, but can be a bit challenging to set up properly. It consumes more CPU during startup (when the weaving happens), and also has a memory footprint.
Compile-time weaving consumes more CPU during the compilation, obviously, but then you don't pay the price on each restart.
I had the same problem ... but after I added this maven repo it's working
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
Related
There are two different projects in which we need to use AspectJ.
Plugin in pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.10</version>
<configuration>
<verbose>true</verbose>
<showWeaveInfo>true</showWeaveInfo>
<source>1.8</source>
<target>1.8</target>
<complianceLevel>1.8</complianceLevel>
<!-- <encoding>UTF-8</encoding> -->
<verbose>false</verbose>
<Xlint>ignore</Xlint>
<outxml>true</outxml>
<forceAjcCompile>true</forceAjcCompile>
<reweavable>false</reweavable>
<!-- this is important: start-->
<sources/>
<weaveDirectories>
<weaveDirectory>${project.build.directory}/classes</weaveDirectory>
</weaveDirectories>
<!-- this is important: end-->
</configuration>
<executions>
<execution>
<!-- The right phase is very important! Compile and weave aspects after all classes compiled by javac -->
<phase>process-classes</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.9</version>
</dependency>
</dependencies>
</plugin>*
Dependencies:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
Exception:
Caused by: org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException: Pointcut expression 'handler(*) && args(e)' contains unsupported pointcut primitive 'handler'
The first project does not contain a Lombok, so the "Build Project" or "Rebuild Project" helps in this situation. But there is weaving during compilation, and everything works out correctly.
But the second project uses Lombok, and the solution with "Build/Rebuild" does not help, because build means weaving at compile time and => AspectJ does not see the functionality of Lombok (For example, getters).
At the same time, a setting has been introduced in the plugin so that weaving works on the spot, and not during compilation: <forceAjcCompile>true</forceAjcCompile> and empty <sources/>.
The combination of AspectJ and Lombok is mentioned in the news documents for frequently asked questions on Lambda power tools:
https://awslabs.github.io/aws-lambda-powertools-java/FAQs/
Poweretools uses aspectj-maven-plugin to compile-time weave (CTW) aspects into the project. In case you want to use Lombok or other compile-time preprocessor for your project, it is required to change aspectj-maven-plugin configuration to enable in-place weaving feature. Otherwise the plugin will ignore changes introduced by Lombok and will use .java files as a source.
To enable in-place weaving feature you need to use following aspectj-maven-plugin configuration:
<configuration>
<forceAjcCompile>true</forceAjcCompile>
<sources/>
<weaveDirectories>
<weaveDirectory>${project.build.directory}/classes</weaveDirectory>
</weaveDirectories>
...
<aspectLibraries>
<aspectLibrary>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-logging</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
A source with the same information, the comments helped a lot:
https://palesz.wordpress.com/2011/12/03/howto-maven-lombok-and-aspectj-together/
The key of the solution is the empty sources tag in the configuration and the forceAjcCompile=true setting.
Please tell me why only "Build" (i.e. weaving during compilation) can help solve this error? What is missing (what is there when starting "build") to start weaving in place? How is it possible to resolve this situation?
The UnsupportedPointcutPrimitiveException message tells you that you seem to be mixing native AspectJ aspects with Spring AOP ones, or maybe you forgot to configure Spring in a way that makes it stop from trying to wire AspectJ aspects redundantly as Spring AOP ones again.
How can I know that without having seen your aspect code or Spring config? Because native AspectJ knows handler() pointcuts, but Spring AOP does not. So it must be Spring picking it up while wiring the application. This problem is completely unrelated to Lombok.
As you might have noticed, I cannot answer more precisely, because your question is so generic and is specifically lacking a sample project or any code, for that matter. If you would please be so kind to post an MCVE on GitHub, I can help you fix your project, in case my general explanation what went wrong is not comprehensive enough for you.
I tried to use #configurable on my project to have #autowired service in object that are outside of spring context or something like that but didn't manage to make it work. The service is always null.
(I compared 4 different tutorials but nothing worked)
So, by despair I tried to download working examples directly to compare them but they didn't worked as well. (but I had to change them a bit, see in "Notes")
Here are the two examples I tried to download but gave me a nullPointerException too :
https://github.com/kenyattaclark/Spring-Configurable-Example
https://www.baeldung.com/spring-inject-bean-into-unmanaged-objects (the link is at the end of the page)
So, are these examples working on your side? Did I miss something really important?
Notes :
I'm using the correto version of java(but I suppose it's unlikely that the problem come from here)
I had to make some changes in the two examples ot make them work so maybe that's why :
for the first one I added a <pluginManagement> in the pom because if I didn't, eclipse was giving me an error "Plugin execution not covered by lifecycle configuration" and if I tried to compile I had the error : "error can't determine superclass of missing type java.lang.Object
when batch building BuildConfig[null] #Files=3 AopXmls=#0
[Xlint:cantFindType]"
for the second one I didn't use <pluginManagement>. But I didn't download the parent folder so I changed that in the pom and also forced the java version to 11 or maven was resetting it to 1.5 and had to update the junit test to junit5.
So there is room for me screwing all this up, but I'm tired of looking everywhere on internet and couldn't even get one working example.
So, if someone know what's wrong or how to make one example of #configurable work please tell me.
I looked at the first example. It uses very old versions of Spring, AspectJ and AspectJ Maven Plugin. I upgraded the POM to use
Spring 5.3.16,
AspectJ 1.9.9 (supports up to Java 18)
AspectJ.dev AspectJ Maven Plugin 1.13.1 (better and more up to date than Mojohaus, also supports Java 18 and can support any more recent version in the future by simply upgrading the aspectjtools plugin dependency, no plugin upgrade necessary).
Please use JDK 11+ on your build system. More recent AspectJ compiler versions need it. You can still compile to Java 8 byte code, though. (But why would you?)
Here is the Maven POM. I also added a separate dependency management section in order to globally manage dependency versions and exclusions. Then I used mvn dependency:analyze and mvn dependency:tree in order to clean up used undeclared and delcared unused dependencies. If your project needs another set of dependencies, you need to adjust it to your needs, of course. I configured it just for what is used in this tiny sample program. In doing so, I also cleaned out a few unused XML name-space and schema declarations. If you need them in your program, just add them again.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.brightdome</groupId>
<artifactId>spring-configurable-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Configurable Sample</name>
<description>
Sample project to show how to work with Spring's #Configurable capability
to inject dependencies into classes not instantiated by Spring.
</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<aspectj.version>1.9.9</aspectj.version>
<spring.version>5.3.16</spring.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>dev.aspectj</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.13.1</version>
<dependencies>
<!-- Override older AspectJ compiler in plugin, use latest one -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
<exclusions>
<!-- Only needed for load-time weaving, not compile-time -->
<exclusion>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<!-- Needed by spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<!-- Needed by compile-time aspects during runtime -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
</dependencies>
</project>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
"
>
<context:spring-configured/>
<context:component-scan base-package="com.brightdome"/>
<context:annotation-config/>
</beans>
package com.brightdome.sample.spring;
import org.springframework.stereotype.Service;
#Service
public class HelloWorldService {
public void sayHello() {
System.out.println("Hello world!");
}
}
package com.brightdome.sample.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
#Configurable
public class HelloWorldClient {
#Autowired
private HelloWorldService service;
public void sayHello() {
// Used injected instance of service
service.sayHello();
}
}
package com.brightdome.sample.spring;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorld {
public static void main(String[] args) {
// Initialize Spring Context
new ClassPathXmlApplicationContext("/META-INF/applicationContext.xml");
// Instantiate class by new'ing it up. i.e., Do not obtain from Spring context
HelloWorldClient client = new HelloWorldClient();
client.sayHello();
}
}
This compiles and runs just fine in both my IDE (IntelliJ IDEA) and with Maven. On the console, it should simply print "Hello world!" and then exit.
I am trying to handle all cross cutting concerns e.g exception, authorization etc. in a project/repo and inject in more than one separate and independent spring-webflux project i.e handling cross-cutting concerns by building a reusable microservice platform.
Can anyone suggest how to accomplish this?
For example:- light4j handle all cross cutting concerns as middleware just need to add as plugin. But it's not compatible with SpringWebflux.
Even using AspectJ we can't use same handlers for different projects until or unless they are under same parent project.
I tried to use load-time weaving feature of AspectJ. I defined aspects in different project and add a plugin of that in current project (in which I want to use) but when exception occurred aspectJ part isn't invoked
Below is my pom.xml for current project
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dummy</groupId>
<artifactId>dummydmanagement</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DummyManagement</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
<dependency>
<groupId>com.jaway.blog.aspectj</groupId>
<artifactId>annotations-element-value-pair-without-main-class</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>/home/mmt8281/codebase/annotations-element-value-pair-without-main-class/target/annotations-element-value-pair-without-main-class-1.0-SNAPSHOT.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Below is pom.xml of aspect project
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jaway.blog.aspectj</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>annotations-element-value-pair-without-main-class</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Attaching gitHub links:-
AspectJ Code
DummyMgmt Code
The problem with "copy & paste programming" is that you use something you do not understand. Maybe it would be better to learn the basics first.
Having said that, there are two ways to solve your problem, given the fact that the aspect module you copied from the blog post is native AspectJ:
Configure all applications which want to use the aspect to also use native AspectJ, either by post-compile binary weaving or by load-time weaving. The Spring manual describes how to use LTW.
Trivially convert the aspect module to a Spring component and make all client projects use Spring AOP.
The latter is what I did, because I suppose that you only want to use the aspect in Spring projects and that you only annotate public methods in Spring components.
The native AspectJ solution would be more powerful, because it would also work with static and protected/private methods, even for non-Spring code and even outside of Spring completely. But why use a cannon if a pistol is sufficient?
I sent you two pull requests:
https://github.com/shas2hwat/AspectjMgmt/pull/1
https://github.com/shas2hwat/DummyManagement/pull/1
What you need to change in the aspect module, is basically this:
Remove AspectJ Maven plugin
Add org.springframework:spring-context in order to be able to add #Component to the aspect class.
I also optimised a few more things, such as update the AspectJ version.
Then, in the application module you need to:
Add org.aspectj:aspectjweaver
Add the aspect module's base package name to the component scan
I also removed the ugly system dependency with the fixed path to the aspect module. If you simply run mvn clean install on the aspect module before you build the application, the library will be found in the local Maven repository. (I think, you need to urgently learn some Maven basics.)
Now when running this application
#SpringBootApplication
#ComponentScan(basePackages = {"com.dummy.dummydmanagement", "com.jayway.blog"})
public class DummyManagementApplication {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(DummyManagementApplication.class, args);
context.getBean(ItemService.class).getAllItemsService();
}
// (...)
}
the console log will be:
YourAspect's aroundAdvice's body is now executed Before yourMethodAround is called.
Exception occured
YourAspect's aroundAdvice's body is now executed After yourMethodAround is called.
Of course, annotating the static main method does not work with Spring AOP, which is why I removed the annotation. That would only work with native AspectJ. Please let me know if you need that, but I think you should keep it simple, because you clearly do not understand the tools you are using.
Update: Because for whatever reason the OP is angry with me now, because I told him to stop texting me directly on Telegram, he also deleted his own repositories. For anyone interested, here are my clones, containing both the original code and my modifications fixing the problem, as described above:
https://github.com/kriegaex/AspectjMgmt, relevant commit here
https://github.com/kriegaex/DummyManagement, relevant commit here
I have one aspect with using aspectJ as below:
public aspect TestAspect {
pointcut classicPointcut(PersistenceManagerImpl object) : execution(manager.PersistenceManagerImpl.new(..)) && target(object);
after(PersistenceManagerImpl object) : classicPointcut(object){
System.err.println(object.getClass().getSimpleName());
}
}
this aspect is in module aspect. this module is packaking as jar. PersistenceManagerImpl is in other module but i need use it in module aspect. For dependency management i use maven. But here is of course problem with cyclic reference. Exists some way how can a resolve this problem ?
----------EDIT----------
I get only this error:
java.lang.NoSuchMethodError:TestAspect.ajc$after$TestAspect$1$cc149106(Ljava/lang/Object;)V
When i move my aspect to same module, when is PersistenceManagerImpl i obtain correct solution(of course). But this is not, what i wanted.
Could you put the error result of the compiling code? You could try to put another module as dependency first then later put the dependency on the aspectj maven plugin at weaveDependency in pom.xml as follow:
....
<dependency>
<groupId>com.maventest</groupId>
<artifactId>mytest</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
....
....
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<showWeaveInfo>true</showWeaveInfo>
<complianceLevel>${maven.compiler.target}</complianceLevel>
<encoding>${project.build.sourceEncoding}</encoding>
<weaveDependencies>
<weaveDependency>
<groupId>com.maventest</groupId>
<artifactId>mytest</artifactId>
</weaveDependency>
</weaveDependencies>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
ps: You could see my question post asking the same thing here
Your aspect seems to be specific for the module in which PersistenceManagerImpl is located, so that module should be a dependency of the aspect module. On the other hand, that module depends on the aspect module because it needs it as an <aspectLibrary> in the AspectJ Maven configuration. Indeed a circular dependency, but an unnecessary one. You have two options:
Move the aspect to the application module which it is specific for because IMO it belongs there if it explicitly uses specific classes from there. Only aspects which implement cross-cutting concerns in a way applicable to multiple modules should be in their own aspect library.
Following the previous thought, you could make your aspect more general, e.g. do something like this:
public aspect TestAspect {
pointcut classicPointcut(Object object) :
execution(*..PersistenceManagerImpl.new(..)) &&
target(object);
after(Object object) : classicPointcut(object){
System.err.println(object.getClass().getSimpleName());
}
}
Java + Spring + Maven application:
Can somebody provide me with the link or advise me on a pure AspectJ implementation without proxy-based Spring AOP?
My application is purely Spring + Maven based. I have currently implemented aspects with Spring AOP which is not solving my problems.
If I try to access a private method2() from public method1() within the same class A this is not supported.
I would like to know :
1) How to write an aspectj with pointcut that supports intraclass method calls?
2) How to configure that into my current Spring,maven project with AspectJ load-time weaving?
3) how to configure AspectJ Maven Plugin for compile-time weaving in Tomcat server + eclipse.
#Controller
class A {
public void method1() {
method2("foo");
}
private String method2(String text) {
return text;
}
}
Expected output:
log.entering(method1)
log.entering(method2)
print abc
log.exiting(method2)
log.exiting(method1)
My recommendation for you is to make a dummy project with Spring Roo (so you can see how the Maven pom.xml and Spring applicationContext.xml file looks) and download Spring STS version of Eclipse which has AspectJ setup correctly.
You can use Maven and AspectJ project together by converting Maven project into AspectJ project by Right Click - Configuration - Convert to AspectJ project. From there you can create Aspect class without annotation or Java class of Aspect using annotation.
As for the result that you want, you can use Around method like below:
#Around("execution ( * A.method1(..))")
public void captureMethodOne(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("log.entering(method1)");
joinPoint.proceed();
System.out.println("log.exiting(method1)");
}
Also do not forget to input aspectj and aspectj maven plugin in the pom.xml, such as
<properties>
<maven.compiler.plugin.version>3.5.1</maven.compiler.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.8</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Hope it helps.
PS Check AspectJ tutorial first, plenty around in the internet.