spring boot 常用注解解析

(1)@SpringBootApplication

       申明让spring boot自动给程序进行必要的配置,这个配置等同于:

@Configuration @EnableAutoConfiguration  @ComponentScan 三个配置。


示例代码:

package com.kfit;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication

publicclass App {

    publicstaticvoid main(String[] args) {

       SpringApplication.run(ApiCoreApp.classargs);

    }

}

(2)@ResponseBody

       该注解修饰的函数,会将结果直接填充到HTTP的响应体中,一般用于构建RESTful的api,该注解一般会配合@RequestMapping一起使用。

示例代码:

@RequestMapping("/test")

    @ResponseBody

    public String test(){

       return"ok";

    }

(3)@Controller

       用于定义控制器类,在spring 项目中由控制器负责将用户发来的URL请求转发到对应的服务接口(service层),一般这个注解在类中,通常方法需要配合注解@RequestMapping。

示例代码:

@Controller

@RequestMapping("/demoInfo")

publicclass DemoController {

    @Autowired

    private DemoInfoService demoInfoService;

   

    @RequestMapping("/hello")

    public String hello(Map<String,Object> map){

       System.out.println("DemoController.hello()");

       map.put("hello","from TemplateController.helloHtml");

       //会使用hello.html或者hello.ftl模板进行渲染显示.

       return"/hello";

    }

}

(4)@RestController

       @ResponseBody和@Controller的合集

示例代码:

package com.kfit.demo.web;

 

import org.springframework.web.bind.annotation.RequestMapping;

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

 

@RestController

@RequestMapping("/demoInfo2")

publicclass DemoController2 {

   

    @RequestMapping("/test")

    public String test(){

       return"ok";

    }

}

(5)@RequestMapping

       提供路由信息,负责URL到Controller中的具体函数的映射。

(6)@EnableAutoConfiguration

       Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring应用。例如,如果你的classpath下存在HSQLDB,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库”。你可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration注解的排除属性来禁用它们。

(7)@ComponentScan 

       表示将该类自动发现(扫描)并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。如果没有配置的话,Spring Boot会扫描启动类所在包下以及子包下的使用了@Service,@Repository等注解的类。

(8)@Configuration

       相当于传统的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过@Configuration类作为项目的配置主类——可以使用@ImportResource注解加载xml配置文件。

(9)@Import

       用来导入其他配置类。

(10)@ImportResource

       用来加载xml配置文件。

例如:@ImportResource({"classpath:application1.xml", "classpath:application2.xml"})

(11)@Autowired

       自动导入依赖的bean

(12)@Service

       一般用于修饰service层的组件

(13)@Repository

       使用@Repository注解可以确保DAO或者repositories提供异常转译,这个注解修饰的DAO或者repositories类会被ComponetScan发现并配置,同时也不需要为它们提供XML配置项。

(14)@Bean

       用@Bean标注方法等价于XML中配置的bean。

(15)@Value

       注入Spring boot application.properties配置的属性的值。

示例代码:
@Value(value = "#{message}") 

private String message; 

(16)@Qualifier

       @Qualifier限定描述符除了能根据名字进行注入,但能进行更细粒度的控制如何选择候选者,具体使用方式如下:

   @Autowired

    @Qualifier(value = "demoInfoService"

    private DemoInfoService demoInfoService;

(17)@Inject

       等价于默认的@Autowired,只是没有required属性

(18)@ConfigurationProperties

通过@ConfigurationProperties注解可以将priperties文件中的属性值与Bean中的属性自动关联,实现了属性值自动注入的功能,如在src/main/resources 下新建application.properties文件,内容为

user.username=feinik
user.userpass=123456

测试类

@Component
//通过prefix属性指定properties文件内容前缀
//也可以通过locations属性指定properties文件路径
@ConfigurationProperties(prefix = "user")
public class UserConfig {
   //属性名需与properties中的键值对应
   private String username;
   private String userpass;

   public String getUsername() {
      return username;
   }

   public void setUsername(String username) {
      this.username = username;
   }

   public String getUserpass() {
      return userpass;
   }

   public void setUserpass(String userpass) {
      this.userpass = userpass;
   }
}

(19) @EnableConfigurationProperties

@EnableConfigurationProperties 注解是对 @ConfigurationProperties 的内嵌支持,默认会将指定的class注册为一个Bean,如:

@ConfigurationProperties注解的类

@ConfigurationProperties(prefix = "hello")
public class MyAutoProperties {
   private static final String MSG = "word";
   private String msg = MSG;

   public String getMsg() {
      return msg;
   }

   public void setMsg(String msg) {
      this.msg = msg;
   }
}

在另外一个类中可以通过 @EnableConfigurationProperties(MyAutoProperties.class) 来将 MyAutoProperties注册为一个Bean

(20) 条件注解(条件满足的情况下才会去解析条件注解注解的类)

@ConditionalOnBean 表示容器里存在指定的Bean的条件时

@ConditionalOnClass 表示当类路径下存在指定的类时

@ConditionalOnJava 基于JVM版本作为判断条件

@ConditionalOnMissingBean 当容器中不存在指定的Bean时

@ConditionalOnMissingClass 当类路径下不存在指定的Class时

@ConditionalOnNotWebApplication 当前项目不是web项目的情况下

@ConditionalOnProperty 指定的属性是否有指定的值

@ConditionalOnWebApplication 当前项目是web项目时



猜你喜欢

转载自blog.csdn.net/smithallenyu/article/details/76336537