I have a JAX application using spring to set up the context.
我有一个使用spring来设置上下文的JAX应用程序。
I have set up an AppConfig to deal with this... like so.
我已经设置了一个AppConfig来处理这个...就像这样。
@Configuration
@ComponentScan("com.whatever")
public class AppConfig {... //etc
However, I keep getting an error because the application is looking for the applicationContext.xml
.
但是,我一直收到错误,因为应用程序正在查找applicationContext.xml。
org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from class path resource [applicationContext.xml];
I believe I have to do something with my web.xml file... but I can't quite figure it out. Here's my web.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>
2 个解决方案
#1
1
You're getting the error because the default way for Jersey to integrate with Spring is to look for this applicationContext.xml file. If you want to go xml-less, you need to configure Spring programmatically with a WebApplicationInitializer
您收到错误是因为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;
}
}
In this example, where you see
在这个例子中,你看到了什么
createWebAplicationContext(SpringAnnotationConfig.class)
Just use your AppConfig
there.
只需在那里使用您的AppConfig。
What we're doing here is loading the Spring context ourselves, and when Jersey looks for it, it will find it, and not try to load the context itself (where it would otherwise try to find the applicationContext.xml file).
我们在这里做的是自己加载Spring上下文,当Jersey查找它时,它会找到它,而不是尝试加载上下文本身(否则它会尝试查找applicationContext.xml文件)。
See also:
- Combining Spring project and Jersey
结合Spring项目和Jersey
#2
1
The best advice I can give you is fully transform your project to spring boot in order to take advantage of all the great functionality it offers.
我能给你的最好的建议是将你的项目完全转换为spring boot,以便利用它提供的所有强大功能。
You should completely remove all XML from your project and do the following steps:
您应该从项目中完全删除所有XML并执行以下步骤:
TL;DR
An easy way to start with spring boot is using spring initializr (Spring initializr) and start migrating your code to the new project.
使用spring initializr(Spring initializr)开始使用spring initializr并开始将代码迁移到新项目的简单方法。
Explanations of starting steps to migrate to spring boot in an existing project:
在现有项目中迁移到spring boot的开始步骤的说明:
Maven Dependencies
Add the following dependencies and remove all other related spring dependencies. In general if you need any other dependency try to first check if there is a spring boot starter for it. (also make sure your packaging is jar
. if you want a war
you should follow this link: convert to war)
添加以下依赖项并删除所有其他相关的spring依赖项。一般情况下,如果您需要任何其他依赖项,请首先检查是否有弹簧启动启动器。 (还要确保你的包装是罐子。如果你想要一场战争,你应该按照这个链接:转换为战争)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Spring Boot Application
Spring Boot应用程序
Create a class annotated with @SpringBootApplication
that looks like the following in your base package. From this point on create classes only in sub-packages of this package so spring can catch all of your components automatically:
创建一个使用@SpringBootApplication注释的类,其类似于基础包中的以下内容。从这一点开始,仅在此包的子包中创建类,因此spring可以自动捕获所有组件:
@SpringBootApplication
public class FooApplication {
public static void main(String[] args) {
SpringApplication.run(FooApplication .class, args);
}
}
Java Configuration
now you can easily add a package called config
for example under your base package and add @Configuration
annotated beans with your custom configuration.
现在,您可以轻松地在您的基础包下添加一个名为config的包,并使用您的自定义配置添加@Configuration带注释的bean。
application.properites/yml
If you don't already have, add application.properties/yml to src/main.resources
with the following configuration as a minimum (more properties can be found here: spring boot common properties)
如果您还没有,请将application.properties/yml添加到src / main.resources,并至少配置以下配置(可在此处找到更多属性:spring boot common properties)
- Map jersey servlet endpoint by adding:
spring.jersey.application-path=/
通过添加以下命令映射泽西servlet端点:spring.jersey.application-path = /
Jersey Configuration
You need to register jersey providers in a custom java configuration class that extends ResourceConfig
:
您需要在扩展ResourceConfig的自定义Java配置类中注册jersey提供程序:
@Configuration
public class JerseyConfiguration extends ResourceConfig {
public JerseyConfiguration() {
register(FooController.class);
register(OtherController.class);
...
}
}
Important Notes
After doing this transformation you don't need web.xml
anymore as spring boot takes care of creating servlets for you. You should remove all XML from your project except for the pom.xml
and convert everything to java. It can coexist with your XML configuration using @ImportResource
but it is not recommended.
执行此转换后,您不再需要web.xml,因为spring boot负责为您创建servlet。除了pom.xml之外,您应该从项目中删除所有XML,并将所有XML转换为java。它可以使用@ImportResource与您的XML配置共存,但不建议这样做。
In order to run your project now with maven use maven build with the command: spring-boot:run
.
现在使用maven使用maven build运行你的项目命令:spring-boot:run。
If you have any static files served by the system move them from src/main/webapp
to src/main/resources/static
(more info here: Static Content)
如果您有系统提供的任何静态文件,请将它们从src / main / webapp移至src / main / resources / static(此处更多信息:静态内容)
All of this should get your started with spring boot and allow you to remove applicationContext.xml
while keeping best practices of spring boot. You could do it in an easier fashion but I think it will serve you better in the long run.
所有这一切都应该让你开始使用spring boot并允许你删除applicationContext.xml,同时保持spring boot的最佳实践。你可以更容易地做到这一点,但我认为从长远来看它会更好地为你服务。
#1
1
You're getting the error because the default way for Jersey to integrate with Spring is to look for this applicationContext.xml file. If you want to go xml-less, you need to configure Spring programmatically with a WebApplicationInitializer
您收到错误是因为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;
}
}
In this example, where you see
在这个例子中,你看到了什么
createWebAplicationContext(SpringAnnotationConfig.class)
Just use your AppConfig
there.
只需在那里使用您的AppConfig。
What we're doing here is loading the Spring context ourselves, and when Jersey looks for it, it will find it, and not try to load the context itself (where it would otherwise try to find the applicationContext.xml file).
我们在这里做的是自己加载Spring上下文,当Jersey查找它时,它会找到它,而不是尝试加载上下文本身(否则它会尝试查找applicationContext.xml文件)。
See also:
- Combining Spring project and Jersey
结合Spring项目和Jersey
#2
1
The best advice I can give you is fully transform your project to spring boot in order to take advantage of all the great functionality it offers.
我能给你的最好的建议是将你的项目完全转换为spring boot,以便利用它提供的所有强大功能。
You should completely remove all XML from your project and do the following steps:
您应该从项目中完全删除所有XML并执行以下步骤:
TL;DR
An easy way to start with spring boot is using spring initializr (Spring initializr) and start migrating your code to the new project.
使用spring initializr(Spring initializr)开始使用spring initializr并开始将代码迁移到新项目的简单方法。
Explanations of starting steps to migrate to spring boot in an existing project:
在现有项目中迁移到spring boot的开始步骤的说明:
Maven Dependencies
Add the following dependencies and remove all other related spring dependencies. In general if you need any other dependency try to first check if there is a spring boot starter for it. (also make sure your packaging is jar
. if you want a war
you should follow this link: convert to war)
添加以下依赖项并删除所有其他相关的spring依赖项。一般情况下,如果您需要任何其他依赖项,请首先检查是否有弹簧启动启动器。 (还要确保你的包装是罐子。如果你想要一场战争,你应该按照这个链接:转换为战争)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Spring Boot Application
Spring Boot应用程序
Create a class annotated with @SpringBootApplication
that looks like the following in your base package. From this point on create classes only in sub-packages of this package so spring can catch all of your components automatically:
创建一个使用@SpringBootApplication注释的类,其类似于基础包中的以下内容。从这一点开始,仅在此包的子包中创建类,因此spring可以自动捕获所有组件:
@SpringBootApplication
public class FooApplication {
public static void main(String[] args) {
SpringApplication.run(FooApplication .class, args);
}
}
Java Configuration
now you can easily add a package called config
for example under your base package and add @Configuration
annotated beans with your custom configuration.
现在,您可以轻松地在您的基础包下添加一个名为config的包,并使用您的自定义配置添加@Configuration带注释的bean。
application.properites/yml
If you don't already have, add application.properties/yml to src/main.resources
with the following configuration as a minimum (more properties can be found here: spring boot common properties)
如果您还没有,请将application.properties/yml添加到src / main.resources,并至少配置以下配置(可在此处找到更多属性:spring boot common properties)
- Map jersey servlet endpoint by adding:
spring.jersey.application-path=/
通过添加以下命令映射泽西servlet端点:spring.jersey.application-path = /
Jersey Configuration
You need to register jersey providers in a custom java configuration class that extends ResourceConfig
:
您需要在扩展ResourceConfig的自定义Java配置类中注册jersey提供程序:
@Configuration
public class JerseyConfiguration extends ResourceConfig {
public JerseyConfiguration() {
register(FooController.class);
register(OtherController.class);
...
}
}
Important Notes
After doing this transformation you don't need web.xml
anymore as spring boot takes care of creating servlets for you. You should remove all XML from your project except for the pom.xml
and convert everything to java. It can coexist with your XML configuration using @ImportResource
but it is not recommended.
执行此转换后,您不再需要web.xml,因为spring boot负责为您创建servlet。除了pom.xml之外,您应该从项目中删除所有XML,并将所有XML转换为java。它可以使用@ImportResource与您的XML配置共存,但不建议这样做。
In order to run your project now with maven use maven build with the command: spring-boot:run
.
现在使用maven使用maven build运行你的项目命令:spring-boot:run。
If you have any static files served by the system move them from src/main/webapp
to src/main/resources/static
(more info here: Static Content)
如果您有系统提供的任何静态文件,请将它们从src / main / webapp移至src / main / resources / static(此处更多信息:静态内容)
All of this should get your started with spring boot and allow you to remove applicationContext.xml
while keeping best practices of spring boot. You could do it in an easier fashion but I think it will serve you better in the long run.
所有这一切都应该让你开始使用spring boot并允许你删除applicationContext.xml,同时保持spring boot的最佳实践。你可以更容易地做到这一点,但我认为从长远来看它会更好地为你服务。