gpt4 book ai didi

java - Spring AOP 中的 Advise 方法错误

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

我正在尝试在 Spring AOP 程序中运行建议,但我不断收到此错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: .......
Caused by: java.lang.IllegalArgumentException: Can not set .......

我遇到的问题是我有原型(prototype)bean,我认为(但我不确定)可能是这个错误背后的原因。

我将 Bean 声明为注释,但通过 AppFactory 类注入(inject)的 FXML 文件 Controller 除外:

示例 Home.fxml 文件 Controller bean 被注入(inject),如下所示:

@Configuration
public class AppFactory {

@Bean
public HomeController homeController() throws IOException {
return (HomeController) loadController("/Home.fxml");
}

FXMLLoader loader = null;

protected Object loadController(String url) throws IOException {
loader = new FXMLLoader(getClass().getResource(url));
loader.load();
return loader.getController();
}
}

通过注释类来声明的内容如下所示:

@Component
@Scope("prototype")
@Entity
@Table(name = "ENTITY_OBJECT")
public class EntityObject extends RevEntity {

private String name;

public String getName() {
return name;
}

}

Aspect 类如下所示:

@Aspect
public class SampleAopAspect {

@Before("execution(public String getName())")
public void timeUpdataedAdvice() {
System.out.println("Before method ->");
}
}

FXML 文件如下所示:

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<aop:aspectj-autoproxy />

<bean id="sampleAopAspect" class="org.SampleAopAspect" />

<context:annotation-config/>
<context:component-scan base-package="wakiliproject"/>

</beans>

我怎样才能运行建议方法,或者我哪里出了问题?提前谢谢大家。

最佳答案

你的原型(prototype)bean需要指定一个proxyMode,例如:

@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")

巴里

关于java - Spring AOP 中的 Advise 方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27095481/

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