切入点不适用于 Spring AOP
为了使用 Spring AOP 实现日志记录,我遵循了以下简单的步骤。 但它似乎不起作用。 任何帮助都会有用
1) 创建了 MyLoggingAspect 类
import org.aspectj.lang.ProceedingJoinPoint;
public class MyLoggingAspect
{
public MyLoggingAspect() {
super();
System.out.println("Instantiated MyLoggingAspect");
}
public Object log(ProceedingJoinPoint call) throws Throwable
{
System.out.println("from logging aspect: entering method [" + call.toShortString()
+"] with param:"+call.getArgs()[0] );
Object point = call.proceed();
System.out.println("from logging aspect: exiting method [" + call.toShortString()
+ "with return as:" +point);
return point;
}
}
2) 创建了一个我想要记录的类 (TixServiceImpl)
public class TixServiceImpl implements TixService{
@Override
public void calculateSomething() {
String s = "did some calculation..";
System.out.println(s);
}
@Override
public String getTixName() {
return null;
}
}
3) 创建了 spring-aspectj.xml< /strong> 文件
<beans...
<bean id="LoggingAspect" class = "MyLoggingAspect"/>
<aop:config>
<aop:aspect ref="LoggingAspect">
<aop:pointcut id="myCutLogging"
expression="execution(* TixService*.*(..))"/>
<aop:around pointcut-ref="myCutLogging" method="log"/>
</aop:aspect>
</aop:config>
</beans>
4) 创建了一个简单的测试客户端 (TixClient)
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class TixClient {
public static void main(String[] a){
ApplicationContext context = new FileSystemXmlApplicationContext("conf/spring-aspectj.xml");
TixService tix = new TixServiceImpl();
tix.calculateSomething();
String s = tix.getTixName();
System.out.println("End of the the client invocation!!");
}
}
5) 它给了我以下输出
...
Instantiated MyLoggingAspect
did some calculation..
End of the the client invocation!!
In order to implement Logging using Spring AOP I followed these simple steps. But it seems like its not working. Any help would be useful
1) Created MyLoggingAspect class
import org.aspectj.lang.ProceedingJoinPoint;
public class MyLoggingAspect
{
public MyLoggingAspect() {
super();
System.out.println("Instantiated MyLoggingAspect");
}
public Object log(ProceedingJoinPoint call) throws Throwable
{
System.out.println("from logging aspect: entering method [" + call.toShortString()
+"] with param:"+call.getArgs()[0] );
Object point = call.proceed();
System.out.println("from logging aspect: exiting method [" + call.toShortString()
+ "with return as:" +point);
return point;
}
}
2) Created a class (TixServiceImpl) where I want logging
public class TixServiceImpl implements TixService{
@Override
public void calculateSomething() {
String s = "did some calculation..";
System.out.println(s);
}
@Override
public String getTixName() {
return null;
}
}
3) Created a spring-aspectj.xml file
<beans...
<bean id="LoggingAspect" class = "MyLoggingAspect"/>
<aop:config>
<aop:aspect ref="LoggingAspect">
<aop:pointcut id="myCutLogging"
expression="execution(* TixService*.*(..))"/>
<aop:around pointcut-ref="myCutLogging" method="log"/>
</aop:aspect>
</aop:config>
</beans>
4) Created a simple test client (TixClient)
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class TixClient {
public static void main(String[] a){
ApplicationContext context = new FileSystemXmlApplicationContext("conf/spring-aspectj.xml");
TixService tix = new TixServiceImpl();
tix.calculateSomething();
String s = tix.getTixName();
System.out.println("End of the the client invocation!!");
}
}
5) It gives me the following Output
...
Instantiated MyLoggingAspect
did some calculation..
End of the the client invocation!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我只是在检查您的代码,但我有预感问题是您没有从 Spring 获取
TixServiceImpl
实例,而是您自己在TixClient 中手动实例化它
。 我认为您的TixService
需要是一个从 Spring ApplicationContext 获取的 Spring bean,以便 Spring 有机会在返回的实例上设置方面。I'm just inspecting your code, but I have a hunch the problem is that you're not getting your
TixServiceImpl
instance from Spring, but rather you're manually instantiating it yourself in yourTixClient
. I'm thinking yourTixService
needs to be a Spring bean, gotten from the Spring ApplicationContext, so that Spring has a chance to set up the aspects on the returned instance.Scott Bale 是对的:让 spring 为您实例化 TixServiceImpl。 同样在这种情况下,启用 Springs 日志记录可能会有所帮助,因为它会告诉您找到了一个方面/通知的目标数量。
Scott Bale is right: Let spring instatiate the TixServiceImpl for you. Also in cases like this, enabling Springs logging might help because it tells you how many targets for a aspect/avice were found.