1

我正在使用 Swing 开发一个桌面应用程序,但是我想使用 Spring 框架来使用 IOC,并且我想使用接口技术来做。但是当我通过接口的实例调用函数并且该接口由声明被调用函数的类实现时,我得到了 nullPointerException。

这是我的简单应用程序代码:

界面:

public interface IEmpMasterService {    
    public InternalResultsResponse<EmpMaster> maintainEmpMaster(
            EmpMaster empMaster);
}

实现类:

public class EmpMasterService implements IEmpMasterService {    
    private IEmpMasterDAO empMasterDAO; 
    public IEmpMasterDAO getEmpMasterDAO() {
        return empMasterDAO;
    }
    public void setEmpMasterDAO(IEmpMasterDAO empMasterDAO) {
        this.empMasterDAO = empMasterDAO;
    }
    @Override
    public InternalResultsResponse<EmpMaster> maintainEmpMaster(
            EmpMaster empMaster) {
        InternalResultsResponse<EmpMaster> response = new InternalResultsResponse<EmpMaster>();
        //some logic her...
        response = getEmpMasterDAO().maintainEmpMaster(request);
        return response;
    }}

DAO接口:

public interface IEmpMasterDAO {
    InternalResultsResponse<EmpMaster> maintainEmpMaster(
            EmpMasterRequest request);
}
class Implementing DAO:
public class EmpMasterDAO extends HibernateDaoSupport implements IEmpMasterDAO{
    @Override
    public InternalResultsResponse<EmpMaster> maintainEmpMaster(
            EmpMasterRequest empMasterRequest) {
//hibernate code goes here...       
        return resultsResponse;
    }
} 

主类:

public class EmpManager extends javax.swing.JFrame {    
    private IEmpMasterService empMasterService;
    private static ApplicationContext context;  
    public IEmpMasterService getEmpMasterService() {
        return empMasterService;
    }
    public void setEmpMasterService(IEmpMasterService empMasterService) {
        this.empMasterService = empMasterService;
    }
public static void main(String args[]) {
        context = new ClassPathXmlApplicationContext(new String[]{"ApplicationContext.xml"});
empMasterService = (EmpMasterService)context.getBean("empMasterService");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        String name = jTextField1.getText();
        String salary = jTextField2.getText();
        EmpMaster empMaster = new EmpMaster();
        empMaster.setEmpName(name);
        empMaster.setSalary(Double.parseDouble(salary));  
//***** getting Error at this line *****//    
        InternalResultsResponse<EmpMaster> response = getEmpMasterService().maintainEmpMaster(empMaster);       
    }

我的 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <import resource="dataSource.xml"/>
       <bean id="mainFrame" class="org.UI.EmpManager">
        <property name="title"> <value>Spring Example</value></property>    
        <property name="empMasterService" ref="empMasterServiceImpl"></property>    
    </bean>     
    <bean id="empMasterServiceImpl" class="org.service.EmpMasterService">
        <property name="empMasterDAO" ref="empMasterDAOTarget"></property>      
    </bean> 
    <bean id="empMasterDAOTarget" class="org.DAO.EmpMasterDAO">
        <property name="hibernateTemplate" ref="matrixHibernateTemplate"/>
    </bean> 
</beans>

当我点击按钮时,我得到了一个错误。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at org.UI.EmpManager.jButton1ActionPerformed(EmpManager.java:140)
    at org.UI.EmpManager.access$0(EmpManager.java:132)
    at org.UI.EmpManager$1.actionPerformed(EmpManager.java:67)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
4

2 回答 2

2

您如何创建 EmpManager,在您的应用程序 xml 中有一个用于管理器的 bean,但它也包含您的 main 方法。然后在您的 main 方法中,您将获取应用程序上下文并设置服务 bean,因此您的 EmpManager bean 不会在您的应用程序上下文中执行任何操作。

我建议将 main 方法提取到不同的类中,在该类中加载应用程序上下文并仅在 UI 管理器上使用 getBean。您当前的设置方式忽略了 Spring 的许多 IoC 功能,直接从应用程序上下文中获取 bean,而不是让 Spring 为您做这些。

这可能不会直接解决你的问题,但它至少可以让 Spring 有一个单一的入口点,并将 DI 保留在 Spring 手中,而不是依靠你自己设置它们。

于 2012-06-05T13:23:53.663 回答
0

您的代码中缺少某些内容。

您发布的主要功能不会调用任何 Swing 框架的创建。这真的是您要调用的代码吗?

其次,bean 名称不匹配,在您的 xml 中,bean 被命名为empMasterServiceImpl,而在代码中您使用empMasterService. 不起作用。

于 2012-06-05T09:43:47.780 回答