Java 如何设置Spring以便省略applicationContext.xml

Java 如何设置Spring以便省略applicationContext.xml,java,spring,jersey,jax-rs,applicationcontext,Java,Spring,Jersey,Jax Rs,Applicationcontext,我有一个JAX应用程序使用spring来设置上下文 我已经设置了一个AppConfig来处理这个。。。像这样 @Configuration @ComponentScan("com.whatever") public class AppConfig {... //etc 但是,我不断收到一个错误,因为应用程序正在查找applicationContext.xml org.springframework.beans.factory.BeanDefinitionStoreException: IOE

我有一个JAX应用程序使用spring来设置上下文

我已经设置了一个AppConfig来处理这个。。。像这样

@Configuration
@ComponentScan("com.whatever")
public class AppConfig {... //etc
但是,我不断收到一个错误,因为应用程序正在查找
applicationContext.xml

org.springframework.beans.factory.BeanDefinitionStoreException: 
IOException parsing XML document from class path resource [applicationContext.xml];
我相信我必须对我的web.xml文件做些什么。。。但我不太明白。这是我的web.xml

<web-app version="3.1" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd">

<listener>
    <listener-class>com.whatever.etc.ApplicationListener</listener-class>
</listener>

<!-- Application -->
<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>
            io.swagger.jaxrs.listing,
            com.shutterstock.media.api
        </param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.scanning.recursive</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.whatever.etc.MainClass</param-value>
    </init-param>
    <init-param>
        <param-name>dirAllowed</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <!-- used to overwrite default 4xx state pages -->
        <param-name>jersey.config.server.response.setStatusOverSendError</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>

<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<!-- Swagger -->
<servlet>
    <servlet-name>Jersey Swagger</servlet-name>
    <servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>

</web-app>

com.whatever.etc.ApplicationListener
Jersey Web应用程序
org.glassfish.jersey.servlet.ServletContainer
jersey.config.server.provider.packages
io.swagger.jaxrs.listing,
com.shutterstock.media.api
jersey.config.server.provider.scanning.recursive
真的
javax.ws.rs.Application
com.whather.etc.MainClass
肮脏的
假的
jersey.config.server.response.setStatusOversender错误
真的
1.
真的
Jersey Web应用程序
/*
大摇大摆
io.swagger.jersey.config.jerseyjaxrconfig
2.

我能给你的最好建议是将项目完全转换为spring boot,以充分利用它提供的所有强大功能

您应该从项目中完全删除所有XML,并执行以下步骤:

TL;DR

使用SpringBoot的一个简单方法是使用SpringInitializer()并开始将代码迁移到新项目

解释在现有项目中迁移到spring boot的开始步骤:

Maven依赖关系

添加以下依赖项并删除所有其他相关的spring依赖项。通常,如果您需要任何其他依赖项,请尝试首先检查是否有spring启动程序。(还要确保你的包装是
jar
。如果你想要
war
,你应该遵循以下链接:)

Java配置

现在,您可以轻松地添加一个名为
config
的包,例如在您的基本包下,并使用自定义配置添加
@Configuration
注释bean

application.properites/yml

如果您还没有,请将application.properties/yml添加到
src/main.resources
,至少使用以下配置(可以在此处找到更多属性:)

  • 通过添加:
    spring.jersey.application path=/
球衣配置

您需要在扩展
ResourceConfig
的自定义java配置类中注册jersey提供程序:

@Configuration
public class JerseyConfiguration extends ResourceConfig {

    public JerseyConfiguration() {
        register(FooController.class);
        register(OtherController.class);
        ...
    }

}
重要注意事项

完成此转换后,您不再需要
web.xml
,因为SpringBoot负责为您创建servlet。您应该从项目中删除除
pom.XML
之外的所有XML,并将所有内容转换为java。它可以使用
@ImportResource
与XML配置共存,但不推荐使用

为了立即使用maven运行您的项目,请使用maven build和命令:
spring boot:run

如果您有系统提供的任何静态文件,请将它们从
src/main/webapp
移动到
src/main/resources/static
(此处有更多信息:)


所有这些都应该从spring boot开始,并允许您删除
applicationContext.xml
,同时保留spring boot的最佳实践。您可以用一种更简单的方式来做,但我认为从长远来看,它会为您提供更好的服务。

您会遇到错误,因为Jersey与Spring集成的默认方式是查找这个applicationContext.xml文件。如果希望减少xml,则需要使用
WebApplicationInitializer以编程方式配置Spring

@Order(1)
public class SpringWebContainerInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        registerContextLoaderListener(servletContext);

        // Set the Jersey used property so it won't load a ContextLoaderListener
        // see bug report https://java.net/jira/browse/JERSEY-2038
        servletContext.setInitParameter("contextConfigLocation", "");
    }

    private void registerContextLoaderListener(ServletContext servletContext) {
        WebApplicationContext webContext;
        webContext = createWebAplicationContext(SpringAnnotationConfig.class);
        servletContext.addListener(new ContextLoaderListener(webContext));
    }

    public WebApplicationContext createWebAplicationContext(Class... configClasses) {
        AnnotationConfigWebApplicationContext context;
        context = new AnnotationConfigWebApplicationContext();
        context.register(configClasses);
        return context;
    }
}
在本例中,您将看到

createWebAplicationContext(SpringAnnotationConfig.class)
只需在那里使用您的
AppConfig

我们在这里所做的是自己加载Spring上下文,当Jersey查找它时,它会找到它,而不是尝试加载上下文本身(否则它会尝试查找applicationContext.xml文件)

另请参见:


感谢您的评论。我将把重点放在靴子上。
@Order(1)
public class SpringWebContainerInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        registerContextLoaderListener(servletContext);

        // Set the Jersey used property so it won't load a ContextLoaderListener
        // see bug report https://java.net/jira/browse/JERSEY-2038
        servletContext.setInitParameter("contextConfigLocation", "");
    }

    private void registerContextLoaderListener(ServletContext servletContext) {
        WebApplicationContext webContext;
        webContext = createWebAplicationContext(SpringAnnotationConfig.class);
        servletContext.addListener(new ContextLoaderListener(webContext));
    }

    public WebApplicationContext createWebAplicationContext(Class... configClasses) {
        AnnotationConfigWebApplicationContext context;
        context = new AnnotationConfigWebApplicationContext();
        context.register(configClasses);
        return context;
    }
}
createWebAplicationContext(SpringAnnotationConfig.class)