Spring Boot系列笔记--HelloWorld

一、编写helloworld过程

  1. 创建maven工程(jar)
  2. 导入springboot相关依赖
    <parent>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-parent</artifactId>
    	<version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
  3. 编写主程序
    /**
     *  @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
     */
    @SpringBootApplication
    public class HelloWorldMainApplication {
          
          
        public static void main(String[] args) {
          
          
            // Spring应用启动起来
            SpringApplication.run(HelloWorldMainApplication.class,args);
        }
    }
    
  4. 编写相关controller、service
    @Controller
    public class HelloController {
          
          
        @ResponseBody
        @RequestMapping("/hello")
        public String hello(){
          
          
            return "Hello World!";
        }
    }
    
  5. 运行
  6. 简化部署
    <!-- 这个插件,可以将应用打包成一个可执行的jar包;-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    

二、注解细节

  1. @SpringBootApplication
    进入该注解:
    @Target({
          
          ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    
    /**
    *	@SpringBootConfiguration : Spring Boot的配置类
    */
    @SpringBootConfiguration
    
    /**
    *	@EnableAutoConfiguration : 开启自动配置功能
    */
    @EnableAutoConfiguration
    @ComponentScan(
        excludeFilters = {
          
          @Filter(
        type = FilterType.CUSTOM,
        classes = {
          
          TypeExcludeFilter.class}
    ), @Filter(
        type = FilterType.CUSTOM,
        classes = {
          
          AutoConfigurationExcludeFilter.class}
    )}
    )
    
    进入@EnableAutoConfiguration注解:
    @AutoConfigurationPackage	//自动配置包
    @Import({
          
          AutoConfigurationImportSelector.class})
    
    @Import注解将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所 有组件扫描到Spring容器
  2. @RestController
    该注解是@ResponseBody和@Controller的结合体

猜你喜欢

转载自blog.csdn.net/weixin_44863537/article/details/108966908