切入点不适用于 Spring AOP

发布于 2024-07-12 22:47:43 字数 2446 浏览 4 评论 0原文

为了使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽清风入怀 2024-07-19 22:47:43

我只是在检查您的代码,但我有预感问题是您没有从 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 your TixClient. I'm thinking your TixService 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.

最美不过初阳 2024-07-19 22:47:43

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文