尋找您寶貴的建議/投入。 下面是實際的xml文件 - ApplicationContext和JobSchedulerServiceContext。在業務邏輯中訪問應用程序上下文的實例時發生NullPointerException。如何訪問應用程序上下文的實例?
--- applicationContext.xml的
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="applicationContextImpl" class="com. services.ApplicationContextImpl" scope="singleton">
<property name="dataAcquisitionService">
<ref bean="dataAcquisitionService" />
</property>
<property name="equipDataAcquisitionService">
<ref bean="equipDataAcquisitionService" />
</property>
<property name="emailService">
<ref bean="emailService" />
</property>
<property name="externalIoService">
<ref bean="externalIoService" />
</property>
<property name="persistenceService">
<ref bean="persistenceService" />
</property>
<property name="messageService">
<ref bean="messageService" />
</property>
<property name="uiService">
<ref bean="uiService" />
</property>
</bean>
<bean id="controller" class="com.services.ColServiceController" scope="singleton">
<property name="applicationContextImpl">
<ref bean="applicationContextImpl" />
</property>
<property name="serviceName">
<value>Service Controller</value>
</property>
<property name="serviceMap">
<map>
<entry key="message">
<ref bean="messageService" />
</entry>
<entry key="persistence">
<ref bean="persistenceService" />
</entry>
<entry key="email">
<ref bean="emailService" />
</entry>
<entry key="dataAcquisition">
<ref bean="dataAcquisitionService" />
</entry>
<entry key="equipDataAcquisition">
<ref bean="equipDataAcquisitionService" />
</entry>
<entry key="jobScheduler">
<ref bean="jobSchedulerService" />
</entry>
<entry key="ui">
<ref bean="uiService" />
</entry>
<entry key="externalIo">
<ref bean="externalIoService" />
</entry>
</map>
</property>
</bean>
<bean id="applicationContextProvider" class="com.cymer.services.ApplicationContextProvid er">
</bean>
</beans>
--- JobSchedulerServiceContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="jobSchedulerService" class="com. services.scheduler.JobSchedulerServiceImpl" scope="singleton">
<property name="applicationContextImpl">
<ref bean="applicationContextImpl" />
</property>
<property name="serviceManual">
<value>true</value>
</property>
<property name="serviceName">
<value>Job Scheduler</value>
</property>
<property name="jobSpecifiers">
<list>
<bean id="automatedDataSharingJob" class="com. services.scheduler.JobSpecifier" scope="singleton">
<property name="taskName">
<value>AutomatedDataSharingJob</value>
</property>
<property name="execClass">
<bean class="com.services.scheduler.job.AutomatedDataSha ringJob" scope="singleton">
<property name="applicationContextImpl">
<ref bean="applicationContextImpl" />
</property>
<property name="jobManual">
<value>false</value>
</property>
</bean>
</property>
</bean>
<bean id="runFullVacuumJob" class="com. services.scheduler.JobSpecifier" scope="singleton">
<property name="taskName">
<value>RunFullVacuumJob</value>
</property>
<property name="execClass">
<bean class="com. services.scheduler.job.RunFullVacuumJob" scope="singleton">
<property name="applicationContextImpl">
<ref bean="applicationContextImpl" />
</property>
</list>
</property>
</bean>
<bean id="dataSharingUtil" class="com.datasharing.util.DataSharingUtil" scope="singleton">
</bean>
</beans>
這是原始的Java類產品是通過從獲得servlet的控制加載應用程序上下文控制器。我嘗試實現了ApplicationContextAware並獲得當前上下文
package com..services;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.xml.XmlBeanDefin itionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlAp plicationContext;
import org.springframework.context.support.GenericApplica tionContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.context.ApplicationContextAwar e;
public class ColServiceController
extends ColServiceBase
implements ApplicationContextAware
{
public static final String S_SERVICE_CONTROLLER = "S_Service_Controller";
private static String[] appContexts =
{
"applicationContext.xml",
"dataAcquisitionServiceContext.xml",
"equipDataAcquisitionServiceContext.xml",
"emailServiceContext.xml",
"externalIoServiceContext.xml",
"jobSchedulerServiceContext.xml",
"messageServiceContext.xml",
"persistenceServiceContext.xml",
"statusDataEventAttributeContext.xml",
"uiServiceContext.xml",
};
private Map<String,ColService> serviceMap = new HashMap<String,ColService>();
public ColServiceController()
{
super(ColServiceController.class);
}
private static ApplicationContext appContext;
public static ApplicationContext getApplicationContext()
{
return appContext;
}
public static ApplicationContext loadSpringContext(final String confPath)
{
ApplicationContext context = null;
// If a discrete conf location has been specified,
// load the bean definition files from there
if (!StringUtil.isEmpty(confPath))
{
System.err.println("Loading context files from: " + confPath);
final GenericApplicationContext ctx = new GenericApplicationContext();
final XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
for (int i=0; i < appContexts.length; i++)
{
final File file = new File(confPath + File.separator + appContexts[i]);
if (file.isFile())
{
System.err.println("Loading context file: " + file.getPath());
xmlReader.loadBeanDefinitions(new FileSystemResource(file));
}
else
{
System.err.println("Skipping: " + appContexts[i]);
}
}
ctx.refresh();
context = (ApplicationContext)ctx;
}
// ...otherwise, use the files located in the classpath
else
{
System.err.println("Loading services from classpath.");
context = new ClassPathXmlApplicationContext(appContexts);
}
return context;
}
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
appContext = ctx; }}
的實例,但getApplicationContext()總是返回NULL。一旦我將獲得對上下文的參考,我需要重新安排工作。
我無法弄清楚。任何人都可以看看嗎?
如果遇到此問題,建議我提供解決方案。由於
這花了很多重新格式化,可能仍然可以使用更多。你需要檢查一下,以確保我編輯它時不會弄亂任何東西。 – 2010-11-06 02:47:23