SpringBoot2系列教程(一)简介及HelloWorld项目

Spring Boot 2.0.0.RELEASE 需要Java 8 or 9 and Spring Framework 5.0.4.RELEASE 或者更高版本. 支持Maven 3.2+和Gradle 4 版本

#####支持的Servlet容器 Tomcat 8.5、Jetty 9.4、Jetty 9.4

####官方版本迁移wiki https://github.com/spring-projects/spring-boot/wiki

####HelloWorld异同项目分析

######1. 修改 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>springboot-mybatis</groupId>
	<artifactId>springboot-mybatis</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springboot-mybatis</name>
	<description>Demo project for Spring Boot</description>

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

	<!-- Add typical dependencies for a web application -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>5.0.4.RELEASE</version>
		</dependency>
	</dependencies>

	<!-- Package as an executable jar -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

复制代码

######2. 修改启动类

package springbootmybatis.springbootmybatis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class MybatisApplication {

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

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

}

复制代码

######代码中三个注解分析 两个MVC注解:

  1. @RestController 是Spring处理web请求时用到的注解。告诉Spring将结果字符串直接呈现给调用者。
  2. @RequestMapping 提供了“路由”信息,将/请求映射给home()方法。

SpringBoot注解: 3. @EnableAutoConfiguration 这个注解告诉Springboot 根据你添加的jar包来猜测配置Spring 。@SpringBootApplication一样,也可以使用exclude属性来禁用不需要自动配置的应用。例子:@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) 那么,它和@SpringBootApplication注解有什么关系呢? ######我们来看下@SpringBootApplication注解源码

所以,@SpringBootApplication注释是相当于使用@Configuration, @EnableAutoConfiguration以及@ComponentScan与他们的 默认属性

通俗的讲:@SpringBootApplication = @Configuration+@EnableAutoConfiguration+@ComponentScan 。前提是默认配置,当然,如果你想要覆盖默认配置,你就需要重写该注解了,重写也很简单,给注解加上参数就好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值