一、创建springBoot项目

创建spring项目

已安装maven,配置了jdk
1.idea-setting-plugins导入spring assistant
2.new->spring assistant->https://start.spring.io->输入相关包信息
创建后,会自动导入相关jar包
3.默认生成的springBoot项目,主程序已生成好,只需要编写自己的逻辑

  • resources文件目录结构
    • static:保存所有的静态资源:js css images
    • templates:保存所有的模板页面(spring Boot默认jar使用嵌入式的Tomcat,默认不支持JSP页面);模板引擎(freemarker\thymeleaf)
    • application.properties:spring boot应用的配置文件

主程序类

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
//@SpringBootApplication:标注在类上,表示过个类为springboot的主配置类,springboot就应该运行这个类的main方法来启动spring的应用。
//@SpringBootConfiguration:表示这是一个springBoot的配置类。            springBoot定义的注解
//@Configuration:配置类上来标注这个注解。配置类===配置文件;也是容器中的一个组件@Component。   spring定义的注解
//@EnableAutoConfiguration:springBoot开启自动配置功能。
//@ImportResource(location = {"classpath:beans.xml"})
@SpringBootApplication
@SpringBootConfiguration
@EnableAutoConfiguration
public class Springboot20190521Application {
    public static void main(String[] args) {
        //SpringApplication.run的参数是SpringBootApplication的类
        SpringApplication.run(Springboot20190521Application.class, args);
    }
}

创建自己的类

package com.example.demo.controller;

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

//@ResponseBody这个类的方法的所有方法返回的数据直接写给浏览器,如果是对象转为json数据
//@RestController   = @ResponseBody +  @Controller

//@ResponseBody
//@Controller
@RestController
public class Study01 {
    @ResponseBody
    @RequestMapping("/study")
    public String study(){
        return "hello world";
    }
}

自动配置机制

@EnableAutoConfiguration

@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {

@AutoConfigurationPackage:自动配置包
@Import({AutoConfigurationImportSelector.class}):spring的底层注解@Important,给容器中导入一个组件(AutoConfigurationImportSelector.class来指定哪些组件)。
将主配置类(@SpringBootApplication标注的类)所对应的包及下面的所有组件扫描到spring容器中。
@AutoConfigurationImportSelector:导入哪些组件选择器
public String[] selectImports(AnnotationMetadata annotationMetadata)将所有需要的组件以全类名的方式添加到容器中
会给容器中导入很多的自动配置类(**AutoConfigation)
从 jar包(org.springframe.boot:spring-boot-autoconfigure)类路径下META-INF/springfactories中获取EnableAutoConfig指定的自动化配置。
J2EE的整合解决方案和自动配置都在spring-boot-test-autoconfigure-2.1.5.RELEASE.jar

你可能感兴趣的:(一、创建springBoot项目)