使用Spring Initializr创建Spring Boot项目

目录

 

一、创建项目

二、启动项目

三、创建web项目


 

一、创建项目

依次选择File -> New -> Project,设置好下图两点之后,点击next

使用Spring Initializr创建Spring Boot项目_第1张图片

配置项目信息,点击next

使用Spring Initializr创建Spring Boot项目_第2张图片

配置Spring Boot版本及要引入的组件,这里我暂时不引入任何组件,点击next

使用Spring Initializr创建Spring Boot项目_第3张图片

最后配置项目名称、项目路径,点击finish

使用Spring Initializr创建Spring Boot项目_第4张图片

创建完的项目结构如下图所示,可以看到主要的文件夹是src,src/main/java里面是项目源代码,src/test/java里面是测试代码,两者包结构相对应,这个包结构就是创建项目时配置的com.example.demo。src/main/resources则是资源文件,IDEA自动帮我们创建了一个配置文件application.properties,我们以后创建的静态文件一般也是存放在这里。

使用Spring Initializr创建Spring Boot项目_第5张图片

target目录如下所示:

使用Spring Initializr创建Spring Boot项目_第6张图片

由于我们没有添加任何组件,pom文件如下所示



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.2.RELEASE
         
    
    com.example
    demo_01
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


 

二、启动项目

现在来启动项目,在启动之前,我们在Spring Boot项目的启动类中写上一句打印语句

使用Spring Initializr创建Spring Boot项目_第7张图片

点击运行,结果如下。可以看到程序的运行端口号是7408,打印语句输出了,程序以code 0码退出,此外还可以看到运行时间信息

使用Spring Initializr创建Spring Boot项目_第8张图片

 

三、创建web项目

上面的项目马上就退出了,如果把它转变为web 项目,就可以持续运行。

首先,通过链接  https://mvnrepository.com/search?q=SpringBoot+web  查找并添加web依赖到pom文件

使用Spring Initializr创建Spring Boot项目_第9张图片

        
        
            org.springframework.boot
            spring-boot-starter-web
            2.2.2.RELEASE
        

创建com.example.demo.controller.HelloController

package com.example.demo.controller;

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

//注意,Spring Boot不能用Controller
@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}

注意,就算在不同包下,也不能有同名的类。比如,创建com.springboot.controller.HelloController。则在启动项目时,会报以下错误。

2020-01-05 21:23:26.990  INFO 17712 --- [  restartedMain] com.example.demo.DemoApplication         : Starting DemoApplication on DESKTOP-CUG0QHK with PID 17712 (D:\WorkStations\Projects\SpringBoot\LoadToSpringBoot\demo\target\classes started by Think in D:\WorkStations\Projects\SpringBoot\LoadToSpringBoot\demo)
2020-01-05 21:23:26.993  INFO 17712 --- [  restartedMain] com.example.demo.DemoApplication         : The following profiles are active: test
2020-01-05 21:23:27.052  INFO 17712 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-01-05 21:23:27.052  INFO 17712 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-01-05 21:23:27.681  WARN 17712 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.example.demo.DemoApplication]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'helloController' for bean class [com.springboot.controller.HelloController] conflicts with existing, non-compatible bean definition of same name and class [com.example.demo.controller.HelloController]
2020-01-05 21:23:27.693 ERROR 17712 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.example.demo.DemoApplication]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'helloController' for bean class [com.springboot.controller.HelloController] conflicts with existing, non-compatible bean definition of same name and class [com.example.demo.controller.HelloController]
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:184) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:325) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:242) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:706) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at com.example.demo.DemoApplication.main(DemoApplication.java:16) [classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_231]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_231]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_231]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_231]
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.2.RELEASE.jar:2.2.2.RELEASE]
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'helloController' for bean class [com.springboot.controller.HelloController] conflicts with existing, non-compatible bean definition of same name and class [com.example.demo.controller.HelloController]
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:349) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:287) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:132) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:290) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:202) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:170) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	... 18 common frames omitted


Process finished with exit code 0

另外,由于项目配置的包结构为com.example.demo,启动类就在这个包下面,我们编写的其他所有类也应该放在这个包下面。因为,Spring Boot是以启动类为起点,查找启动类所在的包以及其子包的所有类,并将其注入。如果在别的包下,像上面的com.springboot.controller包下的类在项目启动后就没有创建实例,自然就不能访问里面规定的URL。

当然,也可以指定要扫描的包来使得com.springboot.controller下的包得以注入,像下面的代码我们就通过ComponentScan注解指定扫描整个com包。

package com.example.demo;

import com.example.demo.entity.BookConfigBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
//@ComponentScan(basePackages = {"com.demo.controller"})
@ComponentScan(basePackages = {"com"})
public class DemoApplication {

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

        System.out.println("打印日志...");
    }
}

 

你可能感兴趣的:(Spring,Boot)