在过去的两周里,我的申请进展顺利。昨晚我远程登录工作,发现当我运行我的应用程序时,我的ApplicationContextProvider
班级不再了解应用程序上下文。除了重新启动我的电脑之外,我还运行了 Maven clean & build。好像摇不动...
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext (ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
}
我的主要课程:
public static void main(String[] args) throws IOException {
System.setProperty("java.util.logging.SimpleFormatter.format", "%4$s: %5$s%n");
final HttpServer server = HttpServer.createSimpleServer(".", 80);
WebappContext ctx = new WebappContext("ProductionQueue", "/");
//enable annotation configuration
ctx.addContextInitParameter("contextClass", "org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
ctx.addContextInitParameter("contextConfigLocation", "com.production");
//allow spring to do all of it's stuff
ctx.addListener("org.springframework.web.context.ContextLoaderListener");
....
ctx.deploy(server);
server.start();
//start the production process
Production.init();
System.in.read();
server.stop();
我的生产班:
public class Production {
private static final Logger logger = Logger.getLogger(Production.class.getName());
/* A list of active workflows */
private static List<Workflow> workflowList = new ArrayList<Workflow>();
private static ProductionService productionService;
/**
* Initialize the production line
*/
public static void init() {
logger.info("Initializing production workflows...");
ApplicationContext context = ApplicationContextProvider.getApplicationContext(); //THIS IS NULL
productionService = (ProductionService) context.getBean("productionService");
根本没有修改任何配置。在我的配置类中,我确实有一个 bean ......
@Configuration
@ComponentScan(basePackages = {
"com.production"
})
@PropertySource(value= {
"classpath:/application.properties",
"classpath:/environment-${FETTER_ENVIRONMENT}.properties"
})
@EnableJpaRepositories("com.production.repository")
@EnableTransactionManagement
public class Config {
@Value("${db.url}")
String PROPERTY_DATABASE_URL;
@Value("${db.user}")
String PROPERTY_DATABASE_USER;
@Value("${db.password}")
String PROPERTY_DATABASE_PASSWORD;
@Value("${persistenceUnit.default}")
String PROPERTY_DEFAULT_PERSISTENCE_UNIT;
@Value("${hibernate.dialect}")
String PROPERTY_HIBERNATE_DIALECT;
@Value("${hibernate.format_sql}")
String PROPERTY_HIBERNATE_FORMAT_SQL;
@Value("${hibernate.show_sql}")
String PROPERTY_HIBERNATE_SHOW_SQL;
@Value("${entitymanager.packages.to.scan}")
String PROPERTY_ENTITYMANAGER_PACKAGES_TO_SCAN;
@Bean
public ApplicationContextProvider applicationContextProvider() {
return new ApplicationContextProvider();
}