【Spring Boot】XML 配置示例

您所在的位置:网站首页 xml文件编写实例有哪些 【Spring Boot】XML 配置示例

【Spring Boot】XML 配置示例

2023-07-20 14:36| 来源: 网络整理| 查看: 265

文章目录 示例工具版本在 Eclipse 中的项目结构Maven 文件创建 XML 配置使用 @ImportResource 来导入 XML 配置创建 Service 和 Controller测试应用程序参考文献源码下载

在这一页,我们将提供spring boot XML配置的例子。

我们将用XML配置创建一个REST网络服务。

我们将在java配置中导入我们的XML文件。

我们需要在spring boot应用程序中使用@ImportResource和@Configuration。

我们可以在项目类路径中保留我们的XML文件。

在这里,我们将创建一个spring boot web应用程序,它将作为REST web服务工作。

我们将创建一个服务类,并将其配置在XML配置中。我们还将在XML配置中配置Jackson2消息转换器以缩进JSON响应。

为了加载XML配置,我们使用了@ImportResource,如下所示。

@ImportResource("classpath:app-config.xml")

我们将在我们的spring boot应用程序中使用@ImportResource和@SpringBootApplication。找出完整的例子。

示例工具版本 Java 8Spring Boot 1.5.2.RELEASEMaven 3.3Eclipse Mars 在 Eclipse 中的项目结构

在 eclipse 中找到项目结构。 在这里插入图片描述

Maven 文件

找到我们例子中使用的maven文件。

pom.xml

4.0.0 com.concretepage spring-boot-demo 0.0.1-SNAPSHOT jar spring-demo Spring Boot Demo Project org.springframework.boot spring-boot-starter-parent 1.5.2.RELEASE 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools true org.springframework.boot spring-boot-maven-plugin

找到maven文件中配置的spring boot启动器的描述。

spring-boot-starter-parent : 用于依赖关系管理的父级POM。

spring-boot-starter-web: 构建Web、REST应用程序的启动器。它使用Tomcat服务器作为默认的嵌入式服务器。

spring-boot-devtools : 它提供了开发者工具。这些工具在应用开发模式中很有帮助。开发者工具的一个特点是在代码发生任何变化时自动重新启动服务器。

spring-boot-maven-plugin : 它用于创建应用程序的可执行JAR。

创建 XML 配置

我已经创建了一个XML配置样本。

app-config.xml

在这里我为服务类创建了一个Bean。为了缩进JSON响应,我们配置了Jackson2消息转换器。我们将在我们的spring boot应用程序中使用这个XML配置。

使用 @ImportResource 来导入 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); } } 创建 Service 和 Controller

查找我们示例中使用的服务。

ArticleService.java

package com.concretepage.service; import java.util.ArrayList; import java.util.List; import com.concretepage.entity.Article; public class ArticleService { public List getAllArticles(){ List list = new ArrayList(); 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 getAllArticles() { List list = articleService.getAllArticles(); return list; } } 测试应用程序

找到测试应用程序的步骤。 1. 下载项目的源代码并导入eclipse中。 2. 使用命令提示符进入根文件夹并运行命令。

mvn clean eclipse:eclipse

在eclipse中刷新该项目。现在类路径已经设置好了。 3. 打开MyApplication类,作为java应用程序运行。 4. 访问URL

http://localhost:8080/user/articles

找到输出的打印画面。 在这里插入图片描述

参考文献

【1】Importing XML configuration 【2】Spring Boot XML Configuration Example

源码下载

spring-boot-xml-configuration-example.zip



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3