Java 如何使用Spring调用自定义注释?

Java 如何使用Spring调用自定义注释?,java,spring,annotations,aspectj,Java,Spring,Annotations,Aspectj,我将Spring3.2.11.RELEASE与Java6一起使用。我正在尝试创建一个自定义注释,但在获取处理该注释的逻辑时遇到了问题。我已经创建了这个注释 package org.mainco.subco.myproject.util; … @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Caching { CacheEvict[] evict() default {};

我将Spring3.2.11.RELEASE与Java6一起使用。我正在尝试创建一个自定义注释,但在获取处理该注释的逻辑时遇到了问题。我已经创建了这个注释

package org.mainco.subco.myproject.util;
…
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Caching
{
    CacheEvict[] evict() default {};
    CacheEvict[] evictByPartialKey() default {};
}
然后我创建这个方面来处理逻辑处理

package org.mainco.subco.myproject.util;
…
@Aspect
@Component
public class CachingAspect 
{
    …

    @Before(value = "@annotation(Caching)", argNames = "evict")
    public void doEvictions(JoinPoint joinPoint, Caching caching)
    {
        System.out.println("called my annotation.");
但是当我像这样注释我的一个方法时

@Repository
@Transactional
public class MyDaoImpl implements MyDao
{
...
@org.mainco.subco.myproject.util.Caching(evict = { @CacheEvict(value="main", key="{'org.mainco.subco.sbjects.repo.ObjectsDao', 'getObjectsStates'}"), 
                                              @CacheEvict(value="main", key="{'org.mainco.subco.sbjects.repo.ObjectsDao', 'getObjectDocument', #document.id}")},
                                       evictByPartialKey = { @CacheEvict(value="main", key="{'org.mainco.subco.sbjects.repo.ObjectsDao', 'getObjectCategories'}")})
public ObjectDocument save(ObjectDocument document)
{
然后调用该方法,我看不到我的注释被调用

m_objectsDao.save(obmDocument);
我已将其包含在我的Spring上下文文件中

<aop:aspectj-autoproxy/>

请改用此通知声明:

@Before(value = "execution(* org.mainco.subco.myproject..*(..)) && @annotation(caching)")
public void doEvictions(JoinPoint joinPoint, Caching caching) {
    System.out.println("annotated method called with caching=" + caching);
}
它将通知连接点限制为具有
@Caching
注释的方法执行连接点,同时将注释的值绑定到通知上下文,以便您可以提取通知方法主体中的注释参数

它将输出以下消息:

annotated method called with caching=@org.mainco.subco.myproject.util.Caching(
evictByPartialKey=[@org.springframework.cache.annotation.CacheEvict(condition=, 
...

如果您也计划拦截非接口方法,请将
更改为
代理目标类的默认值为false,因此它将创建只能代理接口的JDK代理,而不是也可以代理类的基于CGLIB的代理。

使用此建议声明:

@Before(value = "execution(* org.mainco.subco.myproject..*(..)) && @annotation(caching)")
public void doEvictions(JoinPoint joinPoint, Caching caching) {
    System.out.println("annotated method called with caching=" + caching);
}
它将通知连接点限制为具有
@Caching
注释的方法执行连接点,同时将注释的值绑定到通知上下文,以便您可以提取通知方法主体中的注释参数

它将输出以下消息:

annotated method called with caching=@org.mainco.subco.myproject.util.Caching(
evictByPartialKey=[@org.springframework.cache.annotation.CacheEvict(condition=, 
...

如果您也计划拦截非接口方法,请将
更改为
代理目标类的默认值为false,因此它将创建只能代理接口的JDK代理,而不是基于CGLIB的代理,后者也可以代理类。

@Dave我编辑了我的答案,因此它包含一个重要的配置问题。请看一看。您好,我更改了@Before注释以匹配您的注释,但注释仍然没有被调用(“未打印被调用的注释方法”)。我有接口实现的方法的注释,因此我假设我不需要按照您的解释指定”。带注释部分的
save()
方法是由spring管理的bean吗?请编辑并发布该bean的类声明。另外,您在哪里调用
save()
来自?另一个类?同一个类?我发现我没有捕获方面的conext:component扫描,因此除了您的建议之外,现在添加该扫描会产生异常,“java.lang.IllegalArgumentException:error at::0 formal unbound in pointcut”“。根据yoru请求,我还添加了捕获已注释方法的bean定义。@Dave我编辑了我的答案,因此它包含了一个重要的配置问题。请看一看。您好,我更改了@Before注释以匹配您的注释,但注释仍然没有被调用(“未打印被调用的注释方法”)。我有接口实现的方法的注释,因此我假设我不需要按照您的解释指定”。带注释部分的
save()
方法是由spring管理的bean吗?请编辑并发布该bean的类声明。另外,您在哪里调用
save()
来自?另一个类?同一个类?我发现我没有捕获方面的conext:component扫描,因此除了您的建议之外,现在添加该扫描会产生异常,“java.lang.IllegalArgumentException:error at::0 formal unbound in pointcut”“。我还添加了bean定义,该定义捕获了根据yoru请求注释的方法。