Being new to AspectJ and Spring AOP, I have a Spring AOP project with Aspects defined in it. Pointcut is defined on annotation Loggable
. Whenever a method with @Loggable
is executed the advices should get executed.
Annotation:
@Target(ElementType.METHOD)
@Retention(RUNTIME)
public @interface Loggable {
String customMessage() default "";
String[] properties() default {};
String type();
}
Below is the Aspect class:
LoggingAspect:
@Aspect
@Component
public class LoggingAspect {
private final Logger LOGGER = Logger.getLogger(this.getClass());
@Around("loggablePointcut(loggableAnnotation)")
public Object beforeDebug(ProceedingJoinPoint joinpoint, Loggable loggableAnnotation) throws Throwable {
final String logType = loggableAnnotation.type();
LOGGER.log(Level.toLevel(logType), joinpoint.getSignature().toString() + " - " + loggableAnnotation.customMessage());
System.out.println("in here");
// Default Object that we can use to return to the consumer
Object returnObject = null;
try {
returnObject = joinpoint.proceed();
} catch (Throwable throwable) {
throw throwable;
}
return returnObject;
}
@Pointcut("@annotation(loggableAnnotation)")
public void loggablePointcut(Loggable loggableAnnotation) {
}
}
Now to make the app runnable and testing once locally:
Application.java:
@SpringBootApplication
public class Application {
@Loggable(type = "info")
public void testing() throws NoSuchMethodException, SecurityException {
System.out.println("testing");
}
public static void main(String[] args) throws NoSuchMethodException, SecurityException {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
Application app = (Application) ctx.getBean("application");
app.testing();
System.err.println("Executed from project A");
}
}
Till here everything works fine. Now my actual requirement is to use these aspects in my other spring boot projects. I read this article here, but couldn't seem to understand it.
I am confused with following questions:
Q1. How to export the AspectJ project as a jar in other projects? Do I right click and export it as jar and import the generated jar and everything will work? (I tried this but it didnt work)
Q2. Any other options I can use here?
EDIT:
pom.xml:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>Spring_AOP</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring_AOP</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> </dependency> -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
These are the only files I have.