Spring 、Spring Boot常用注解

1、@Service用于标注业务层组件
2、@Controller用于标注控制层组件(如struts中的action)
3、@Repository用于标注数据访问组件,即DAO组件.
4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
5、@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法,默认按类型装配
      当接口存在两个实现类的时候必须使用@Qualifier指定注入哪个实现类,否则可以省略。
  如:
@Autowired  
@Qualifier("chinese")  
private Man man; 
6、@Qualifier注解了,qualifier的意思是合格者,通过这个标示,表明了哪个实现类才是我们所需要的,我们修改调用代码,添加@Qualifier注解,需要注意的是@Qualifier的参数名称必须为我们之前定义@Service注解的名称之一!
7、@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan;通常作为主类的注解。
8、@Bean注解告诉Spring,一个带有@Bean的注解方法将返回一个对象,该对象应该被注册为在Spring应用程序上下文中的bean。
9、@Configuration标识该类为Spring的配置类,该类可以使用Spring IoC容器作为bean定义的来源;替换Spring的xml配置文件;该注解与@Bean注解配合使用;

@Configuration public class Conf { @Bean public Car car() { Car car = new Car(); car.setWheel(wheel()); return car; } @Bean public Wheel wheel() { return new Wheel(); } }

10、@ResponseBody 用该注解修饰的函数,会将结果直接填充到HTTP的响应体中,一般用于构建RESTful web services的api;

11、@RestController = @ResponseBody+@Controller;

12、@RequestMapping 注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上。

@RestController  
@RequestMapping("/home")  
public class IndexController {  
    @RequestMapping("/get")  
    String get() {  
        //mapped to hostname:port/home/  
        return "Hello from get";  
    }  
    @RequestMapping("/index")  
    String index() {  
        //mapped to hostname:port/home/index/  
        return "Hello from index";  
    }  
}  

多个请求映射到一个方法上去,如下:

@RestController
@RequestMapping("/home")
public class IndexController {

    @RequestMapping(value = {
        "",
        "/page",
        "page*",
        "view/*,**/msg"
    })
    String indexMultipleMapping() {
        return "Hello from index multiple mapping.";
    }
}

@RequestMapping 注解能够处理 HTTP 请求的方法, 比如 GET, PUT, POST, DELETE 以及 PATCH。指定请求映射到一个特定的 HTTP 方法,需要在 @RequestMapping 中使用 method 来声明 HTTP 请求所使用的方法类型,如下:

@RestController  
@RequestMapping("/home")  
public class IndexController {  
    @RequestMapping(method = RequestMethod.GET)  
    String get() {  
        return "Hello from get";  
    }  
    @RequestMapping(method = RequestMethod.DELETE)  
    String delete() {  
        return "Hello from delete";  
    }  
    @RequestMapping(method = RequestMethod.POST)  
    String post() {  
        return "Hello from post";  
    }  
    @RequestMapping(method = RequestMethod.PUT)  
    String put() {  
        return "Hello from put";  
    }  
    @RequestMapping(method = RequestMethod.PATCH)  
    String patch() {  
        return "Hello from patch";  
    }  
}  

备注:该注解详细介绍,参考:http://www.cnblogs.com/DreamScience/p/8947961.html

13、@RequestParam 注解指定了需要被映射到处理方法参数的请求参数;配合 @RequestMapping 一起使用,可以将请求的参数同处理方法的参数绑定在一起。

@RestController  
@RequestMapping("/home")  
public class IndexController {  
  
    @RequestMapping(value = "/id")  
    String getIdByValue(@RequestParam("id") String personId) {  
        System.out.println("ID is " + personId);  
        return "Get ID from query string of URL with value element";  
    }  
    @RequestMapping(value = "/personId")  
    String getId(@RequestParam String personId) {  
        System.out.println("ID is " + personId);  
        return "Get ID from query string of URL without value element";  
    }  
}  

@RequestParam 注解的 required 这个参数定义了参数值是否是必须要传的

@RestController  
@RequestMapping("/home")  
public class IndexController {  
    @RequestMapping(value = "/name")  
    String getName(@RequestParam(value = "person", required = false) String personName) {  
        return "Required element of request param";  
    }  
}  

@RequestParam 的 defaultValue 取值就是用来给取值为空的请求参数提供一个默认值的。

@RestController
@RequestMapping("/home")
public class IndexController {
    @RequestMapping(value = "/name")
    String getName(@RequestParam(value = "person", defaultValue = "John") String personName) {
        return "Required element of request param";
    }
}

14、@EnableAutoConfiguration 自动配置,这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置。

15、@ComponentScan 自动扫描@Component, @Controller, @Service, and @Repository注解组件类装入Spring的bean容器。

7、@Primary的意思是在众多相同的bean中,优先使用用@Primary注解的bean.
8、@Resource 是JDK1.6支持的注解默认按照名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名,按照名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
9、@Inject 这是jsr330中的规范,通过‘AutowiredAnnotationBeanPostProcessor’ 类实现的依赖注入。