Springboot学习教程(二) IDEA创建springboot项目

目录

1.IDEA创建springboot项目

2.注解介绍


 

 

1.IDEA创建springboot项目

File ==> New ==> Project

 

Spring Initializr ==> next

 

编辑项目信息 ==> next

注意:打包类型必须为jar类型,且jdk必须在1.8及以上

注意:Artifact不可以大小写混合,否则会报错

 

Web ==> Spring Web ==> next

项目创建成功!

注:第一次创建springboot项目导包需要时间可能会长一些

 

自动生成的pom文件:

spring-boot-starter-parent依赖,集成了很多常用jar包,可以点击进去查看

spring-boot-starter-web依赖,springboot的web组件集成了springmvc   

spring-boot-starter-test依赖,测试依赖

 

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--spring-boot-starter-parent 整合第三方常用框架依赖信息(各种依赖信息)-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.itmayiedu</groupId>
    <artifactId>springboot-helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-helloworld</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--spring-boot-starter-web 是springboot整合springmvc的web组件,实现原理:maven依赖继承关系-->
        <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>

 

程序自动生成的SpringbootdemoApplication,会有一个@SpringBootApplication的注解,这个注解用来标明这个类是程序的入口

注:不是说springboot的入口类必须叫SpringbootdemoApplication,重点是@SpringBootApplication注解和main方法里面的启动代码,入口类名任意。

 

我们在springboothelloworld目录下创建一个controller类

package com.itmayiedu.springboothelloworld;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

    @RequestMapping("/hello")
    public String hello(){
        return "第一个springboot项目";
    }
}

 

启动SpringbootdemoApplication类:

 

启动成功!

注:我们可以从控制台日志中看到Tomcat的默认端口号是8080

 

访问成功

 

2.注解介绍:

HelloWorldController类:

package com.itmayiedu.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

    @RequestMapping("/hello")
    public String helloworld(){
        return "我的第一个springboot项目";
    }
}

@RestController注解:

此注解是spring提供的注解,不是springboot提供的。

@RestController注解相当于@Controller + @Responsebody,即标注此controller类的所有方法返回json格式。

我们可以看@RestController注解源码:

 

SpringbootApplication类:

package com.itmayiedu;

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

@SpringBootApplication
public class SpringbootApplication {

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

@SpringBootApplication注解:

@SpringBootApplication开启了Spring的组件扫描和springboot的自动配置功能,相当于将以下三个注解组合在了一起

(1)@Configuration:表名该类使用基于Java的配置,将此类作为配置类

(2)@ComponentScan:启用注解扫描

(3)@EnableAutoConfiguration:开启springboot的自动配置功能

 

这三个注解也分别可以和@SpringBootApplication同时使用,表示新的注解配置参数覆盖默认的注解配置。

比如我们可以同时使用 @ComponentScan(basePackages = "com.itmayiedu.controller"),表示使用新的扫包范围覆盖默认的@ComponentScan 扫包范围。默认的扫包范围是类当前目录以下和同级目录及以下范围。

 

注意:@EnableAutoConfiguration只能扫描SpringbootdemoApplication类的同级目录及其下的文件。

以我们的项目为例,也就是说我们自己创建的HelloWorldController类,只能和SpringbootdemoApplication类同级

或者把SpringbootdemoApplication类放到itmayidu目录下, HelloWorldController类放到springboothelloworld目录下,

此时SpringbootdemoApplication类与springboothelloworld目录同级,就可以扫描到springboothelloworld目录下的文件了。

以下这种情况,是不能访问成功的!!!

HelloWorldController类springboothelloworld目录同级,@EnableAutoConfiguration注解不能扫描到SpringbootdemoApplication类的上一级目录的同级目录的。

总之,@EnableAutoConfiguration只能扫描SpringbootdemoApplication类的同级目录及其下的文件。

 

所以,我建议创建项目时,将SpringbootdemoApplication类放到itmayiedu目录下,然后创建的同级目录controller包,services包,dao包等目录下的类都可以被扫描到。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值