java方面在春季没有被调用
当调用MyBean的save方法时,不会调用aspect
我的豆子。爪哇
package com.crm.web.beans;
public class MyBean {
public String save() {
System.out.pringln("Save is called");
}
}
AppConfig。爪哇
@Configuration
@EnableAspectJAutoProxy
public class AppConfig {
@Bean
public MyLogger myAspect() {
return new MyLogger();
}
}
麦洛格。爪哇
@Configuration
@Aspect
public class MyLogger {
private Logger log = Logger.getAnonymousLogger();
@Around("execution(* com.crm.web.beans.*(..))")
public void log(JoinPoint point) {
System.out.println("This is calledddddddddd");
log.info(point.getSignature().getName() + " called...");
}
}
应用程序上下文。xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<context:annotation-config />
<context:spring-configured />
</beans>
Iam仅调用save方法“save is called”会被打印出来。 提前谢谢
# 1 楼答案
你不是
aspectjweaver.jar
和aspectjrt.jar
(@seeSpring Aspectj documentation)或MyBean
本身内李># 2 楼答案
好吧,我想我成功了,试试这个:
然后:
这样可能会出现“无法解决的循环引用”错误,我认为这是因为它试图记录logger类。你可以修改你的Around表达式,不管怎样,C没有一个“*”,改成这样:
还可以查看flob和JavaBond的答案
# 3 楼答案
你的切入点表达式不正确。应该是下面的样子
参见参考文档here(参考点-服务包中定义的任何方法的执行)
还可以将
@Configuration
注释更正为@Component
onMyLogger
,如下所示见解释here。粘贴下面的相关部分
您可以在Spring XML配置中将方面类注册为常规bean,或者通过类路径扫描自动检测它们,就像其他Spring托管bean一样。但是,请注意,@Aspect注释不足以在类路径中自动检测:为此,您需要添加一个单独的@Component注释(或者根据Spring的组件扫描器规则,添加一个符合条件的自定义原型注释)