我想劫持在数据库上操作的方法。为此,我想使用 Spring AOP。但是当我没有在我的对象上使用@autowired 依赖时,它会得到 NullPointerException。如果我使用这个@autowired 依赖,AOP 不会触发。
这是我的对象初始化。
ApplicationContext context1 = new ClassPathXmlApplicationContext("Spring-Customer.xml");
@Autowired
EmployeeDAO dao = (EmployeeDAO) context1.getBean("employeeDAO");
这个 dao obect 将在 @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 版本我有 jars aspectjrt.jar,version-1.6.11 aspectjweaver.jar,version-1.6.11