spring boot example

spring Boot dependencies 使用org.springframework.boot groupId,典型的,你的maven pom 文件从spring-boot-starter-parent工程继承,并且声明

依赖关系为一个或多个"starters",下面是一个典型的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.example</groupId>
	<artifactId>myproject</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<!-- 默认从 Spring Boot继承 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.M7</version>
	</parent>

	<!-- Add typical dependencies for a web application 添加一个典型的依赖用于web application -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<!-- Package as an executable jar 可以打包为可执行的jar -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<!-- Add Spring repositories -->
	<!-- (you don't need this if you are using a .RELEASE version) -->
	<repositories>
		<repository>
			<id>spring-snapshots</id>
			<url>http://repo.spring.io/snapshot</url>
			<snapshots><enabled>true</enabled></snapshots>
		</repository>
		<repository>
			<id>spring-milestones</id>
			<url>http://repo.spring.io/milestone</url>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>spring-snapshots</id>
			<url>http://repo.spring.io/snapshot</url>
		</pluginRepository>
		<pluginRepository>
			<id>spring-milestones</id>
			<url>http://repo.spring.io/milestone</url>
		</pluginRepository>
	</pluginRepositories>
</project>
spring-boot-starter-parent是一个主要使用Spring boot 的方式。但是它不可能一直都适用。有时你可能需要从不同的父POM 继承。
Spring Boot提供了很多"Starters"可以添加相应的jar 到你的classpath。spring-boot-starter-parent是一个特殊的starter,它提供了有效的默认Maven,
并且它也提供了dependency-management 所以你就可以省略其他依赖的version 标签。

其他的"Starters"提供了依赖当你开发特殊类型的应用时所需要的。比如如果你要开发一个web 应用,需要添加spring-boot-starter-web 依赖,
<?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.example</groupId>
	<artifactId>myproject</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.M7</version>
	</parent>

	<!-- Additional lines to be added here... -->

	<!-- (you don't need this if you are using a .RELEASE version) -->
	<repositories>
		<repository>
			<id>spring-snapshots</id>
			<url>http://repo.spring.io/snapshot</url>
			<snapshots><enabled>true</enabled></snapshots>
		</repository>
		<repository>
			<id>spring-milestones</id>
			<url>http://repo.spring.io/milestone</url>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>spring-snapshots</id>
			<url>http://repo.spring.io/snapshot</url>
		</pluginRepository>
		<pluginRepository>
			<id>spring-milestones</id>
			<url>http://repo.spring.io/milestone</url>
		</pluginRepository>
	</pluginRepositories>
</project>

上面的pom.xml 使用命令行:mvn dependency:tree 输出一个数代表这个工程依赖
$ mvn dependency:tree

[INFO] com.example:myproject:jar:0.0.1-SNAPSHOT

你可以看到Spring-boot-starter-parent它自己没有提供任何依赖。添加必要的依赖在pom.xml文件中添加spring-boot-starter-web依赖在parent节点下面:
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
</dependencies>
如果你再次执行mvn dependency:tree,你可以看到其他的依赖,包括Tomcat web server 和spring boot本身。
mport org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example {

	@RequestMapping("/")
	String home() {
		return "Hello World!";
	}

	public static void main(String[] args) throws Exception {
		SpringApplication.run(Example.class, args);
	}

}


@RestController 和@RequestMapping注解
在Example中第一个注解是@RestController,这个被看做一个stereotype 注解,它给人们对code 提供了提示,但是在Spring这个class扮演了一个特殊的角色。
在这个例子中,这个类是一个web @Controller,所以当Spring处理web Request 时会考虑到这个类。
@RequestMapping 注解提供了请求路径信息,它告诉Spring任何以"/"为路径的Http 请求都会被映射到home方法。@RestController 注解告诉Spring 直接渲染"Hello World"给调用者。
这两个注解是Spring MVC 注解。

这个类中第二个注解是@EnableAutoConfiguration 注解,这个注解告诉Spring Boot "推测"你通过你已经添加的jar依赖想要如何配置Spring。因为spring-boot-starter-web添加了Tomcat和SpringMVC,自动配置假定你已经开发了一个web应用并且相应的
设置了Spring。
Auto-configuration是用来和"Starters"更好的work 的,但是这两个概念没有直接绑定。可以自由的选择jar依赖,并且Spring boot 对应你的工程仍然是最后的auto-configuration

这个应用的最后一部分是main方法。这仅仅是一个标准的方法,这个方法遵循了java习俗作为一个应用的入口点。这个main方法委托Spring Boot的SpringApplication类调用run 方法。SpringApplication启动这个应用,启动Spring,然后反而启动了auto-configured Tomcat web server.需要传入Example.class作为run方法的参数,告诉SpringApplication这个是Spring重要的构成。args 数组同时通过命令行也传入参数。

猜你喜欢

转载自blog.csdn.net/z278718149/article/details/78866432