代码改变世界

SpringBoot学习一

2019-05-11 16:14  阿方技术圈  阅读(20)  评论(0)    收藏  举报

一   首先创建Maven工程,创建后的项目目录结构如图所示

再对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>com.jxd.example</groupId>
    <artifactId>programer</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--引入父類依賴-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>
    <!--引入web需要的依賴-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!--添加可以執行jar文件的maven插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

创建主程序执行类

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

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

  

注解说明:@SpringBootApplication:  SpringBoot应用标注在某个类上说明这个类是SpringBoot主配置类,SpringBoot就应该运行这个main方法启动SpringBoot应用
@SpringBootConfiguration:SpringBoot的配置类
@EnableAutoConfiguration:开启自动配置功能,以前需要配置的东西会自动配置
@ComponentScan:
 

编写Controller类

package com.jxd.controller;

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

@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "世界你好!";
    }
}

在浏览器输入网址http://localhost:8080/hello