弹簧引导启动的AOP不会指点我的方面
问题描述:
我有以下实现:弹簧引导启动的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类,方面将发生。
注意:通过接口级别的注释,代理甚至不会创建,而注释放置在实现级别中将为该资源创建代理。
问题是:有没有一种方法可以建议对象的注释只出现在界面上?
答
可能这种替代是为那些谁喜欢我有用发现没有直接的方法来进行排序,通过代理在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);
}
};
}
};
}
}
所以你会注意到,我们使用方法拦截器对它进行排序。
请确保在提交答案前没有问题。 invoke方法返回的地方在哪里?以及Advice类的包是什么? –