Spring AOP:只能建议 Context Beans? [英] Spring AOP: Only Context Beans Could be Adviced?

查看:28
本文介绍了Spring AOP:只能建议 Context Beans?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

方面:

@Aspect公共类 LoggerAspect {@Pointcut("执行(* aop.LoggerAspTest.*(..))")私有无效 infoMethods(){}@Before("infoMethods()")public void logBefore(JoinPoint joinPoint) {Logger logger = Logger.getLogger(joinPoint.getTarget().getClass());logger.info("joinPoint 的类型:" + joinPoint.getKind());logger.info("joinPoint 的参数:" + joinPoint.getArgs());logger.info("joinPoint 的源位置:" + joinPoint.getSourceLocation());logger.info("joinPoint 的静态部分:" + joinPoint.getStaticPart());logger.info("joinPoint 的目标类:" + joinPoint.getTarget().getClass());logger.info("joinPoint 的这个:" + joinPoint.getThis());}}

测试类:

@Component公共类 LoggerAspTest {//测试方法公共无效getInfo(){System.out.println("在logger方面测试方法中!!!");}}

类 - 执行者:

public class Main {//主要的公共静态无效主(字符串 [] args){ApplicationContext ctx = new FileSystemXmlApplicationContext("applicationContext.xml");LoggerAspTest aspect = (LoggerAspTest) ctx.getBean("aspectTest");方面.getInfo();}

}

最后 - applicationContext.xml:

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

好吧,一切正常.但是当我更改类执行器 (Main) 以便我不是通过 Spring 的 ApplicationContext.getBean() 创建 LoggerAspTest 时,而是通过 LoggerAspTest aspect = new LoggerAspTest(); 方面什么也不做.>

问题是:切面是否仅适用于由 Spring 的上下文实例化的 bean?".我真的希望方面像全局拦截器"一样工作,知道它们必须继续使用哪些方法......

提前致谢.

解决方案

是的,必须是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.

解决方案

Yes it needs to be spring bean

这篇关于Spring AOP:只能建议 Context Beans?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆