【问题标题】:logging with AOP in spring?在春季使用 AOP 进行日志记录?
【发布时间】:2013-03-22 17:14:15
【问题描述】:

我是办公室里的新人。所以没有对我的指导。

我需要使用log4j 来实现使用AOP 的日志记录。

我在基本的spring MVC 示例中实现了没有AOP 的日志记录?

还在AOP 中使用aspectJ 没有记录的小样本(刚刚制作了Sysout)?

不知道怎么集成?

谁能给我一个创业想法?

肯定会感谢好的答案...

【问题讨论】:

标签: java spring spring-mvc log4j aspectj


【解决方案1】:

您需要执行几个步骤来集成 Aspectj:

  1. Install AspectJ
  2. 将 aop.xml 添加到项目中的 META-INF\aop.xml
  3. 在您的项目类路径中添加 aspectjrt-x.x.0.jar 和 aspectjweaver-x.x.0.jar
  4. 将 -javaagent:/path to aspectj installation/aspectjweaver-1.7.0.jar 添加到服务器的 JVM。

这是一个示例 aop.xml:

<aspectj>
 <aspects>
  <aspect name="test.MySimpleLoggerAspect" />
 </aspects>
 <weaver>
  <include within="test.myproject.*" />
 </weaver>     
</aspectj>

如果您已经在使用 Spring,那么最好使用 Spring 来简化您的设置。

【讨论】:

    【解决方案2】:

    Spring 让我们可以很容易地使用 AOP。这是一个简单的日志记录示例:

    @Aspect
    public class MyLogger {
    
        private Logger log = Logger.getLogger(getClass());
    
        @After("execution(* com.example.web.HomeController.*(..))")
        public void log(JoinPoint point) {
            log.info(point.getSignature().getName() + " called...");
        }
    }
    

    然后只需配置您的 applicationContext.xml(或等效项):

        <aop:aspectj-autoproxy>
            <aop:include name="myLogger"/>
        </aop:aspectj-autoproxy>
    
        <bean id="myLogger" class="com.example.aspect.MyLogger"/>
    

    您会在 MyLogger 类中注意到我在方法上方指定了@After。这称为建议,它基本上指定此“日志”方法将在相关方法之后调用。其他选项包括@Before, @Around, @AfterThrowing

    表达式"execution(* com.example.web.HomeController.*(..))" 被称为切入点表达式并指定我们的目标(在本例中为 HomeController 类的所有方法)。

    附: aop 命名空间 (xmlns:aop="http://www.springframework.org/schema/aop") 和模式位置(取决于版本)需要添加到您的 applicationContext.xml 顶部。这是我的设置:

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
    

    【讨论】:

    • 您是否还必须用@Component 注释MyLogger 类?如果您还没有,您还需要 applicationContext.xml 中的 component-scan 元素。
    • @Component 注释用于(以及其他)告诉 Spring 有问题的类应该在容器中管理(即 Spring bean)。当您在应用程序上下文中指定一个 bean(如 MyLogger 类)时,您实际上是在告诉 Spring 管理该类(因此不需要注释),这允许您将类从 3rd 方库放入容器中。
    • 来自documentationYou may register aspect classes as regular beans in your Spring XML configuration, or autodetect them throuch classpath scanning - just like any other Spring-managed bean. However, note that the @Aspect annotation is not sufficient for autodetection in the classpath: For that purpose, you need to add a separate @Component annotation (or alternatively a custom stereotype annotation that qualifies, as per the rules of Spring's component scanner).
    • &lt;aop:include&gt; 可能做同样的事情。
    • 一些严重的限制 - spring 日志记录仅限于 spring bean,唯一支持的连接点是这些 bean 上的公共方法。
    猜你喜欢
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    相关资源
    最近更新 更多