2
我想讓所有DAO方法返回一個空集合而不是null。我怎樣才能用AOP彈簧做到這一點?在春天自動轉換爲空列表爲空
我想讓所有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();
}
}
調整的切入點,以匹配只有你想攔截的方法。還需要添加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>
,使CLASSPATH掃描:
<aop:aspectj-autoproxy/>