Spring Boot XML配置示例

x33g5p2x  于2022-09-19 转载在 Spring  
字(6.1k)|赞(0)|评价(0)|浏览(353)

在本文中,我们将提供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。寻找完整的例子,一步一步来。

使用的软件

在我们的例子中,我们使用了以下软件。

  1. Java 8
  2. Spring Boot 1.5.2.RELEASE
  3. Maven 3.3
  4. Eclipse Mars

Eclipse中的项目结构

在eclipse中查找项目结构。

Maven文件

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

<?xml version="1.0" encoding="UTF-8"?>
&ltproject 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">
	
	&ltmodelVersion&gt4.0.0</modelVersion>
	&ltgroupId&gtcom.concretepage</groupId>
	&ltartifactId&gtspring-boot-demo</artifactId>
	&ltversion&gt0.0.1-SNAPSHOT</version>
	&ltpackaging&gtjar</packaging>
	&ltname&gtspring-demo</name>
	&ltdescription&gtSpring Boot Demo Project</description>
	&ltparent>
		&ltgroupId&gtorg.springframework.boot</groupId>
		&ltartifactId&gtspring-boot-starter-parent</artifactId>
		&ltversion&gt1.5.2.RELEASE</version>
	</parent>
	&ltproperties>
		&ltjava.version&gt1.8</java.version>
	</properties>
	&ltdependencies>
	   &ltdependency>
		&ltgroupId&gtorg.springframework.boot</groupId>
		&ltartifactId&gtspring-boot-starter-web</artifactId>
	   </dependency>
    	   &ltdependency>
                &ltgroupId&gtorg.springframework.boot</groupId>
                &ltartifactId&gtspring-boot-devtools</artifactId>
                &ltoptional&gttrue</optional>
           </dependency> 
	</dependencies> 
	&ltbuild>
	   &ltplugins>
		&ltplugin>
			&ltgroupId&gtorg.springframework.boot</groupId>
			&ltartifactId&gtspring-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配置

我已经创建了一个XML配置样本。
app-config.xml

<?xml version="1.0" encoding="UTF-8"?>
&ltbeans 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"> 
        
        &ltbean class="com.concretepage.service.ArticleService"/>
	&ltbean name="jackson2ObjectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
	    &ltproperty name="indentOutput" value="true"/>
	</bean>    
	&ltmvc:annotation-driven>
	    &ltmvc:message-converters>
	        &ltbean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
	            &ltproperty name="objectMapper" ref="jackson2ObjectMapper" />
	        </bean>
	    </mvc:message-converters>
	</mvc:annotation-driven>
</beans>

在这里我为服务类创建了一个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);
        }       
}

创建服务和控制器

找到我们例子中使用的服务。
ArticleService.java

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

测试应用程序

找到测试应用程序的步骤。

  1. 下载项目源代码并导入eclipse中。
  2. 使用命令提示符进入根文件夹并运行命令
mvn clean eclipse:eclipse

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

http://localhost:8080/user/articles

找到输出的打印屏幕。

相关文章

微信公众号

最新文章

更多