如何使用havnate和spring与javafx

时间:2016-04-14 11:24:09

标签: spring hibernate javafx

我正在开发一个JavaFX应用程序,但是在组合JavaFX和Spring时遇到了问题。我在Spring配置中做错了什么是我不明白的。这是我的JavaFX代码: 主:

public class Main extends Application {
private static final SpringFxmlLoader loader =new SpringFxmlLoader();
@Override
public void start(Stage primaryStage) {
    try {

        Parent root=(Parent)loader.load("/com/isims/view/PatientAjt.fxml");
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch(args);
}}

控制器:

@Repository
public class PatientAjtController {
    @FXML
    private TextField id;
    @FXML
    private TextField name;
    @FXML
    private TextField surname;
    @FXML
    private TextField adresse;
    @FXML
    private TextField numtel;

    @Autowired
    IPatientService ipatientservice;
     public IPatientService getIpatientservice() {
            return ipatientservice;
        }

        public void setIpatientservice(IPatientService ipatientservice) {
            this.ipatientservice = ipatientservice;
        }


    // Event Listener on Button.onAction
        @FXML

    public void handleBtnAjt(ActionEvent event) throws IOException {


        Patient p= new Patient();
        p.setId(Integer.parseInt(id.getText()));
        p.setName(name.getText());
        p.setSurname(surname.getText());
        p.setAdresse(adresse.getText());
        p.setNumtel(Integer.parseInt(numtel.getText()));
        getIpatientservice().addPatient(p);
        }
    }

SpringFxmlLoader:

public class SpringFxmlLoader {

 private static final ApplicationContext applicationContext = new AnnotationConfigApplicationContext("SpringXMLConfig.xml");

 public Object load(String url) {
  try (InputStream fxmlStream = SpringFxmlLoader.class
    .getResourceAsStream(url)) {
   System.err.println(SpringFxmlLoader.class
    .getResourceAsStream(url));
   FXMLLoader loader = new FXMLLoader();
   loader.setControllerFactory(new Callback<Class<?>, Object>() {
    @Override
    public Object call(Class<?> clazz) {
     return applicationContext.getBean(clazz);
    }
   });
   return loader.load(fxmlStream);
  } catch (IOException ioException) {
   throw new RuntimeException(ioException);
  }
 }
}

SpringXMLConfig.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:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:flow="http://www.springframework.org/schema/webflow-config"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:osgi="http://www.springframework.org/schema/osgi"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.1.RELEASE.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.1.RELEASE.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.1.RELEASE.xsd
          http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.1.RELEASE.xsd
          http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.1.RELEASE.xsd
          http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.1.RELEASE.xsd
          http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.1.RELEASE.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.1.RELEASE.xsd
">
 <context:component-scan base-package="com.isims.service"/>
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean> 

</beans>

ERROR:

No qualifying bean of type [com.isims.controller.PatientAjtController] is defined

添加: 我试图使用Java配置:

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
//@ImportResource("classpath:/spring-client-security.xml")
@ComponentScan(basePackages = { "com.isims.controller", "com.isims.service" })
public class SpringApplicationConfig {

}

此行错误:

getIpatientservice().addPatient(p);

感谢。

1 个答案:

答案 0 :(得分:0)

不扫描包含控制器的包。试试

<context:component-scan base-package="com.isims.service, com.isims.controller"/>

而不是您当前的context:component-scan元素。

将控制器注释为@Repository也没有意义。将其注释为@Component。另外,如果您要多次加载相同的FXML文件,那么每次加载它时都会有一个新的控制器实例非常重要(否则所有控制器都将引用同一组TextField s等)。因此,您应该将控制器作为原型范围:

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class PatientAjtController {
    @FXML
    private TextField id;
    @FXML
    private TextField name;
    @FXML
    private TextField surname;
    @FXML
    private TextField adresse;
    @FXML
    private TextField numtel;

    // ...
}

或者,不是自动扫描控制器包,而是显式声明控制器bean:

<?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:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:flow="http://www.springframework.org/schema/webflow-config"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:osgi="http://www.springframework.org/schema/osgi"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.1.RELEASE.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.1.RELEASE.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.1.RELEASE.xsd
          http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.1.RELEASE.xsd
          http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.1.RELEASE.xsd
          http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.1.RELEASE.xsd
          http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.1.RELEASE.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.1.RELEASE.xsd
">
 <context:component-scan base-package="com.isims.service"/>
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean> 

    <bean class="com.isims.controller.PatientAjtController" scope="prototype" />

</beans>

并从控制器类中删除注释:

public class PatientAjtController {
    @FXML
    private TextField id;
    @FXML
    private TextField name;
    @FXML
    private TextField surname;
    @FXML
    private TextField adresse;
    @FXML
    private TextField numtel;

    @Autowired
    IPatientService ipatientservice;
    public IPatientService getIpatientservice() {
        return ipatientservice;
    }

    public void setIpatientservice(IPatientService ipatientservice) {
        this.ipatientservice = ipatientservice;
    }

    // etc etc ...
}

自动扫描可能更可取,但这种方法可能至少可以解决问题。