0
我正在用sping3編寫一些aop代碼。 這是我的註釋。爲什麼findAnnotation或getAnnotation返回null?
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
String name() default "foo"
}
而且我將pointcut
設置爲上述註釋。
<aop:pointcut id="service" expression="@annotation(com.foo.datasource.DataSource)" />
<aop:advisor advice-ref="dataSourceExchange" pointcut-ref="service" order="1"/>
<bean id="dataSourceExchange" class="com.foo.datasource.DataSourceExchange"/>
我寫了一個服務方法,並添加了上面的註解。在將要在服務之前調用的DataSourceExchange
類中,我嘗試獲取註釋。
class DataSourceExchange implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("Method name : "
+ invocation.getMethod().getName());
System.out.println("Method arguments : "
+ Arrays.toString(invocation.getArguments()));
DataSource dataSource = AnnotationUtils.findAnnotation(invocation.getMethod(), DataSource.class);
System.out.println(dataSource);
我正確地得到了方法的名字。 但註釋dataSource
只是返回null
。 什麼問題?我認爲我所調用的服務方法肯定是註解的,否則它不會觸發pointcut
。