springboot的helloworld项目

32 篇文章 0 订阅
31 篇文章 0 订阅

springboot官方文档:https://docs.spring.io/spring-boot/docs/
1.pom.xml配置:

<groupId>com.springboot.helloWorld</groupId>
<artifactId>SpringbootHelloWorld</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>1.5.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

2.编写springboot主程序:
package com.asiainfo.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

  • @author jack

  • @create 2019-07-05 23:00
    /
    /

    @SpringBootApplication:说明这是一个springboot应用
    */
    @SpringBootApplication
    public class SpringbootHelloWorld {

    public static void main(String[] args) {
    //springboot应用启动
    SpringApplication.run(SpringbootHelloWorld.class,args) ;
    }
    }
    3.编写controller:
    注意:controller所在的包必须是主程序所在包的子包,否则报如下错误,
    在这里插入图片描述
    package com.asiainfo.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**

  • @author jack
  • @create 2019-07-05 23:25
    */

@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return “hello World” ;
}
}
4.运行主程序:
编写完成之后只需要运行主程序,springboot会自动启动tomcat的端口,访问方式:
http:localhost:8080/hello即可(不需要书写项目名)
5.简化部署:
在pom.xml中配置springboot打包插件:



org.springframework.boot
spring-boot-maven-plugin



配置完成之后即可,使用maven打包将打包的项目放在了target目录下
在这里插入图片描述
,可将打包的项目复制到本地依然可以访问,此处我将打包后的项目放在桌面,使用dos命令行使用java -jar 命令即可直接访问在这里插入图片描述
为了测试成功,需要先将idea中的程序停止。
springboot在打包时将所有的依赖都变成了jar包一起被打包,因此无需进行启动tomcat等
二.细节解析:
@SpringBootApplication :springboot应用标注在某个类上说明这个类是springboot的主配置类,springboot就应该运行这个类的main方法来启动springboot
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {}
@SpringBootConfiguration:Spring Boot的配置类;

​ 标注在某个类上,表示这是一个Spring Boot的配置类;

​ @Configuration:配置类上来标注这个注解;

​ 配置类 ----- 配置文件;配置类也是容器中的一个组件;@Component
@EnableAutoConfiguration:开启自动配置功能;

​ 以前我们需要配置的东西,Spring Boot帮我们自动配置;@EnableAutoConfiguration告诉SpringBoot开启自动配置功能;这样自动配置才能生效;
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {}
@AutoConfigurationPackage:自动配置包

​ @Import(AutoConfigurationPackages.Registrar.class):

​ Spring的底层注解@Import,给容器中导入一个组件;导入的组件由AutoConfigurationPackages.Registrar.class;

将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器;

​ @Import(EnableAutoConfigurationImportSelector.class);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值