Free-Minded
Free-Minded

Reputation: 5420

Spring AOP : Around aspect not working

I have one POJO class on which i have to hijack one of its method with my own logic.

POJO :

CustomerModel

package com.test.model;

public class CustomerModel
{
    public static final String EMAIL = "email";
    public static final String uid = "uid";
    public static final String DEFAULTB2BUNIT = "defaultB2BUnit";

    @Accessor(qualifier = "defaultB2BUnit", type = Accessor.Type.GETTER)
    public B2BUnitModel getDefaultB2BUnit()
    {
        return getPersistenceContext().getPropertyValue(DEFAULTB2BUNIT);
    }

    ...
}

This POJO class is auto generated and i can't change it. I want to use spring AOP feature to hijack getDefaultB2BUnit() method so that i can execute my logic to send correct B2BUnit.

So far i have done this ..

Aspect -

@Aspect
public class AccountSelectionAspect
{

@Resource
private AccountSelectionService accountSelectionService;

@Around("execution(* com.test.model.CustomerModel.getDefaultB2BUnit())")
public Object decideAround(final ProceedingJoinPoint joinPoint) throws Throwable
{
    final B2BUnitModel account = accountSelectionService.getSelectedAccount();

    if (account == null)
    {
        return joinPoint.proceed();
    }

    return account;
}
}

spring.xml -

<aop:aspectj-autoproxy />
<bean class="com.test.core.account.aspect.AccountSelectionAspect"/>

Now this aspect is not calling at all. Whenever there is a call made to fetch defaultB2BUnit, AOP is not hijacking the method.

final CustomerModel customer = currentUser;
B2BUnitModel defaultB2BUnit = customer.getDefaultB2BUnit();

Kindly suggest.

Update -

Can I not be able to use AOP on POJO class? What is the alternative then ?

Upvotes: 1

Views: 2733

Answers (1)

aussie
aussie

Reputation: 119

Spring AOP wont work with classes that are not spring beans. You need to use AspectJ for this. Read more:spring-docs.

Upvotes: 1

Related Questions