具有自定义注释的Spring AOP不起作用

时间:2016-03-12 01:35:43

标签: java annotations aspectj spring-aop

我查看了每个example on stack exchange以及spring的示例网站,看起来这一切都应该有效。我一定错过了一些简单的事情

我有一个自定义注释,理想情况下,如果要注释类或者注释任何方法,我想应用于类的所有方法。这是方面,测试和代码:

注释

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target({METHOD})
@Retention(RUNTIME)
public @interface Monitor {
    String value() default "Monitor";
}

方面

@Aspect
@Component
public class LatencyAspect {

    @Autowired
    private Logger logger;

    @Around("@annotation(Monitor)")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object retVal = joinPoint.proceed();

        logger.info("logged");

        return retVal;
    }
}

测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class,classes = {LatencyConfig.class, LatencyTest.ContextConfiguration.class})
@ComponentScan
public class LatencyTest {
    final static Logger log = mock(Logger.class);

    @Autowired
    private SomeClass someClass;


    @Test
    public void testExample() throws Exception {
        someClass.doSomething("foo");
        verify(log).info("logged");

    }

    @EnableAspectJAutoProxy(proxyTargetClass = true)
    @Configuration
    static class ContextConfiguration {


        @Bean
        public SomeClass properties() {
            return new SomeClass();
        }

        @Bean
        public Logger log() {
            return log;
        }
    }


    public static class SomeClass {

        @Monitor
        @Transient
        public String doSomething(String whatever) {
            return "done";
        }
    }
}

结果

Wanted but not invoked:
logger.info("logged");
-> at org.bongiorno.latency.LatencyTest.testExample(LatencyTest.java:74)
Actually, there were zero interactions with this mock.

链接

Actual source

1 个答案:

答案 0 :(得分:1)

Spring没有从JUnit测试类中获取#include <stdio.h> #include <czmq.h> typedef struct { zsock_t *pipe; // Actor command pipe zpoller_t *poller; // Socket poller int terminated; } accountactor_t; typedef struct{ zactor_t *actor; int foo; } account_t; accountactor_t * accountactor_new (zsock_t *pipe, void *args) { accountactor_t *self = (accountactor_t *) zmalloc (sizeof (accountactor_t)); assert (self); self->pipe = pipe; self->poller = zpoller_new (self->pipe, NULL); self->terminated = 0; return self; } static void accountactor_recv_api (accountactor_t *self) { // Get the whole message of the pipe in one go zmsg_t *request = zmsg_recv (self->pipe); if (!request){ return; // Interrupted } char *command = zmsg_popstr (request); if (streq (command, "START")){ zsys_debug("START command received!"); zsock_signal (self->pipe, 0); }else if (streq (command, "STOP")){ zsys_debug("STOP command received!"); zsock_signal (self->pipe, 0); }else if (streq (command, "$TERM")){ zsys_debug("$TERM command received!"); // The $TERM command is send by zactor_destroy() method self->terminated = 1; }else { zsys_error ("Invalid command '%s'", command); zsock_signal (self->pipe, -1); } zmsg_destroy(&request); if(command){ free(command); } } void actor_fcn (zsock_t *pipe, void *args) { accountactor_t * self = accountactor_new (pipe, args); if (!self) return; // Interrupted int rc = 0; // Signal actor successfully initiated zsock_signal (self->pipe, 0); while (!self->terminated) { zsock_t *which = (zsock_t *) zpoller_wait (self->poller, -1); if (which == self->pipe){ accountactor_recv_api (self); } } if(zpoller_terminated(self->poller)){ zsys_debug("Poller Interrupted!"); }else if(zpoller_expired(self->poller)){ zsys_debug("Poller Expired!"); } // Free object itself zpoller_destroy (&self->poller); zsock_destroy(&self->pipe); free(self); self = NULL; } void s_account_free (void *argument) { account_t *account = (account_t *) argument; zstr_send (account->actor, "$TERM"); zactor_destroy(&account->actor); free(account); zsys_debug("Item removed!"); } int main(){ zhash_t *table = zhash_new(); int i = 0; account_t *ptrs[1024]; char key[10]; for(i=0; i<1024; i++){ ptrs[i] = (account_t *) zmalloc (sizeof (account_t)); ptrs[i]->actor = zactor_new (actor_fcn, NULL); sprintf(&key[0],"%d",i); zhash_insert(table, key, (void *)ptrs[i]); zhash_freefn(table, key, s_account_free); zstr_send (ptrs[i]->actor, "START"); zsock_wait (ptrs[i]->actor); zsys_debug("%d actor started!",i); } i = zhash_size(table); // Delete all while(i--){ sprintf(&key[0],"%d",i); zhash_delete(table, key); free(ptrs[i]); } return 0; } 注释。将注释移动到@ComponentScan类或测试本地LatencyConfig内部配置类。