0

我有以下层次结构:

@Validated
public class BaseResource 

public class DeviceResource extends BaseResource

@Validated注释如下:

package com.redbend.validation.annotation;

import static java.lang.annotation.ElementType.TYPE;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.inject.Scope;



@Scope
@Target(TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Validated {

}

我有以下建议的Spring Aspect:

    package com.redbend.validation.aspect;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;

import javax.validation.constraints.NotNull;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.redbend.exceptions.EExceptionMsgID;
import com.redbend.exceptions.runtime.MissingMandatoryParameterException;
import com.redbend.validation.annotation.MandatoryOneOfParams;
import com.redbend.validation.annotation.MandatoryParams;
import com.redbend.validation.annotation.NotEmpty;
import com.redbend.validation.annotation.OneOfParamsForValue;
import com.redbend.validation.annotation.OneOfParamsForValueMap;
import com.redbend.validation.annotation.ParamsForValue;
import com.redbend.validation.annotation.ParamsForValueMap;

@Aspect
@Component
@Order(2)


public class ValidationInterceptor {

    private static Logger logger = LoggerFactory.getLogger(ValidationInterceptor.class);

    public ValidationInterceptor() {
        // TODO Auto-generated constructor stub
    }

    @Before("within(com.redbend..*) && @within(com.redbend.validation.annotation.Validated) ")
    public void validate(JoinPoint joinPoint) throws Exception {
        validateParams(joinPoint);
    }

当我在 中调用一个方法时DeviceResource,它不会被方面捕获,甚至认为它继承自带BaseResource有注释的方法@Validated,并@Validated带有注释@Inherited。当我用它
注释时工作正常。如何在不使用注释的情况下使方面拦截我的方法?DeviceResource@ValidatedDeviceResource@Validated

谢谢,

阿米尔

4

2 回答 2

2
within(@com.redbend.validation.annotation.Validated)

不正确,应该是

@within(com.redbend.validation.annotation.Validated)
于 2013-06-04T12:15:01.480 回答
0

我最终通过改变我方面的切入点表达式来解决它:

@Before("within(com.redbend..*) && within(@com.redbend.validation.annotation.Validated *)")

我仍然不知道为什么它以前不起作用或为什么@within(com.redbend.validation.annotation.Validated) 不起作用......

于 2013-06-05T10:50:35.677 回答