Spring AOP:只能建议上下文 Bean?

发布于 2024-12-23 00:39:48 字数 2329 浏览 0 评论 0原文

我是 Spring AOP 的新手,我尝试使用方面进行日志记录。这是我的配置:

方面:

@Aspect
public class LoggerAspect {

 @Pointcut("execution(* aop.LoggerAspTest.*(..))")
 private void infoMethods(){}

 @Before("infoMethods()")
 public void logBefore(JoinPoint joinPoint) {
   Logger logger = Logger.getLogger(joinPoint.getTarget().getClass());

    logger.info("joinPoint's kind: " + joinPoint.getKind());
    logger.info("joinPoint's args: " + joinPoint.getArgs());
    logger.info("joinPoint's source location: " + joinPoint.getSourceLocation());
    logger.info("joinPoint's staticPart: " + joinPoint.getStaticPart());
    logger.info("joinPoint's targetClass: " + joinPoint.getTarget().getClass());
    logger.info("joinPoint's this: " + joinPoint.getThis());
 }
}

测试类:

@Component
public class LoggerAspTest {
  // test method
  public void getInfo() {
    System.out.println("in the logger aspect test method!!!");
  }
}

类 - 执行器:

public class Main {
  // main
  public static void main(String[] args) {
    ApplicationContext ctx = new FileSystemXmlApplicationContext("applicationContext.xml");
    LoggerAspTest aspect = (LoggerAspTest) ctx.getBean("aspectTest");
    aspect.getInfo();
 }

}

最后 - applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<aop:aspectj-autoproxy/>
<bean id="aspectTest" class="aop.LoggerAspTest"/>
...

嗯,一切都很完美。 但是,当我更改类执行器(Main)以便我不是通过 Spring 的 ApplicationContext.getBean() 创建 LoggerAspTest,而是通过 LoggerAspTestspect = new LoggerAspTest(); 时,方面什么也不做。

问题是:“方面真的只适用于由 Spring 上下文实例化的 bean 吗?”。我真的希望各个方面能够像“全局拦截器”一样工作,知道它们必须继续执行哪些方法......

提前致谢。

I'm new to Spring AOP and I try to use an aspect for logging. Here is my configuration:

The aspect:

@Aspect
public class LoggerAspect {

 @Pointcut("execution(* aop.LoggerAspTest.*(..))")
 private void infoMethods(){}

 @Before("infoMethods()")
 public void logBefore(JoinPoint joinPoint) {
   Logger logger = Logger.getLogger(joinPoint.getTarget().getClass());

    logger.info("joinPoint's kind: " + joinPoint.getKind());
    logger.info("joinPoint's args: " + joinPoint.getArgs());
    logger.info("joinPoint's source location: " + joinPoint.getSourceLocation());
    logger.info("joinPoint's staticPart: " + joinPoint.getStaticPart());
    logger.info("joinPoint's targetClass: " + joinPoint.getTarget().getClass());
    logger.info("joinPoint's this: " + joinPoint.getThis());
 }
}

The test class:

@Component
public class LoggerAspTest {
  // test method
  public void getInfo() {
    System.out.println("in the logger aspect test method!!!");
  }
}

The class - executor:

public class Main {
  // main
  public static void main(String[] args) {
    ApplicationContext ctx = new FileSystemXmlApplicationContext("applicationContext.xml");
    LoggerAspTest aspect = (LoggerAspTest) ctx.getBean("aspectTest");
    aspect.getInfo();
 }

}

And finally - applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<aop:aspectj-autoproxy/>
<bean id="aspectTest" class="aop.LoggerAspTest"/>
...

Well, everything works perfectly.
But when I change the class-executor (Main) so that I create the LoggerAspTest not by Spring's ApplicationContext.getBean(), but via LoggerAspTest aspect = new LoggerAspTest(); the aspect does nothing.

The question is: "Is it true that aspects work only with beans that were instantiated by Spring's context?". I really expected aspects to work like "global interceptors", that know which methods they must proceed on...

Thank's in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

£冰雨忧蓝° 2024-12-30 00:39:48

是的,它必须是春豆

Yes it needs to be spring bean

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