2

I want to have all my DAO methods to return an empty collection instead of null. How can I do this with AOP spring?

1 Answer 1

6

This should work:

@Aspect
@Service
public class DaoAspect {

    @Around("execution(java.util.List com.example.*Dao.get*())")
    public Object aroundGetDaoMethods(ProceedingJoinPoint joinPoint) throws Throwable {
        final Object retVal = joinPoint.proceed();
        return retVal != null ? retVal : Collections.emptyList();
    }

}

Adjust pointcut to match only methods you wish to intercept. Also you need to add AspectJ JARs:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>3.1.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.6.6</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.6.6</version>
</dependency>

and enable CLASSPATH scanning:

<aop:aspectj-autoproxy/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.