简体   繁体   中英

Expression Execute Spring aspects

I have a problem relationing with spring aop

(edit:If my method isnt static, the code works fine)

I have this strucuture in packages:

aaa.bbb.ccc.Clase1.java
aaa.bbb.ddd.Clase2java

and i want whit aspects put point cut

and I want to put the aspect pointcut at aaa, meaning that all functions that hang from whatever package or subpackage of aaa pass through the aspect pointcut

I have this writen, but this code doesnt write in the log:

@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);
}

I tried this code:

@After("execution(* aaa..*(..))")
@After("execution( aaa..*(..))")
@After("execution(* aaa.*.*(..))")

and something more, but i never achieved anything...

EDIT:

all CODE and classes:

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);

Thanks a lot, One saludate

You cannot intercept static methods with Spring AOP. Spring AOP is proxy-based, is not like AspectJ where there are some deeper mechanisms for interception. All advices are applied to instances of classes that are managed by Spring. A static method "belongs" to a class, not to an instance of that class (you call aaa.bbb.conversor.conversion.Conversion.conversion1() not conversionInstance.conversion1() ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM