spring boot 入门

 简介

Spring Boot 是由 Pivotal 团队提供的全新框架。Spring Boot 是所有基于 Spring Framework 5.0 开发的项目的起点。Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置文件。

优点

      1.零配置,无冗余代码生成和XML 强制配置,遵循“约定大于配置,使得开发简单。

      2.提供非功能性特征,如嵌入式服务器、安全性、度量、运行状况检查、外部化配置等

      3.集成了大量常用的第三方库的配置,自动装配。

      4.集成大量的测试框架,测试方便。

缺点

     由于零配置封装了很多细节,所以增大了定位问题的难度。

 

快速入门

1.新建maven项目,项目结构如下

  • 1、Application.java 建议放到根目录下面,主要用于做一些框架配置
  • 2、controller 负责页面访问控制

2.pom文件中引入springboot


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

	<dependencies>
	   <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	   </dependency>
	    <dependency>
	        <groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
	        <scope>test</scope>
	     </dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<fork>true</fork>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

pom.xml 文件中默认有两个模块:

  • spring-boot-starter :核心模块,包括自动配置支持、日志和 YAML,
  • spring-boot-starter-web web 模块提供web应用必要的组件。
  • spring-boot-starter-test :测试模块,包括 JUnit、Hamcrest、MockMvc。

3.编写controller

@RestController
public class HelloWorldController
{
    @RequestMapping("/hello")
    public String index()
    {
        return "Hello World";
    }
}

    @RestController 注解集成了@Controller和@ResponseBody两个注解,返回json数据结构,默认采用的是jackjson。

4. 启动主程序,浏览器中输入http://localhost:8080/hello,可以看到效果。

     

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值