结合javafx和spring,参数不通过?

时间:2014-03-23 08:39:07

标签: java spring javafx-2

我尝试使用spring作为配置来设置javaFx的布局。这导致我下面的代码...

Spring XML文件:

<bean id="test2" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" lazy-init="true">
    <property name="targetObject">
        <!-- <ref bean="test"/> -->

        <bean class="bin.client.xTest01_CallJavaFx">
            <property name="TextVar ">                                              
                <value>Ali 2 baba</value>               
            </property>             
        </bean>

    </property>
    <property name="targetMethod">
        <value>genUI</value>
    </property>
</bean>

javaFx文件:

public class xTest01_CallJavaFx extends Application{    

public String TextVar = new String();

public String getTextVar () {
    return TextVar ;
}
public void setTextVar (String arg) {
    this.TextVar = arg;
}

public void start(Stage stage){     

    System.out.println( textVar ); // Variable can't be detected

    Group root = new Group();
    Scene scene = new Scene(root,200,100);
    //root.getChildren().add(new Text(50,50,"Hello World"));
    root.getChildren().add(new Text(50,50, textVar ) );  // UI generated with 
                                 // empty text field
    //root.getChildren().add(new Text(50,50,textVar));
    stage.setScene(scene);
    stage.show();
}

public void genUI(String[] args){

    System.out.println("Set here : " + textVar ); // able to display value from 
                                    // spring
    launch(args);   
}

public static void main(String[] args) {  // Run the program !
    ApplicationContext context = new FileSystemXmlApplicationContext( "C:/welcome.xml" );
    Class trvProcess;       
    trvProcess = (Class)context.getBean("test2");
}

任何人都知道为什么来自spring的变量注入进入bean但是在使用javafx扩展后可访问?

谢谢..

1 个答案:

答案 0 :(得分:0)

您的设置方法错误:

public void setTextVar (String arg) {
    this.TextVar = TextVar ;
}

应该是

public void setTextVar (String arg) {
    this.TextVar = arg ;
}

更大的问题是launch(...)方法创建了一个类的新实例并调用其start(...)方法。所以,当然,这不是Spring设置字段的实例。如果您想一起使用Spring和JavaFX,我建议您阅读this blog