我想通過完成一個記錄函數執行時間的非常簡單的函數來了解Aspects和自定義註釋。我有以下方面和註釋和註釋函數,但是當我調用註釋函數時,方面代碼不會被調用。我如何將註釋與該方面聯繫起來?如何獲得註釋功能點火?
另外我試着用@Aspect聲明文件的一個方面不是類,並且刪除'@annotation(Benchmark)',並且不試圖傳入註釋,但它似乎永遠不會工作。
@Benchmark譯註:
看點
package net.tia.util.aspects;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import java.text.MessageFormat;
@Component
@Aspect
public class BenchmarkAspect {
private static final Logger logger = Logger.getLogger(BenchmarkAspect.class);
@Around(value = "@annotation(Benchmark)")
public Object benchmarkFunctionExecution(final ProceedingJoinPoint joinPoint, final Benchmark benchmark) throws Throwable {
final long startMillis = System.currentTimeMillis();
Object retVal = null;
try {
System.out.println("Starting timed operation");
retVal = joinPoint.proceed();
return retVal;
} finally {
final long duration = System.currentTimeMillis() - startMillis;
String logMessage = MessageFormat.format("{0} Call to {1} took {2}ms", annotation.description(), joinPoint.getSignature(), duration);
if(annotation.logReturnValue() && (retVal != null)){
logMessage += " Returned: " + retVal.toString();
}
logger.debug(logMessage);
}
}
}
調用上我已經在一個已經存在的@Service bean,同時從端點稱爲聲明的隨機函數。
@Benchmark(logReturnValue = true, description = "Overall Time")
public String dummyFunc() {
log.info("Attempting to call aspect");
{
配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
"
>
<!-- enable custom annotations for benchmarking -->
<aop:aspectj-autoproxy/>
<!-- Annotation-driven non-MVC classes -->
<context:annotation-config/>
<context:component-scan base-package="net.tia.extensions,net.tia.sec" />
<beans profile="ldap">
<bean id="AuthenticationProvider" class="net.tia.authentication.LdapAuthenticationProviderImpl">
<property name="userDetailsService" ref="localUserDetailsService"/>
<property name="authenticator">
<bean class="net.tia.authentication.LdapAuthenticatorImpl">
<property name="pricipalPattern">
<value>tia\#login#</value>
</property>
<property name="securityContextSource">
<bean class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
<constructor-arg value="ldap://127.0.0.1:452/"/>
</bean>
</property>
</bean>
</property>
</bean>
</beans>
<beans profile="altAuth">
<bean id="siteAuthenticationProvider" class="net.tia.authentication.siteAuthenticationProviderImpl">
<property name="userDetailsService" ref="localUserDetailsService"/>
</bean>
<bean id="singleSignOnIdentityDriver" class="net.tia.authentication.siteIdentityDriver"/>
</beans>
</beans>
請告訴我們您的配置和採樣方法調用。 –
啊等待它可能是配置,我還以爲系統已經有它啓用,因爲它有 \t' \t \t \t \t < - - > \t AOP:AspectJ的自動代理>' 但這可能並不適用於我在做什麼。 –
使用' '更新了我的配置,但仍然無效。由於這是在一個大型項目中,我會在配置方面進行一些嘗試,但是對於'@ Around',我的語法是否正確?我看到人們說了很多不同的方式來做這個 –