Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execution of Spring AOP Around advice in DAO returns null in service

Tags:

spring

aop

I have written my pointcut and custom around advise, which will execute one Dao method.

Pointcut

@Pointcut("execution(* com.dao.*.get*(..))")
    public void creditPointcut(){
    }

Around Advice

@Around("creditPointcut()") 
    public void around(ProceedingJoinPoint point) throws Throwable{
        LOGGER.info("Method name: "+point.getSignature().getName()+" started");
        point.proceed();
        LOGGER.info("Method name: "+point.getSignature().getName()+" ended");

    }

Target - Dao Method

public Account getAccountDetails(int accntNo) {
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Account where accountNo=:acctNo");
        query.setInteger("acctNo", accntNo);
        Account account = (Account)query.list().get(0);
        return account;
    }

But, this advice returns null account in my service.

Service layer method

@Override
    @Transactional(isolation=Isolation.READ_COMMITTED )
    public Account getAccountDetails(int accntNo) {
        Account account = accountDao.getAccountDetails(accntNo);
        return account;
    }

Please help me why i am getting null account in my service, after executing the around advice in Dao.

like image 835
Sivaranjani D Avatar asked Nov 16 '15 18:11

Sivaranjani D


People also ask

What will after advice do in Spring AOP?

After advice is used in Aspect-Oriented Programming to achieve the cross-cutting. It is an advice type which ensures that an advice runs after the method execution. We use @After annotation to implement the after advice.

Which list of five advice types is correct in Spring AOP?

Run advice after the method execution, regardless of its outcome. Run advice after the method execution, only if the method completes successfully. Run advice after the method execution, only if the method exits by throwing an exception. Run advice before and after the advised method is invoked.

What is around advice in spring?

Around Advice is the strongest advice among all the advice since it runs “around” a matched method execution i.e. before and after the advised method. It can choose whether to proceed to the join point or to bypass join point by returning its own return value or throwing an exception.


Video Answer


1 Answers

That's because you are not returning anything from the advice:

@Around("creditPointcut()") 
public Object around(ProceedingJoinPoint point) throws Throwable{
    LOGGER.info("Method name: "+point.getSignature().getName()+" started");
    Object ret = point.proceed();
    LOGGER.info("Method name: "+point.getSignature().getName()+" ended");
    return ret;

}
like image 125
Ori Dar Avatar answered Oct 16 '22 19:10

Ori Dar