1

データベース上で操作されているメソッドをハイジャックしたい。このために、Spring AOP を使用したいと思います。しかし、オブジェクトで @autowired depedency を使用しなかった場合、NullPointerException が発生します。この @autowired 依存関係を使用すると、AOP が起動しません。

これが私のオブジェクトの初期化です。

ApplicationContext context1 = new ClassPathXmlApplicationContext("Spring-Customer.xml");
@Autowired
EmployeeDAO dao = (EmployeeDAO) context1.getBean("employeeDAO");

この dao オブジェクトは @Transactional メソッドで使用されます

@Transactional
    public void save(Employee employee) {
        dao.persist(employee);
    }

そして、私はこのようにビームを初期化しました:

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

    <aop:aspectj-autoproxy proxy-target-class="true" />

    <bean id="employeeDAO" class="com.timesheetz.employee.integration.EmployeeDAO " />

    <!-- Aspect -->
    <bean id="logAspect" class="com.timesheetz.tools.service.LoggerAspect" />

</beans>

メソッドをハイジャックする LoggerAspect クラスは次のとおりです。

 package com.timesheetz.tools.service;

import java.util.Arrays;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggerAspect
{
    private Log log = LogFactory.getLog(this.getClass());

    @Pointcut("execution(* *.*(..))")
    protected void loggingOperation() {}

    @Before("loggingOperation()")
    @Order(1)
    public void logJoinPoint(JoinPoint joinPoint)
    {
        System.out.println("Join point kind : " + joinPoint.getKind());
        System.out.println("Signature declaring type : "+ joinPoint.getSignature().getDeclaringTypeName());
        System.out.println("Signature name : " + joinPoint.getSignature().getName());
        System.out.println("Arguments : " + Arrays.toString(joinPoint.getArgs()));
        System.out.println("Target class : "+ joinPoint.getTarget().getClass().getName());
        System.out.println("This class : " + joinPoint.getThis().getClass().getName());
    }

    @AfterReturning(pointcut="loggingOperation()", returning = "result")
    @Order(2)
    public void logAfter(JoinPoint joinPoint, Object result)
    {
        System.out.println("Exiting from Method :"+joinPoint.getSignature().getName());
        System.out.println("Return value :"+result);
    }

    @AfterThrowing(pointcut="execution(* *.*(..))", throwing = "e")
    @Order(3)
    public void logAfterThrowing(JoinPoint joinPoint, Throwable e)
    {
        System.out.println("An exception has been thrown in "+ joinPoint.getSignature().getName() + "()");
        System.out.println("Cause :"+e.getCause());
    }

    @Around("execution(* *.*(..))")
    @Order(4)
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable
    {
        System.out.println("The method " + joinPoint.getSignature().getName()+ "() begins with " + Arrays.toString(joinPoint.getArgs()));
        try
        {
            Object result = joinPoint.proceed();
            System.out.println("The method " + joinPoint.getSignature().getName()+ "() ends with " + result);
            return result;
        } catch (IllegalArgumentException e)
        {
            System.out.println("Illegal argument "+ Arrays.toString(joinPoint.getArgs()) + " in "+ joinPoint.getSignature().getName() + "()");
            throw e;
        }        
    }

}

そして、pom.xmlに次のタグを挿入しました

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.0.5</version>
</dependency>

 <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.0.5</version>
</dependency>

<!-- Spring AOP + AspectJ -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>3.0.5</version>
</dependency>

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.6.11</version>
</dependency>

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.6.11</version>
</dependency>

私はSpring 3.0.5バージョンを使用しています

4

0 に答える 0