03 spring boot入门——常用的注解配置讲解

要掌握的知识点

1、@RestController and @RequestMapping(springMVC的注解,不是springboot特有的);

2、@RestController = @Controller+@ResponseBody ;

3、@SpringBootApplication = @Configuration+@EnableAutoConfiguration+@ComponentScan。

简要说明

        以上列出的几个注解配置是我们在spring boot开发中经常需要的,所以在这篇文章中做一下简要的介绍,通过本文希望大家对这几个注解有一个大概的了解,能分清楚几个注解之间的关系,在开发中合理使用。

注解介绍

1、@Controller/@ResponseBody/@RestController/@RequestMapping介绍

        @RestController是spring4之后新加入的一个注解,它是@Controller和@ResponseBody两个注解的组合,所以,在我们开发时,如果配置了@RestController这个注解,那其他的这两个注解就不用配置了,如下的两段代码所示,他们是等价的:

  • 配置@RestController注解的代码
@RestController
public class HelloController {

    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}
  • 配置@Controller和@ResponseBody注解的代码
@Controller
@ResponseBody
public class HelloController {

    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}

        以上两段代码除了配置的注解不同之外,其余都一样,代码运行结果相同。

        @RequestMapping这个注解是用来配置url映射的。此注解即可以作用在控制器的某个方法上,也可以作用在此控制器类上。如下:

  • @RequestMapping注解作用在处理器方法上
@RestController
public class HelloController {

    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}

        上述代码中sayHello这个方法所响应的url=localhost:8080/hello。

  • @RequestMapping注解作用在类级别上
@Controller
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping(method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}

        上述代码中sayHello这个方法所响应的url=localhost:8080/hello,和第一个没有区别。

  • @RequestMapping注解作用在类级别和处理器方法上
@RestController
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping(value="/sayHello",method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
    @RequestMapping(value="/sayHi",method= RequestMethod.GET)
    public String sayHi(){
        return "hi";
    }
}

        上述代码中的sayHello所响应的url=localhost:8080/hello/sayHello。sayHi所响应的url=localhost:8080/hello/sayHi。

        所以从这两个方法所响应的url可以看到的是:当控制器在类级别上添加@RequestMapping注解时,这个注解会应用到控制器的所有处理器方法上。处理器方法上的@RequestMapping注解会对类级别上的@RequestMapping的声明进行补充。          @RequestMapping中的method参数有很多中选择,一般使用get/post。

2、@SpringBootApplication注解介绍

        @SpringBootApplication注解是spring boot项目入口类文件中的一个注解,我们先看一下示例代码:

@RestController
@SpringBootApplication
public class Ch522Application {
 
    @RequestMapping("/")
    String index() {
        return "Hello Spring Boot";
        //return "book name is:" + bookName + " and book author is:" + bookAuthor;
    }
 
    public static void main(String[] args) {
        SpringApplication.run(Ch522Application.class, args);
    }
}

        从上述代码可以得知:Spring Boot通常有一个名为*Application的入口类,入口类有一个main方法,这个main方法就是一个标准的Java应用的入口方法;在main方法中使用 SpringApplication.run(XXXApplication.class, args),启动Spring Boot应用项目。

        通过以上的描述,我们对此注解的作用有了一个了解,接下来我们看一下它的源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {
 
    /**
     * Exclude specific auto-configuration classes such that they will never be applied.
     * @return the classes to exclude
     */
    Class<?>[] exclude() default {};
 
    /**
     * Exclude specific auto-configuration class names such that they will never be
     * applied.
     * @return the class names to exclude
     * @since 1.3.0
     */
    String[] excludeName() default {};
 
    /**
     * Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}
     * for a type-safe alternative to String-based package names.
     * @return base packages to scan
     * @since 1.3.0
     */
    @AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
    String[] scanBasePackages() default {};
 
    /**
     * Type-safe alternative to {@link #scanBasePackages} for specifying the packages to
     * scan for annotated components. The package of each class specified will be scanned.
     * <p>
     * Consider creating a special no-op marker class or interface in each package that
     * serves no purpose other than being referenced by this attribute.
     * @return base packages to scan
     * @since 1.3.0
     */
    @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
    Class<?>[] scanBasePackageClasses() default {};
 }

        从源码我们可以看出@SpringBootApplication注解组合了@Configuration、@EnableAutoConfiguration、@ComponentScan这三个注解;若不使用@SpringBootApplication注解,则可以在入口类上直接使用@Configuration、@EnableAutoConfiguration、@ComponentScan这三个注解。

        接下来简要介绍一下这三个注解的作用:

        @EnableAutoConfiguration让Spring Boot根据类路径的jar包依赖为当前项目进行自动化配置。例如:添加了spring-boot-starter-web依赖,会自动添加Tomcat和Spring MVC的依赖,那么Spring Boot会对Tomcat和Spring MVC进行自动配置。

        Spring Boot会自动扫描@SpringBootApplication所在类同级包以及下级包里的Bean。建议入口类放置的位置在groupId+arctifactID组合的包名下。

        @SpringBootConfiguration:这是Spring Boot项目的配置注解,这也是一个组合注解,在Spring Boot项目中推荐使用@ SpringBootConfiguration替代@Configuration。

        @ComponentScan:默认扫描@SpringBootApplication所在类的同级目录以及它的子目录。

总结

        以上通过代码和介绍,简单讲解了一下spring boot开发中经常遇到的几个注解,简单介绍了它们的作用和之间的基本关系,本文不要求大家立刻掌握其中的运行原理和源码阅读,只需要有一个大概的印象即可,后续我们在开发中循序渐进的去了解各个注解的作用及配置技巧。

原文地址:https://www.cnblogs.com/xuqw/p/11794618.html