如何让我的 Spring 引导应用程序基于外部 Servlet?
How can I base my Spring Boot app on an external Servlet?
我正在尝试将 this example 变成一个 Spring 引导应用程序,问题在于 RestfulServer
是一个 servlet,我并不是真的想制作一个单独的应用程序,但是一个或多或少扩展了那个 servlet 的应用程序。我似乎 运行 遇到的问题是我尝试过 WebApplicationInitializer
和 SpringBootApplicationInitializer
的问题是它们在执行其他 @Configuration
classes 之前未初始化 servlet。如何将 JpaServerDemo
class 转换为 Spring 引导?(注意:我们不需要第二个 servlet)
更新 这是我尝试的最后一件事,但是我也尝试了 WebApplicationInitializer
的各种迭代,或者在 configure
方法中做一些事情似乎很棘手,因为它没有 servletContext
,而 onStartup
也没有 ApplicationContext
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
public void onStartup( ServletContext servletContext ) throws ServletException {
super.onStartup( servletContext );
RestfulServer server = new RestfulServer();
servletContext.addServlet( "restful", server );
/*
* We want to support FHIR DSTU3 format. This means that the server
* will use the DSTU3 bundle format and other DSTU3 encoding changes.
*
* If you want to use DSTU1 instead, change the following line, and change the 3 occurrences of dstu2 in web.xml to dstu1
*/
server.setFhirContext( FhirContext.forDstu3() );
WebApplicationContext myAppCtx = this.createRootApplicationContext( servletContext );
// Get the spring context from the web container (it's declared in web.xml)
/*
* The BaseJavaConfigDstu3.java class is a spring configuration
* file which is automatically generated as a part of hapi-fhir-jpaserver-base and
* contains bean definitions for a resource provider for each resource type
*/
List<IResourceProvider> beans = myAppCtx.getBean( "myResourceProvidersDstu3", List.class );
server.setResourceProviders( beans );
/*
* The system provider implements non-resource-type methods, such as
* transaction, and global history.
*/
server.setPlainProviders( myAppCtx.getBean( "mySystemProviderDstu3", JpaSystemProviderDstu3.class ) );
/*
* The conformance provider exports the supported resources, search parameters, etc for
* this server. The JPA version adds resource counts to the exported statement, so it
* is a nice addition.
*/
IFhirSystemDao<Bundle, Meta> systemDao = myAppCtx.getBean( "mySystemDaoDstu3", IFhirSystemDao.class );
DaoConfig daoConfig = myAppCtx.getBean( DaoConfig.class );
JpaConformanceProviderDstu3 confProvider = new JpaConformanceProviderDstu3( server, systemDao, daoConfig );
confProvider.setImplementationDescription( "Example Server" );
server.setServerConformanceProvider( confProvider );
/*
* Enable ETag Support (this is already the default)
*/
server.setETagSupport( ETagSupportEnum.ENABLED );
/*
* This server tries to dynamically generate narratives
*/
FhirContext ctx = server.getFhirContext();
ctx.setNarrativeGenerator( new
DefaultThymeleafNarrativeGenerator() );
/*
* Default to JSON and pretty printing
*/
server.setDefaultPrettyPrint( true );
server.setDefaultResponseEncoding( EncodingEnum.JSON );
/*
* -- New in HAPI FHIR 1.5 --
* This configures the server to page search results to and from
* the database, instead of only paging them to memory. This may mean
* a performance hit when performing searches that return lots of results,
* but makes the server much more scalable.
*/
server.setPagingProvider( myAppCtx.getBean( DatabaseBackedPagingProvider.class ) );
/*
* Load interceptors for the server from Spring (these are defined in FhirServerConfig.java)
*/
Collection<IServerInterceptor> interceptorBeans = myAppCtx.getBeansOfType( IServerInterceptor.class ).values();
for (
IServerInterceptor interceptor : interceptorBeans )
{
server.registerInterceptor( interceptor );
}
/*
* If you are hosting this server at a specific DNS name, the server will try to
* figure out the FHIR base URL based on what the web container tells it, but
* this doesn't always work. If you are setting links in your search bundles that
* just refer to "localhost", you might want to use a server address strategy:
*/
//setServerAddressStrategy(new HardcodedServerAddressStrategy("http://example.com/fhir/baseDstu2"));
/*
* If you are using DSTU3+, you may want to add a terminology uploader, which allows
* uploading of external terminologies such as Snomed CT. Note that this uploader
* does not have any security attached (any anonymous user may use it by default)
* so it is a potential security vulnerability. Consider using an AuthorizationInterceptor
* with this feature.
*/
server.registerProvider( myAppCtx.getBean( TerminologyUploaderProviderDstu3.class ) );
}
@Override
protected SpringApplicationBuilder configure( final SpringApplicationBuilder builder ) {
return builder;
}
public static void main( String[] args ) {
configureApplication( new SpringApplicationBuilder() ).run( args );
}
private static SpringApplicationBuilder configureApplication( final SpringApplicationBuilder builder ) {
return builder.sources( Application.class );
}
}
这似乎有效,我将 @WebServlet
添加到 class,并使用 Spring 4.3 的无注释构造函数注入功能注入 WebApplicationContext
。我还将 @ServletComponentScan
添加到我的 Application
中,它在同一个包
中
@WebServlet( "/fhir/*" )
public class FhirServlet extends RestfulServer {
private static final long serialVersionUID = 3341258540126825379L;
private final WebApplicationContext myAppCtx;
public FhirServlet( WebApplicationContext myAppCtx ) {
this.myAppCtx = myAppCtx;
}
@Override
protected void initialize() throws ServletException {
this.setFhirContext( FhirContext.forDstu3() );
this.setServerAddressStrategy( new IncomingRequestAddressStrategy() );
this.setDefaultPrettyPrint( true );
this.setDefaultResponseEncoding( EncodingEnum.JSON );
this.setETagSupport( ETagSupportEnum.ENABLED );
/*
* The BaseJavaConfigDstu3.java class is a spring configuration
* file which is automatically generated as a part of hapi-fhir-jpaserver-base and
* contains bean definitions for a resource provider for each resource type
*/
List<IResourceProvider> beans = myAppCtx.getBean( "myResourceProvidersDstu3", List.class );
setResourceProviders( beans );
/*
* The system provider implements non-resource-type methods, such as
* transaction, and global history.
*/
setPlainProviders( myAppCtx.getBean( "mySystemProviderDstu3", JpaSystemProviderDstu3.class ) );
/*
* The conformance provider exports the supported resources, search parameters, etc for
* this server. The JPA version adds resource counts to the exported statement, so it
* is a nice addition.
*/
IFhirSystemDao<Bundle, Meta> systemDao = myAppCtx.getBean( "mySystemDaoDstu3", IFhirSystemDao.class );
JpaConformanceProviderDstu3 confProvider = new JpaConformanceProviderDstu3( this, systemDao, myAppCtx.getBean( DaoConfig.class ) );
confProvider.setImplementationDescription( "Example Server" );
setServerConformanceProvider( confProvider );
/*
* This server tries to dynamically generate narratives
*/
getFhirContext().setNarrativeGenerator( new DefaultThymeleafNarrativeGenerator() );
/*
* -- New in HAPI FHIR 1.5 --
* This configures the server to page search results to and from
* the database, instead of only paging them to memory. This may mean
* a performance hit when performing searches that return lots of results,
* but makes the server much more scalable.
*/
setPagingProvider( myAppCtx.getBean( DatabaseBackedPagingProvider.class ) );
/*
* Load interceptors for the server from Spring (these are defined in FhirServerConfig.java)
*/
Collection<IServerInterceptor> interceptorBeans = myAppCtx.getBeansOfType( IServerInterceptor.class ).values();
for ( IServerInterceptor interceptor : interceptorBeans ) {
this.registerInterceptor( interceptor );
}
/*
* If you are hosting this server at a specific DNS name, the server will try to
* figure out the FHIR base URL based on what the web container tells it, but
* this doesn't always work. If you are setting links in your search bundles that
* just refer to "localhost", you might want to use a server address strategy:
*/
//setServerAddressStrategy(new HardcodedServerAddressStrategy("http://example.com/fhir/baseDstu2"));
/*
* If you are using DSTU3+, you may want to add a terminology uploader, which allows
* uploading of external terminologies such as Snomed CT. Note that this uploader
* does not have any security attached (any anonymous user may use it by default)
* so it is a potential security vulnerability. Consider using an AuthorizationInterceptor
* with this feature.
*/
registerProvider( myAppCtx.getBean( TerminologyUploaderProviderDstu3.class ) );
}
}
我正在尝试将 this example 变成一个 Spring 引导应用程序,问题在于 RestfulServer
是一个 servlet,我并不是真的想制作一个单独的应用程序,但是一个或多或少扩展了那个 servlet 的应用程序。我似乎 运行 遇到的问题是我尝试过 WebApplicationInitializer
和 SpringBootApplicationInitializer
的问题是它们在执行其他 @Configuration
classes 之前未初始化 servlet。如何将 JpaServerDemo
class 转换为 Spring 引导?(注意:我们不需要第二个 servlet)
更新 这是我尝试的最后一件事,但是我也尝试了 WebApplicationInitializer
的各种迭代,或者在 configure
方法中做一些事情似乎很棘手,因为它没有 servletContext
,而 onStartup
也没有 ApplicationContext
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
public void onStartup( ServletContext servletContext ) throws ServletException {
super.onStartup( servletContext );
RestfulServer server = new RestfulServer();
servletContext.addServlet( "restful", server );
/*
* We want to support FHIR DSTU3 format. This means that the server
* will use the DSTU3 bundle format and other DSTU3 encoding changes.
*
* If you want to use DSTU1 instead, change the following line, and change the 3 occurrences of dstu2 in web.xml to dstu1
*/
server.setFhirContext( FhirContext.forDstu3() );
WebApplicationContext myAppCtx = this.createRootApplicationContext( servletContext );
// Get the spring context from the web container (it's declared in web.xml)
/*
* The BaseJavaConfigDstu3.java class is a spring configuration
* file which is automatically generated as a part of hapi-fhir-jpaserver-base and
* contains bean definitions for a resource provider for each resource type
*/
List<IResourceProvider> beans = myAppCtx.getBean( "myResourceProvidersDstu3", List.class );
server.setResourceProviders( beans );
/*
* The system provider implements non-resource-type methods, such as
* transaction, and global history.
*/
server.setPlainProviders( myAppCtx.getBean( "mySystemProviderDstu3", JpaSystemProviderDstu3.class ) );
/*
* The conformance provider exports the supported resources, search parameters, etc for
* this server. The JPA version adds resource counts to the exported statement, so it
* is a nice addition.
*/
IFhirSystemDao<Bundle, Meta> systemDao = myAppCtx.getBean( "mySystemDaoDstu3", IFhirSystemDao.class );
DaoConfig daoConfig = myAppCtx.getBean( DaoConfig.class );
JpaConformanceProviderDstu3 confProvider = new JpaConformanceProviderDstu3( server, systemDao, daoConfig );
confProvider.setImplementationDescription( "Example Server" );
server.setServerConformanceProvider( confProvider );
/*
* Enable ETag Support (this is already the default)
*/
server.setETagSupport( ETagSupportEnum.ENABLED );
/*
* This server tries to dynamically generate narratives
*/
FhirContext ctx = server.getFhirContext();
ctx.setNarrativeGenerator( new
DefaultThymeleafNarrativeGenerator() );
/*
* Default to JSON and pretty printing
*/
server.setDefaultPrettyPrint( true );
server.setDefaultResponseEncoding( EncodingEnum.JSON );
/*
* -- New in HAPI FHIR 1.5 --
* This configures the server to page search results to and from
* the database, instead of only paging them to memory. This may mean
* a performance hit when performing searches that return lots of results,
* but makes the server much more scalable.
*/
server.setPagingProvider( myAppCtx.getBean( DatabaseBackedPagingProvider.class ) );
/*
* Load interceptors for the server from Spring (these are defined in FhirServerConfig.java)
*/
Collection<IServerInterceptor> interceptorBeans = myAppCtx.getBeansOfType( IServerInterceptor.class ).values();
for (
IServerInterceptor interceptor : interceptorBeans )
{
server.registerInterceptor( interceptor );
}
/*
* If you are hosting this server at a specific DNS name, the server will try to
* figure out the FHIR base URL based on what the web container tells it, but
* this doesn't always work. If you are setting links in your search bundles that
* just refer to "localhost", you might want to use a server address strategy:
*/
//setServerAddressStrategy(new HardcodedServerAddressStrategy("http://example.com/fhir/baseDstu2"));
/*
* If you are using DSTU3+, you may want to add a terminology uploader, which allows
* uploading of external terminologies such as Snomed CT. Note that this uploader
* does not have any security attached (any anonymous user may use it by default)
* so it is a potential security vulnerability. Consider using an AuthorizationInterceptor
* with this feature.
*/
server.registerProvider( myAppCtx.getBean( TerminologyUploaderProviderDstu3.class ) );
}
@Override
protected SpringApplicationBuilder configure( final SpringApplicationBuilder builder ) {
return builder;
}
public static void main( String[] args ) {
configureApplication( new SpringApplicationBuilder() ).run( args );
}
private static SpringApplicationBuilder configureApplication( final SpringApplicationBuilder builder ) {
return builder.sources( Application.class );
}
}
这似乎有效,我将 @WebServlet
添加到 class,并使用 Spring 4.3 的无注释构造函数注入功能注入 WebApplicationContext
。我还将 @ServletComponentScan
添加到我的 Application
中,它在同一个包
@WebServlet( "/fhir/*" )
public class FhirServlet extends RestfulServer {
private static final long serialVersionUID = 3341258540126825379L;
private final WebApplicationContext myAppCtx;
public FhirServlet( WebApplicationContext myAppCtx ) {
this.myAppCtx = myAppCtx;
}
@Override
protected void initialize() throws ServletException {
this.setFhirContext( FhirContext.forDstu3() );
this.setServerAddressStrategy( new IncomingRequestAddressStrategy() );
this.setDefaultPrettyPrint( true );
this.setDefaultResponseEncoding( EncodingEnum.JSON );
this.setETagSupport( ETagSupportEnum.ENABLED );
/*
* The BaseJavaConfigDstu3.java class is a spring configuration
* file which is automatically generated as a part of hapi-fhir-jpaserver-base and
* contains bean definitions for a resource provider for each resource type
*/
List<IResourceProvider> beans = myAppCtx.getBean( "myResourceProvidersDstu3", List.class );
setResourceProviders( beans );
/*
* The system provider implements non-resource-type methods, such as
* transaction, and global history.
*/
setPlainProviders( myAppCtx.getBean( "mySystemProviderDstu3", JpaSystemProviderDstu3.class ) );
/*
* The conformance provider exports the supported resources, search parameters, etc for
* this server. The JPA version adds resource counts to the exported statement, so it
* is a nice addition.
*/
IFhirSystemDao<Bundle, Meta> systemDao = myAppCtx.getBean( "mySystemDaoDstu3", IFhirSystemDao.class );
JpaConformanceProviderDstu3 confProvider = new JpaConformanceProviderDstu3( this, systemDao, myAppCtx.getBean( DaoConfig.class ) );
confProvider.setImplementationDescription( "Example Server" );
setServerConformanceProvider( confProvider );
/*
* This server tries to dynamically generate narratives
*/
getFhirContext().setNarrativeGenerator( new DefaultThymeleafNarrativeGenerator() );
/*
* -- New in HAPI FHIR 1.5 --
* This configures the server to page search results to and from
* the database, instead of only paging them to memory. This may mean
* a performance hit when performing searches that return lots of results,
* but makes the server much more scalable.
*/
setPagingProvider( myAppCtx.getBean( DatabaseBackedPagingProvider.class ) );
/*
* Load interceptors for the server from Spring (these are defined in FhirServerConfig.java)
*/
Collection<IServerInterceptor> interceptorBeans = myAppCtx.getBeansOfType( IServerInterceptor.class ).values();
for ( IServerInterceptor interceptor : interceptorBeans ) {
this.registerInterceptor( interceptor );
}
/*
* If you are hosting this server at a specific DNS name, the server will try to
* figure out the FHIR base URL based on what the web container tells it, but
* this doesn't always work. If you are setting links in your search bundles that
* just refer to "localhost", you might want to use a server address strategy:
*/
//setServerAddressStrategy(new HardcodedServerAddressStrategy("http://example.com/fhir/baseDstu2"));
/*
* If you are using DSTU3+, you may want to add a terminology uploader, which allows
* uploading of external terminologies such as Snomed CT. Note that this uploader
* does not have any security attached (any anonymous user may use it by default)
* so it is a potential security vulnerability. Consider using an AuthorizationInterceptor
* with this feature.
*/
registerProvider( myAppCtx.getBean( TerminologyUploaderProviderDstu3.class ) );
}
}