I have a doubt in Spring AOP ( aspectj )
Let's say one of the Advice threw unexpected/Unchecked exception while executing some piece of logic ( here After Advice having problem )
@Aspect
public class DataBaseLoggingAspect {
@After("allGetters()")
public void printAfterAspect(JoinPoint joinPoint) throws Exception {
System.out.println("after logger storing in DB ");
Triangle t = (Triangle) joinPoint.getTarget();
t.getName();
// consider below logic is persisting data into DB and throw SQL Exception
throw new SQLException();
}
@Before("allGetters()")
public void printBeforeAspect(JoinPoint joinPoint) throws SQLException {
System.out.println("before logger storing in DB");
throw new SQLException();
}
@Pointcut("execution(public String room.aop.model.*.*())")
public void allGetters() {
}
}
Main Method :
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");
ShapeService service = applicationContext.getBean("shapeService",ShapeService.class);
service.getTraingle().getName();
System.out.println("calling another method");
}
}
Due to exception in @After Advice below error is thrown, but my expectation is to continue the business logic which is to print "calling another method" in the console.
before logger storing in DB
after logger storing in DB
triangle shape
Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at room.aop.model.Triangle$$EnhancerBySpringCGLIB$$460db113.getName(<generated>)
at room.aop.client.Main.main(Main.java:16)
Caused by: java.sql.SQLException
at room.aop.aspects.DataBaseLoggingAspect.printAfterAspect(DataBaseLoggingAspect.java:22)
My thoughts to fix above problem as below, but how this can be handled in Spring :
- Aspects has to run in different thread.
- Run the Aspects in fire and forget principle ( Asynchronous )