我正在使用Swing开发一个桌面应用程序,但是我想使用Spring框架来使用IOC,而我想使用Interface技术。但是当我通过接口的实例调用一个函数并且该接口是由声明了被调用函数的类实现时,我得到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;
}}
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)
请帮助我非常紧急,我很长时间都在这里。
答案 0 :(得分:2)
你是如何创建EmpManager的,你的应用程序xml中有一个bean用于管理器,但它也包含你的main方法。然后在main方法中获取应用程序上下文并设置服务bean,这样你的EmpManager bean就不会在你的应用程序上下文中做任何事情。
我建议将main方法解压缩到另一个类中,在该类中加载Application Context并仅在UI管理器上使用getBean。你现在设置它的方式忽略了Spring的许多IoC功能,直接从应用程序上下文中获取bean,而不是让Spring为你做这些。
这可能无法直接解决您的问题,但它至少会让Spring有一个单一的入口点,并将DI保留在Spring手中,而不是依赖于您自己设置它。
答案 1 :(得分:0)
您的代码中缺少某些内容。
您发布的主要功能不会调用任何Swing框架的创建。它真的是你要调用的代码吗?
其次,bean名称不匹配,在你的xml中,bean被命名为empMasterServiceImpl
,而在代码中你正在使用empMasterService
。它不起作用。