在本文中,我们将提供Spring Boot XML配置示例。我们将用XML配置创建一个REST网络服务。我们将在java配置中导入我们的XML文件。我们需要在我们的spring boot应用程序中使用@ImportResource
与@Configuration
。我们可以在项目classpath中保留我们的XML文件。在这里,我们将创建一个作为REST网络服务工作的spring boot网络应用。我们将创建一个服务类,并将其配置在XML配置中。我们还将在XML配置中配置Jackson2消息转换器以缩进JSON响应。
为了加载XML配置,@ImportResource
被使用如下。
@ImportResource("classpath:app-config.xml")
我们将在我们的spring boot应用程序中使用@ImportResource
与@SpringBootApplication
。寻找完整的例子,一步一步来。
在我们的例子中,我们使用了以下软件。
在eclipse中查找项目结构。
找到我们例子中使用的maven文件。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.concretepage</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-demo</name>
<description>Spring Boot Demo Project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
找到maven文件中配置的spring boot starter的描述。
*spring-boot-starter-parent 。用于依赖性管理的父POM。
spring-boot-starter-web。用于构建Web、REST应用程序的启动程序。它使用Tomcat服务器作为默认的嵌入式服务器。
spring-boot-devtools 。它提供了开发者工具。这些工具在应用开发模式中很有帮助。开发者工具的一个特点是在代码发生任何变化时自动重新启动服务器。
spring-boot-maven-plugin 。它用于创建应用程序的可执行JAR。
我已经创建了一个XML配置样本。
app-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean class="com.concretepage.service.ArticleService"/>
<bean name="jackson2ObjectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="indentOutput" value="true"/>
</bean>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jackson2ObjectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
在这里我为服务类创建了一个Bean。为了缩进JSON响应,我们配置了Jackson2消息转换器。我们将在我们的spring boot应用程序中使用这个XML配置。
在配置文件中使用@ImportResource
与@Configuration
导入XML文件。在我们的主类中,我们使用@SpringBootApplication
注释。@SpringBootApplication
是@Configuration
、@EnableAutoConfiguration
和@ComponentScan
注释的组合。
MyApplication.java
package com.concretepage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource("classpath:app-config.xml")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
找到我们例子中使用的服务。
ArticleService.java
package com.concretepage.service;
import java.util.ArrayList;
import java.util.List;
import com.concretepage.entity.Article;
public class ArticleService {
public List<Article> getAllArticles(){
List<Article> list = new ArrayList<Article>();
list.add(new Article(1, "Java Concurrency", "Java"));
list.add(new Article(2, "Hibernate HQL", "Hibernate"));
list.add(new Article(3, "Spring MVC with Hibernate", "Spring"));
return list;
}
}
Article.java
package com.concretepage.entity;
public class Article {
private int articleId;
private String title;
private String category;
public Article(int articleId, String title, String category) {
this.articleId = articleId;
this.title = title;
this.category = category;
}
public int getArticleId() {
return articleId;
}
public String getTitle() {
return title;
}
public String getCategory() {
return category;
}
}
找到我们的例子中使用的控制器。
ArticleController.java
package com.concretepage.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.concretepage.entity.Article;
import com.concretepage.service.ArticleService;
@RestController
@RequestMapping("user")
public class ArticleController {
@Autowired
private ArticleService articleService;
@GetMapping("articles")
public List<Article> getAllArticles() {
List<Article> list = articleService.getAllArticles();
return list;
}
}
找到测试应用程序的步骤。
mvn clean eclipse:eclipse
在eclipse中刷新该项目。现在classpath已经设置好了。
3. 打开MyApplication
类并作为java应用程序运行。
4. 访问URL
http://localhost:8080/user/articles
找到输出的打印屏幕。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.concretepage.com/spring-boot/spring-boot-xml-configuration-example
内容来源于网络,如有侵权,请联系作者删除!