我正在尝试使用Spring中的AOP记录方法。我只用System.out.println()
尝试了以下代码,但它没有被调用。
创建注释:
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
public @interface Loggable {
}
创建方面
@Aspect
public class MethodLogger {
@Around("execution(* *(..)) && @annotation(Loggable)")
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("this is called");
return result;
}
}
用于登录服务层
中的方法 @Service("RegionService")
@Transactional(readOnly = false)
public class RegionService implements IRegionService{
@Loggable
@Override
public List<> fetch() {
return dao.xyz();
}
}
Spring配置
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
>
<context:component-scan base-package="com.sst.tender.spring"/>
<context:annotation-config/>
<context:spring-configured/>
</beans>
答案 0 :(得分:1)
将@Component
添加到MethodLogger
课程。您还必须通过以下方式之一启用AspectJ:
@EnableAspectJAutoProxy
添加到配置bean类中。 (注释驱动方法)<aop:aspectj-autoproxy />
添加到应用程序上下文文件。 (XML驱动的方法)