建议Spring AOP中的方法错误 [英] Advise method error in Spring AOP

查看:246
本文介绍了建议Spring AOP中的方法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to run an advise in a Spring AOP program but I keep getting this error:

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,我认为(但我不确定)可能是这个错误背后的原因。

The problem I have is that I have prototype beans which I think (But I'm not certain) might be behind this error.

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

I have my Beans declared as annotations, except for the FXML file controllers which are injected via an AppFactory class:

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

a sample Home.fxml file controller bean gets injected like so:

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

通过注释类声明的类似于例如:

The ones declared by annotating a class look like, for example:

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

    private String name;

    public String getName() {
        return name;
    }

}

Aspect类看起来像:

The Aspect class looks like:

@Aspect
public class SampleAopAspect {

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

FXML文件如下所示:

The FXML file looks like:

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

我怎样才能使建议方法运行,或者说我错了?提前谢谢大家。

How can I make the advice methods to run, or whare am I going wrong? Thank you all in advance.

推荐答案

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

Your prototype beans need to specify a proxyMode, e.g.:

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

Barry

这篇关于建议Spring AOP中的方法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆