Springboot(一)-IDEA搭建springboot项目(demo)

jdk版本:1.8.0_162

1.打开IDEA-file-new-project-Spring Initializer,JDK和URL选默认,next

(这一步如果是不能联网的话,可以选择直接创建maven项目)

2.project metadata --默认

3.dependencies选 web-web(如有其他需要可自行按需选择)

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.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.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>
View Code

4.项目搭建完成后的目录结构

DemoApplication.java 是启动类

 application.yml 是项目配置文件,放在resources目录下 

5.DemoApplication.java

主要通过@SringBootApplication标示启动类,相当于 @Configuration,@EnableAutoConfiguration,@ComponentScan

@Configuration的注解类标示这个类可以使用Spring IoC容器作为bean定义的来源。在类中使用@Bean注解告诉Spring,一个带有@Bean的注解方法将返回一个对象,该对象应该被注册为在Spring应用程序上下文中的bean。

@EnableAutoConfiguration:能够自动配置spring的上下文,试图猜测和配置你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置。

@ComponentScan:会自动扫描指定包下的全部标有@Component的类,并注册成bean,包括@Component下的子注解@Service,@Repository,@Controller。

6.run-》DemoApplication.java即可以启动服务

原文地址:https://www.cnblogs.com/cici20166/p/8697757.html