fas*_*ava 2 collections aop spring nullable
我想让所有DAO方法返回一个空集合而不是null.我怎么能用AOP弹簧做到这一点?
这应该工作:
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
调整切入点以仅匹配您希望拦截的方法.您还需要添加AspectJ JAR:
<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>
Run Code Online (Sandbox Code Playgroud)
并启用CLASSPATH扫描:
<aop:aspectj-autoproxy/>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
398 次 |
最近记录: |