简体   繁体   中英

NullPointerException while accessing instance of application context in business logic. How to access an instance of application context?

Looking out for your valuable suggestion/inputs. Below are the actual xml files - ApplicationContext and JobSchedulerServiceContext.

--- 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>

And this is the original java class of product which is loading the application context by getting control from servlet controller. I was trying out to implement ApplicationContextAware and get an instance of current context

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; }}

But the getApplicationContext() is always returning NULL. Once i will get the reference to context , i need to reschedule the job.

I am not able to figure it out. Could anyone have a look at it?

Do suggest me the solution if you come across this. Thanks

Make your class implement ApplicationContextAware . You will be passed the ApplicationContext in the setter method that you are forced to implement.

Update:

  • make the context field non-static.
  • get rid of the static method completely
  • read the spring official documentation - using static methods is never a good idea with spring beans. Their instances live inside the spring container, and they are instantiated by spring. If it is you who instantiates your classes or invoke static methods, then spring does not work.

就像是

ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"context.xml"});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM