在Spring中实现GUI中的服务

时间:2014-07-03 10:59:54

标签: java spring autowired spring-annotations

我的问题是我的GUI中有NullPointerException。 null的对象应该被Spring取代。请参阅我的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                        http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <context:annotation-config />

    <context:component-scan base-package="com.Person"
        annotation-config="true" />

    <tx:annotation-driven transaction-manager="transactionManager"
        proxy-target-class="true" />

    <bean id="mainGUI" class="com.Person.MainWindow" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
        <property name="url" value="jdbc:hsqldb:hsql://localhost/testDB" />
        <property name="username" value="sa" />
        <property name="password" value="" />
        <property name="initialSize" value="5" />
        <property name="maxActive" value="10" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="jpaData" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="entityManager"
        class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

</beans>

我收到错误此处List<Person> test = personService.getAll()personService为空:

@Component
public class PersonDataPanel extends JPanel {

    public PersonTableModel atm; 

    @Autowired
    public PersonServiceImpl personService;

    public JPanel placePersonTable() {

        log.info("enter placePersonTable method");

        JPanel selectionArea = new JPanel(new FlowLayout(FlowLayout.CENTER));

        List<Person> test = personService.getAll(); //here I am getting the error

        log.info(test.toString());

        atm = new PersonTableModel(test);
        JTable PersonTable = new JTable(atm);
        PersonTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN );
        PersonTable.setFillsViewportHeight(true);   

        repaint();

        return selectionArea;

    }
}

为什么这是我的桌面应用程序的追逐?我以为我的注释是正确的。我使用过@Autowired,所以服务应该实例化?

我非常感谢你的回答!

更新

我的PersonServiceImpl课程:

@Component
public class PersonServiceImpl {

    /**
     * PersonDAO Access
     */
    @Autowired
    private PersonDaoImpl PersonDAO;

    public List<Person> getAll() {
        log.info("reveiving all data in the Person Service");

        return PersonDAO.getAll();
    }
}

0 个答案:

没有答案