如果我@Validated向我的服务的接口/实现添加注释,那么该服务不再是事务性的.Stacktrace显示没有,TransactionInterceptor但我只看到MethodValidationInterceptor.如果我删除@Validated然后我看到TransactionInterceptor并MethodValidationInterceptor消失当然.这些方面是否相互排斥?
@Service
//@Validated <- here is my problem :)
public interface AdminService {
String test(String key, String result);
}
public class AdminServiceImpl implements AdminService, BeanNameAware, ApplicationContextAware {
@Override
@Transactional(transactionManager = "transactionManager")
public String test(String key, String result) {
return "hehe";
}
}
@Configuration
@EnableAspectJAutoProxy(exposeProxy = true)
@EnableTransactionManagement(order = AspectPrecedence.TRANSACTION_MANAGEMENT_ORDER)
public class AppConfiguration {..}
Run Code Online (Sandbox Code Playgroud)
我正在使用 Spring MVC 4.1 向一个安静的 web 服务添加速率限制。
我创建了一个@RateLimited可以应用于控制器方法的注释。Spring AOP 方面会拦截对这些方法的调用,并在请求过多时抛出异常:
@Aspect
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class RateLimitingAspect {
@Autowired
private RateLimitService rateLimitService;
@Before("execution(* com.example..*.*(.., javax.servlet.ServletRequest+, ..)) " +
"&& @annotation(com.example.RateLimited)")
public void wait(JoinPoint jp) throws Throwable {
ServletRequest request =
Arrays
.stream(jp.getArgs())
.filter(Objects::nonNull)
.filter(arg -> ServletRequest.class.isAssignableFrom(arg.getClass()))
.map(ServletRequest.class::cast)
.findFirst()
.get();
String ip = request.getRemoteAddr();
int secondsToWait = rateLimitService.secondsUntilNextAllowedAttempt(ip);
if (secondsToWait > 0) {
throw new TooManyRequestsException(secondsToWait);
}
}
Run Code Online (Sandbox Code Playgroud)
这一切都完美无缺,除非@RateLimited控制器方法的参数标记为@Valid,例如:
@RateLimited
@RequestMapping(method = RequestMethod.POST)
public HttpEntity<?> createAccount( …Run Code Online (Sandbox Code Playgroud)