我能够根据我的基本需求获得@annotation切入点.
@Pointcut ("@annotation(path.to.my.CustomAnnotation)")
public void actionAnnotatedPointCut() {}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将它绑定到下面的建议体时,我得到了IllegalArgumentException.
@Pointcut ("@annotation(customAnnotation)")
public void actionAnnotatedPointCut(CustomAnnotation customAnnotation) {}
Run Code Online (Sandbox Code Playgroud)
例外:
Caused by: java.lang.IllegalArgumentException: error at ::0 incompatible number of arguments to pointcut, expected 1 found 0
Run Code Online (Sandbox Code Playgroud)
完整跟踪:
Jan 29, 2014 9:45:29 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Jan 29, 2014 9:45:29 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor': Initialization of bean failed; nested exception is …Run Code Online (Sandbox Code Playgroud) 我有一个字段设置切入点,这似乎按照我的预期进行.其定义如下
before(Object newval): set(@Serviced private * *.*) && args(newval)
以上内容旨在捕获:每当使用@Serviced注释的私有字段属性设置时,请在建议之前调用my.
一切似乎都运行正常,除了我的代码中的一个案例,它通过java反射设置一个与上面匹配的变量(即通过java.lang.reflect.Field.set(....).
任何想法,我怎么能抓住那个"集合"呢?
谢谢
我在Java中使用AspectJ来记录对某些方法的调用.我看过网上但无法找到答案:
当两个@Around建议与方法匹配时会发生什么?
具体来说,我正在使用两个@Around建议,如下所示:
@Around("condition1() && condition2() && condition3()")
public Object around(ProceedingJoinPoint point) {
return around(point, null);
}
@Around("condition1() && condition2() && condition3() && args(request)")
public Object around(ProceedingJoinPoint point, Object request) {
...
result = (Result) point.proceed();
...
}
Run Code Online (Sandbox Code Playgroud)
point.proceed()如果这两个建议都匹配,这会导致被调用两次(实际方法被调用两次)吗?
在执行类的静态方法之前和之后需要完成一些日志记录。我尝试使用 Spring AOP 来实现这一点,但它不起作用,而对于正常方法来说它起作用。请帮助我理解如何实现这一点,如果可以使用注释来完成,那就太好了。
我正在编写一个方面来记录控制器中每个 API 调用的请求和响应。我希望能够在类上使用此注释,因此使用 @Target(ElementType.TYPE)
之前我添加了 @Target(ElementType.Method) 并且我在方法上使用了这个注释并且它工作正常。现在我想将其更改为 @Target(ElementType.TYPE)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ReLogger {}
Run Code Online (Sandbox Code Playgroud)
@Aspect
@Component
public class ReLoggerAspect {
public static final Logger log = LoggerFactory.getLogger("ReLoggerAspect");
@PostConstruct
private void postConstruct() {
log.info("ReLoggerAspect Created");
}
@Around("@annotation(ReLogger)")
private Object reqLoggingAspect(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("Request {}",jointPoint.getArgs()[0);
}
}
Run Code Online (Sandbox Code Playgroud)
在类上使用 @ReLoggerAspect
@RestController
@RequestMapping(value = "....", produces = { "application/json" })
@ReLogger
public class Samplecontroller {
/** Some logic here**/.....
}
Run Code Online (Sandbox Code Playgroud)
调用 API SampleController 时,它不会打印请求
我使用Spring安全性3.2.0和相同版本的Spring框架.Spring安全性在我的项目中运行良好.为了保护我的DAO类(和其他类)中的方法,我想使用以下切入点方法(在spring-security.xml文件中).
<global-method-security>
<protect-pointcut expression="execution(*controller.*.*(..))" access="ROLE_ADMIN"/>
</global-method-security>
Run Code Online (Sandbox Code Playgroud)
我希望指定的切入点表达式能够保护controller包内所有类中的所有方法,并且只能由具有ROLE_ADMIN指定权限的用户访问.
但是当我尝试使用这个表达式时,进程终止时会出现以下保存spring-security.xml文件的异常.
PropertyAccessException 1:org.springframework.beans.MethodInvocationException:属性'pointcutMap'抛出异常; 嵌套异常是java.lang.IllegalArgumentException:切入点格式不正确:在字符位置26执行时期望'名称模式'(控制器.*(..))^
我正在尝试使用3.4.1 元素部分的保护切入点子部分的添加安全性切入点中的参考文档所指定的方法.<global-method-security>
这种情况下正确的表达式语法是什么?
编辑:
使用protect-pointcut添加安全切入点
使用protect-pointcut特别强大,因为它允许您只使用简单的声明将安全性应用于许多bean.请考虑以下示例:
<global-method-security>
<protect-pointcut expression="execution(* com.mycompany.*Service.*(..))" access="ROLE_USER"/>
</global-method-security>
Run Code Online (Sandbox Code Playgroud)
这将保护应用程序上下文中声明的bean上的所有方法,这些bean的类在com.mycompany包中,其类名以"Service"结尾.只有具有该ROLE_USER角色的用户才能调用这些方法.与URL匹配一样,最具体的匹配必须首先出现在切入点列表中,因为将使用第一个匹配表达式.安全注释优先于切入点.
复制并粘贴参考文档中解释的部分(因为有人可能会发现滚动文档很繁琐).