【问题标题】:Advise method error in Spring AOPSpring AOP 中的 Advice 方法错误
【发布时间】:2014-11-23 23:03:10
【问题描述】:

我正在尝试在 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 .......

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

除了通过 AppFactory 类注入的 FXML 文件控制器之外,我将 Bean 声明为注释:

一个示例 Home.fxml 文件控制器 bean 像这样被注入:

@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>

如何使建议方法运行,或者我哪里出错了?提前谢谢大家。

【问题讨论】:

    标签: java spring javafx spring-aop


    【解决方案1】:

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

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

    巴里

    【讨论】:

    • 感谢@blagerweij 的回复。现在我收到错误 Pointcut is not well-formed: expecting ')' at character position!
    • 你在上面问题中的切入点语法是正确的,所以你一定是改变了一些东西。如果您希望我们帮助您发现语法错误,与我们分享代码如何?
    • 您在 SampleAopAspect 中使用了内联切入点,应该没问题。您确定提供的 sn-ps 复制正确吗?例如,“EntityObject”类既是“组件”又是“实体”,这是非常不寻常的。顺便说一句,如果您正在寻找一种方法来填充最后修改的用户名和时间戳,您可能需要查看 spring-data AuditorAware:&lt;jpa:auditing auditor-aware-ref="auditorAware" /&gt; 请参阅 [projects.spring.io/spring-data-jpa/]
    • 谢谢@blagerweij。效果很好!我有一个原型@Component 我没有更改为@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype") 就离开了。做了你的建议。现在我很好。再次感谢>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多