留意您宝贵的建议/意见。 下面是实际的xml文件 - ApplicationContext和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>
这是原始的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。一旦我得到上下文的参考,我需要重新安排工作。
我无法理解。有人可以看看吗?
如果你遇到这个问题,请向我建议解决方案。感谢
答案 0 :(得分:3)
让您的班级实施ApplicationContextAware
。您将在您被迫实施的setter方法中传递ApplicationContext
。
更新
context
字段非静态。static
方法对于spring bean来说绝不是一个好主意。他们的实例存在于spring容器中,并且它们由spring实例化。如果是你谁实例化你的类或调用静态方法,那么spring不起作用。答案 1 :(得分:0)
类似
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"context.xml"});