gpt4 book ai didi

java - 表达式执行 Spring 方面

转载 作者:行者123 更新时间:2023-12-01 12:53:43 27 4
gpt4 key购买 nike

我在使用 spring aop 时遇到问题

(编辑:如果我的方法不是静态的,则代码可以正常工作)

我的包中有这个结构:

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

我想要在哪些方面进行切入

我想将方面切入点放在 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.*.*(..))")

还有更多,但我从未取得任何成就......

编辑:

所有代码和类:

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

转换.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);

非常感谢,一句敬意

最佳答案

您无法使用 Spring AOP 拦截静态方法。 Spring AOP是基于代理的,不像AspectJ有一些更深层次的拦截机制。所有建议都应用于 Spring 管理的类的实例。静态方法“属于”一个类,而不是该类的实例(您调用的是 aaa.bbb.conversor.conversion.Conversion.conversion1() 而不是 conversionInstance.conversion1())。

关于java - 表达式执行 Spring 方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24033419/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com