我遇到与spring aop相关的问题
(编辑:如果我的方法不是静态的,代码工作正常)
我在包裹中有这个结构:
aaa.bbb.ccc.Clase1.java
aaa.bbb.ddd.Clase2java
我希望whit方面放点切割
我希望将方面切入点放在aaa,这意味着从aaa的任何包或子包中挂起的所有函数都会通过方面切入点
我有这个写,但这段代码没有在日志中写:
@After("execution( ¿Expression?)")
public void logAfter(JoinPoint joinPoint) {
String clasName = joinPoint.getTarget().getClass().getName();
String method = joinPoint.getSignature().getName();
log.info("Empieza la funcion "+clasName +"."+ method);
}
我试过这段代码:
@After("execution(* aaa..*(..))")
@After("execution( aaa..*(..))")
@After("execution(* aaa.*.*(..))")
等等,但我从来没有取得任何成就......
编辑:
所有CODE和类:
spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd">
<context:annotation-config />
<tx:annotation-driven />
<aop:aspectj-autoproxy />
<context:property-placeholder location= "classpath:config/ConversorCfg.properties" />
<bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="org.springframework.util.Log4jConfigurer.initLogging" />
<property name="arguments">
<list>
<value>classpath:log4j.xml</value>
</list>
</property>
</bean>
<!-- Aspect -->
<bean id="logConversor" class="aaa.bbb.util.LogConversor"/>
<bean id="conversion" class="aaa.bbb.conversor.conversion.Conversion"/>
</beans>
LogConversor.java:
package aaa.bbb.util;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LogConversor {
private static Logger log = Logger.getLogger("log");
@After("execution(* aaa.bbb..*(..))")
public void logAfter(JoinPoint joinPoint) {
String clasName = joinPoint.getTarget().getClass().getName();
String method = joinPoint.getSignature().getName();
log.info("Empieza la funcion "+clasName +"."+ method);
}
}
Conversion.java:
package aaa.bbb.conversor.conversion;
import org.springframework.stereotype.Controller;
@Controller
public class Conversion {
public static void conversion1(String ficheroEntrada, String ficheroConfiguracion) {
}
}
Main.java
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Conversion.conversion1(ficheroEntrada, ficheroConfiguracion);
非常感谢, 一个saludate
答案 0 :(得分:1)
您无法使用Spring AOP拦截static
方法。 Spring AOP是基于代理的,与AspectJ不同,它有一些更深层次的拦截机制。所有建议都应用于由Spring管理的类的实例。静态方法&#34;属于&#34;到某个课程,而不是该课程的实例(您致电aaa.bbb.conversor.conversion.Conversion.conversion1()
而不是conversionInstance.conversion1()
)。