<springBoot>(三)(入门篇)新建一个springBoot项目(两种启动的方式)

1.1、创建一个Maven工程

假如你现在还在为自己的技术担忧,假如你现在想提升自己的工资,假如你想在职场上获得更多的话语权,假如你想顺利的度过35岁这个魔咒,假如你想体验BAT的工作环境,那么现在请我们一起开启提升技术之旅吧,详情请点击http://106.12.206.16:8080/qingruihappy/index.html

名为”springboot-helloworld” 类型为Jar工程项目

 

 1 <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">
 2   <modelVersion>4.0.0</modelVersion>
 3   <groupId>com.qingruihappy</groupId>
 4   <artifactId>springboot-helloworld</artifactId>
 5   <version>0.0.1-SNAPSHOT</version>
 6   
 7   
 8   
 9   <parent>
10         <groupId>org.springframework.boot</groupId>
11         <artifactId>spring-boot-starter-parent</artifactId>
12         <version>1.3.3.RELEASE</version>
13     </parent>
14 
15     <dependencies>
16       <!--SpringBoot web 组件  --> 
17         <dependency>
18             <groupId>org.springframework.boot</groupId>
19             <artifactId>spring-boot-starter-web</artifactId>
20         </dependency>
21     </dependencies>
22 
23     
24 </project>

 

 

 

spring-boot-starter-parent作用

在pom.xml中引入spring-boot-start-parent,spring官方的解释叫什么stater poms,它可以提供dependency management,也就是说依赖管理,引入以后在申明其它dependency的时候就不需要version了,后面可以看到。

spring-boot-starter-web作用

springweb 核心组件

spring-boot-maven-plugin作用

 如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动的。如果使用maven 的spring-boot:run的话是不需要此配置的。(我在测试的时候,如果不配置下面的plugin也是直接在Main中运行的。)

 

 

 

 

 

@RestController

在上加上RestController 表示修饰该Controller所有的方法返回JSON格式,直接可以编写

Restful接口

@EnableAutoConfiguration

注解:作用在于让 Spring Boot   根据应用所声明的依赖来对 Spring 框架进行自动配置
        这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。由于spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置。

 SpringApplication.run(HelloController.class, args);

   标识为启动类

 

 

启动方式一:

 1 @RestController
 2 @EnableAutoConfiguration
 3 public class HelloController {
 4     @RequestMapping("/hello")
 5     public String index() {
 6         return "Hello World";
 7     }    
 8 public static void main(String[] args) {
 9         SpringApplication.run(HelloController.class, args);
10     }
11 }

启动主程序,打开浏览器访问http://localhost:8080/index,可以看到页面输出Hello World

启动方式二:

 

 1 package com.itmayiedu.app;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 5 import org.springframework.context.annotation.ComponentScan;
 6 
 7 
 8 @ComponentScan(basePackages="com.itmayiedu.contoller")
 9 @EnableAutoConfiguration
10 public class App {
11 
12      public static void main(String[] args) {
13         SpringApplication.run(App.class, args);
14     }
15     
16 }

 

 

 1 package com.itmayiedu.contoller;
 2 
 3 import org.springframework.web.bind.annotation.RequestMapping;
 4 import org.springframework.web.bind.annotation.RestController;
 5 
 6 
 7 @RestController
 8 public class TestController {
 9 
10     @RequestMapping("/hello")
11     public String hello() {
12         return "success";
13     }
14 
15 }

让它去扫描对应的controller下面的类,有点类似于springmvc 的扫描包一样的。。。。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值