Reputation: 41097
I want to have all my DAO methods to return an empty collection instead of null. How can I do this with AOP spring?
Upvotes: 2
Views: 856
Reputation: 340863
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/>
Upvotes: 6