1.springboot介绍

来源:互联网 发布:男人眼中的女神 知乎 编辑:程序博客网 时间:2024/06/05 16:54

1.介绍


简单来说springboot最大的感受在于不用写配置

web.xml配置
springmvc配置
不用自己安装启动和配置tomcat,springboot已经内嵌
可以和rest风格的微服务契合,快速开发,依赖和程序全打包,jar包启动等优点
maven项目结构
main/java(这里面放程序,使用app主类启动)
main/resources(这下面放static资源templates资源和application.yml配置文件)
自动引入依赖:springboot自动引入你使用的依赖的依赖




2.helloworld

(1)pom.xml
spring-boot-starter-parent(parent依赖,下面引入的都不需要执行版本)
spring-boot-starter-web
spring-boot-starter-test
spring-boot-maven-plugin(maven打包用的插件)
完整文件如下
<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.tyf</groupId>  <artifactId>hello-world</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>jar</packaging>  <name>hello-world</name>  <url>http://maven.apache.org</url>   <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.2.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <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>            </plugin>        </plugins>    </build>        </project>

(2)app
package com.tyf.hello_world;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.embedded.LocalServerPort;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.Bean;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication   //标注启动类@RestController//标注controllerpublic class App {public static void main(String[] args) throws Exception{        SpringApplication.run(App.class, args);           }@Bean //static方法执行过程中查看context启动引入的bean    public static CommandLineRunner commandLineRunner(ApplicationContext ctx){String[] beanNames = ctx.getBeanDefinitionNames();        for (String beanName : beanNames) {            System.out.println(beanName);        }return null;};    @RequestMapping("/test")public String test(){return "test" ;}    }

启动访问
localhost:8080/test