1

私たちは Web アプリケーションを構築しており、それHttpServerが開始された後、いくつかのアイテムをデータベースからメモリにロードしようとしています。これにサービス層を利用する方法を私が知っている最良の方法は、次のクラスを使用することです。

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

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 {
    final HttpServer server = HttpServer.createSimpleServer(".", 8181);

    WebappContext ctx = new WebappContext("Socket", "/");

    //enable annotation configuration
    ctx.addContextInitParameter("contextClass", "org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
    ctx.addContextInitParameter("contextLocation", "com.production");

    //allow spring to do all of it's stuff
    ctx.addListener("org.springframework.web.context.ContextLoaderListener");

    //serve static assets
    StaticHttpHandler staticHttpHandler = new StaticHttpHandler("src/main/web");
    server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");

    //deploy
    ctx.deploy(server);

    //NULL
    WorkflowService workflowService = (WorkflowService) ApplicationContextProvider.getApplicationContext().getBean("workflowService");

    server.start();

サービスクラスには注釈が付けられて@Serviceおり、構成クラスは...

@Configuration
@ComponentScan(basePackages = {
        "com.production"
})
@PropertySource(value= {
        "classpath:/application.properties",
        "classpath:/environment-${MYAPP_ENVIRONMENT}.properties"
})
@EnableJpaRepositories("com.fettergroup.production.repositories")
@EnableTransactionManagement
public class Config {

    ...

    @Bean
    public ApplicationContextProvider applicationContextProvider() {
        return new ApplicationContextProvider();
    }
}

グリズリーを使用するこのアプリケーションの何かWebContextが、問題の根底にあると思います。しかし、私はそれについて何をすべきかわからない。グーグルをいくつか実行しましたが、使用しているソリューションが機能するはずです...

4

1 に答える 1