2017-09-25 118 views
0

我有以下實現:彈簧引導啓動的AOP不會指點我的方面

public interface BusinessResource { 

    @RequiresAuthorization 
    public ResponseEnvelope getResource(ParamObj param); 
} 

@Component 
public class BusinessResourceImpl implements BusinessResource { 

    @Autowired 
    public Response getResource(ParamObj param) { 
     return Response.ok().build(); 
    } 
} 

import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.Around; 
import org.aspectj.lang.annotation.Aspect; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 

@Aspect 
@Component 
public class AuthorizerAspect { 

    protected static final Logger LOGGER = 
         LoggerFactory.getLogger(AuthorizerAspect.class); 

    @Autowired 
    public AuthorizerAspect() { 
     LOGGER.info("Break point works here..." + 
     "so spring is creating the aspect as a component..."); 
    } 

    @Around(value="@annotation(annotation)") 
    public Object intercept(ProceedingJoinPoint jp, 
          RequiresAuthorization annotation) throws Throwable { 
     LOGGER.info("BEGIN"); 
     jp.proceed(); 
     LOGGER.info("END"); 
    } 
} 

的maven的依賴關係可以用spring-boot-starter-aop依賴關係年。那麼,什麼情況是,如果@RequiresAuthorization是在BusinessResource接口聲明的方法使用AuthorizerAspect不會對周圍的getResource方法攔截,但如果我改變現在執行標註同樣的方法在BusinessResourceImpl類,方面將發生。

注意:通過接口級別的註釋,代理甚至不會創建,而註釋放置在實現級別中將爲該資源創建代理。

問題是:有沒有一種方法可以建議對象的註釋只出現在界面上?

回答

0

可能這種替代是爲那些誰喜歡我有用發現沒有直接的方法來進行排序,通過代理在Spring AOP的這種限制:

public interface BusinessResource { 

    @RequiresAuthorization 
    public ResponseEnvelope getResource(ParamObj param); 
} 

而且

@Component 
public class BusinessResourceImpl implements BusinessResource { 

    @Autowired 
    public Response getResource(ParamObj param) { 
     return Response.ok().build(); 
    } 
} 

而且

import import org.aopalliance.intercept.MethodInvocation; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Configuration; 

@Configuration 
public class AuthorizerAspect { 

    protected static final Logger LOGGER = 
         LoggerFactory.getLogger(AuthorizerAspect.class); 

    @Autowired 
    public AuthorizerAspect() { 
     LOGGER.info("Break point works here..." + 
     "so spring is creating the aspect as a component..."); 
    } 

    public Object invoke(MethodInvocation invocation) throws Throwable { 
     LOGGER.info("BEGIN"); 
     invocation.proceed(); 
     LOGGER.info("END"); 
    } 

    @Bean 
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() { 
     return new DefaultAdvisorAutoProxyCreator(); 
    } 

    @Bean("requiresAuthorizationPointcut") 
    public AbstractPointcutAdvisor createPointcut() { 
     return new AbstractPointcutAdvisor() { 

      private static final long serialVersionUID = 4733447191475535406L; 

      @Override 
      public Advice getAdvice() { 
       return AuthorizerAspect.this; 
      } 

      @Override 
      public Pointcut getPointcut() { 
       return new StaticMethodMatcherPointcut() { 
        @Override 
        public boolean matches(Method method, Class<?> targetClass) { 
         if (method.isAnnotationPresent(RequiresAuthorization.class)) { 
          return true; 
         } 
         if (method.getDeclaringClass().isInterface()) { 
          String methodName = method.getName(); 
          try { 
           Method targetMethod = targetClass.getMethod(methodName, method.getParameterTypes()); 
           return targetMethod != null && targetMethod.isAnnotationPresent(RequiresAuthorization.class); 
          } catch (NoSuchMethodException | 
            SecurityException e) { 
           LOGGER.debug("FAILURE LOG HERE", 
              e.getMessage()); 
           return false; 
          } 
         } 
         return method.isAnnotationPresent(RequiresAuthorization.class); 
        } 
       }; 
      } 
     }; 
    } 
} 

所以你會注意到,我們使用方法攔截器對它進行排序。

+0

請確保在提交答案前沒有問題。 invoke方法返回的地方在哪裏?以及Advice類的包是什麼? –