在字段上具有Spring自定义批注的AOP

问题描述 投票:0回答:1

我正在研究字段的自定义注释。切入点不起作用。

@Aspect
@Configurable(preConstruction = true, autowire = Autowire.BY_TYPE)
@Component
public class LocalizationAspect {

@Pointcut("execution(@com.util.Localized java.lang.String com..*.*(..))")
public void isLocalized() {}



@Around("isLocalized()")
public String getLocalized(ProceedingJoinPoint pjp) throws Throwable {
    String originalValue = (String) pjp.proceed();
..Do some operation with the value fetched.
    return value;
}

CustomAnnotationclass

@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Localized {

String lookupName() default "";
}
}

FieldClass

@Entity
@Table(name = "main_elements", schema = "dbo") 
@Configurable(preConstruction = true, autowire = Autowire.BY_TYPE)
public class MainElements implements Serializable {
private static final long serialVersionUID = 1L;
private static Logger logger = Logger.getLogger(MainElements.class);

@Localized
@Column(name = "name")
private String name;

getters & Setters;
}

[我想要的是,当执行的方法与切点表达式匹配并且使用具有自定义字段批注的字段的任何模型对象时,应该执行切点,以便我可以在运行时修改值。

在应用程序上下文中启用了代理。我目前已从Java 7切换到Java 8,并已升级到Spring 4.3.5。它以前在Java 7中工作。PS:没有编译时错误。我已经省略了代码,以提高可读性。

java spring inversion-of-control aop pointcut
1个回答
0
投票

您的切入点与用Localized注释的方法的执行匹配,而不是注释的field的执行。为了拦截字段注释,您需要使用成熟的AspectJ方面,对于Spring AOP,您不能这样做,因为它不支持任何切入点

  • hasfield()
  • get()
  • set()

取决于您想要做什么,您可能需要其中两个。如何在三种不同情况下使用它们,我有answered hereMCVE,即完整的代码示例。 Spring手册告诉您如何从Spring AOP切换到AspectJ with load-time weaving (LTW)

© www.soinside.com 2019 - 2024. All rights reserved.