2012-07-08 24 views
2

我正在嘗試在桌面應用程序中使用spring,但是我在自動裝配操作方法中遇到了問題,例如我的JPanel在桌面應用程序中不能使用@Autowired

我加載ApplicationContext的在我的主要方法如下:

public static void main(String[] args) { 

     new ClassPathXmlApplicationContext(
       "classpath:/META-INF/spring/applicationContext.xml"); 
      MainFrame frame = new MainFrame(); 
    Signup signup = new Signup(); 
    frame.add(signup); 
    frame.setResizable(false); 
    frame.setTitle("Please input your data"); 
    frame.setBounds(100, 100, 450, 180); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 

} 

,我可以看到,它的加載,沒有任何問題。

我的面板代碼:

@Component 
public class Signup extends JPanel { 


    @Autowired 
    private UserDao userDao; 

    public Signup() { 



     JButton btn_submit = new JButton("Submit"); 
     btn_submit.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       registerUser(); 
      } 
     }); 



    } 

    private void registerUser() { 

     User newUser = new User(); 
     newUser.setName(username); 
     newUser.setSalary(salary); 
     userDao.addUser(newUser); 

    } 
} 

context:component-scan配置正確,和我使用context:annotation-config太多,但我總是得到NullPointerException異常userDao.addUser(newUser); 這意味着依賴注入不工作,因爲它應該。

請指教如何解決此問題。

UPDATE:的applicationContext.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:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation=" 
       http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <context:component-scan base-package="${project.groupId}" /> 

    <context:annotation-config /> 

    <tx:annotation-driven transaction-manager="transactionManager" /> 

    <bean id="propertyPlaceholderConfigurer" 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 

       <value>classpath:messages/application.properties</value> 

      </list> 
     </property> 
    </bean> 


    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 

     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan" value="${project.groupId}.domain" /> 


     <property name="hibernateProperties"> 
      <value> 
       hibernate.dialect=org.hibernate.dialect.DerbyDialect 
       hibernate.show_sql=false 
       hibernate.format_sql=false 
       hibernate.hbm2ddl.auto=validate 
      </value> 
     </property> 

    </bean> 


    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 

     <property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" /> 

     <property name="url" value="jdbc:derby:test" /> 

     <property name="username" value="root" /> 

     <property name="password" value="root" /> 

    </bean> 


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

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> 


</beans> 
+2

小心分享你的Spring配置? – GaryF 2012-07-08 22:22:30

+0

@GaryF,沒有太多可以告訴它,但無論如何,我分享它。 – 2012-07-08 22:27:12

+0

您希望注入UserDao的哪個具體子類?你能否提供該課程的代碼?它是什麼包? project.groupId的價值是什麼? – 2012-07-08 22:28:22

回答

8

如果你要在桌面環境下配置Spring,然後必須是一個與ApplicationContext工作。

例如,如果你想獲得你已經張貼在這裏您Signup類的保持,你會做這樣的事情在你main方法:

public static void main(String[] args) { 
    ApplicationContext appContext = new ClassPathXmlApplicationContext(
      "classpath:/META-INF/spring/applicationContext.xml"); 
    Signup signup = appContext.getBean(Signup.class); 
    //use signup here... 
} 

使用new Signup()獲得的新實例Signup類,它不會以您想要的方式工作,因爲您希望它成爲Spring託管類! (其實,你可以這樣工作,但這超出了我的答案)

+1

這是一個有經驗的人的回答,你發現沒有我發佈的錯誤,非常感謝,這是我的一個愚蠢的錯誤。 – 2012-07-08 22:30:40

+0

+1很棒的猜測! – 2012-07-08 22:30:50

+1

大聲笑,這是一個常見的誤解。 :) – 2012-07-08 22:31:35