Java @在春季AOP中没有接到呼叫
我试图在Spring中使用AOP记录一个方法。我用Java @在春季AOP中没有接到呼叫,java,spring,spring-aop,Java,Spring,Spring Aop,我试图在Spring中使用AOP记录一个方法。我用System.out.println()尝试了以下代码,但没有调用它 创建的注释: @Retention(value = RetentionPolicy.RUNTIME) @Target(value = ElementType.METHOD) public @interface Loggable { } http://www.springframework.org/schema/beans http://
System.out.println()尝试了以下代码,但没有调用它
创建的注释:
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
public @interface Loggable {
}
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>
创建的方面
@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();
}
}
@服务(“RegionService”)
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>
@事务(只读=假)
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>
公共类RegionService实现IRegionService{
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>
@可通行
@凌驾
公共列表获取(){
返回dao.xyz();
}
}
弹簧配置
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>
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"
>
将@Component
添加到MethodLogger
类中。此外,还必须通过以下方式之一启用AspectJ:
- 将
@enableAspectProxy
添加到配置bean类中。(注释驱动方法)
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>
将
添加到应用程序上下文文件。(XML驱动的方法)
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>
您可以发布您的Spring配置吗?另外,服务层是Springbean吗?我看不到aspectj 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>