idea+springboot helloworld入门

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/myvanguard/article/details/84798092

1.开发环境准备

  • IntelliJ IDEA 2018.1.4 x64
  • apache-maven-3.3.9
  • jdk1.8.0_92
  • 2.1.1.RELEASE

idea maven设置 采取默认配置

2.创建springboot项目

3.启动springboot项目

忽视

package com.springboot;

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

@SpringBootApplication
public class DemoApplication {

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

4.编写相关的Controller、Service 并测试##


package com.springboot.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 "Hello World!";
    }
}


5.解析springboot helloworld 项目


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<!--依赖于下面的项目 该项目包含了很多配置 所以不需要手动配置-->

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.1.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
</parent>


<!--spring-boot-starter是场景启动器 下面的依赖导入了web所需要的模块-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>



/**
 * @SpringBootApplication 用来标注一个主程序 说明这是一个springboot应用
 */
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
		//启动springboot程序
        SpringApplication.run(DemoApplication.class, args);
    }
}



@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 是springboot的配置类
  • 配置类 本质上是配置文件,配置类是容器的一个组件
  • @EnableAutoConfiguration 开启字段注解功能 会自动引入很多注解


@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {


6.项目打包编译


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.springboot.DemoApplication</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>


Building jar: D:\sdk\IdeaProjects\springboot\target\demo-0.0.1-SNAPSHOT.jar

可以使用java -jar命令运行 该项目

Exception in thread “main" java.lang.UnsupportedClassVersionError

说明项目的jdk 与 系统配置的jdk版本不一致

猜你喜欢

转载自blog.csdn.net/myvanguard/article/details/84798092