1:@SpringBootApplication:
包含@Configuration、@EnableAutoConfiguration、@ComponentScan通常用在主类上;

@SpringBootApplication: Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,  SpringBoot 就应该运行这个类的main方法来启动SpringBoot应用;

例子:

@SpringBootApplication
public class Application {
	
	public static void main(String[] args) { 
		SpringApplication.run(Application.class,args); 
	}
	
}

2@Service

用于标注业务层组件;

使用:

在applicationContext.xml文件中加一行:

<context:component-scan base-package="com.hzhi.clas"/>

加上这一行以后,将自动扫描路径下面的包,如果一个类带了@Service注解,将自动注册到Spring容器,不需要再在applicationContext.xml文件定义bean了,类似的还包括@Component、@Repository、@Controller。


3@RestController:
用于标注控制层组件(如struts中的action),包含@Controller和@ResponseBody;

@Controller:
用于标注是控制层组件,需要返回页面时请用@Controller而不是@RestController;

package net.biancheng.controller;
import org.springframework.stereotype.Controller;
@Controller
public class IndexController {
    // 处理请求的方法
}

区别:

 @Controller:  标识一个Spring类是Spring MVC controller处理器 @RestController:   @RestController是@Controller和@ResponseBody的结合体,两个标注合并起来的作用。

@Conroller类中的方法可以直接通过返回String跳转到jsp、ftl、html等模版页面。在方法上加@ResponseBody注解,也可以返回实体对象

@RestController类中的所有方法只能返回String、Object、Json等实体对象,不能跳转到模版页面。

4:@RequestMapping

一个控制器内有多个处理请求的方法,如 UserController 里通常有增加用户、修改用户信息、删除指定用户、根据条件获取用户列表等。每个方法负责不同的请求操作,而 @RequestMapping 就负责将请求映射到对应的控制器方法上。  在基于注解的控制器类中可以为每个请求编写对应的处理方法。使用 @RequestMapping 注解将请求与处理方法一 一对应即可。  @RequestMapping 注解可用于类或方法上。用于类上,表示类中的所有响应请求的方法都以该地址作为父路径。  @RequestMapping 注解常用属性如下。

1. value 属性 value 属性是 @RequestMapping 注解的默认属性,因此如果只有 value 属性时,可以省略该属性名,如果有其它属性,则必须写上 value 属性名称。如下。 @RequestMapping(value="toUser") @RequestMapping("toUser") value 属性支持通配符匹配,如 @RequestMapping(value="toUser/*") 表示 http://localhost:8080/toUser/1 或 http://localhost:8080/toUser/hahaha 都能够正常访问。

2. path属性 path 属性和 value 属性都用来作为映射使用。即 @RequestMapping(value="toUser") 和 @RequestMapping(path="toUser") 都能访问 toUser() 方法。  path 属性支持通配符匹配,如 @RequestMapping(path="toUser/*") 表示 http://localhost:8080/toUser/1 或 http://localhost:8080/toUser/hahaha 都能够正常访问。

3. name属性 name属性相当于方法的注释,使方法更易理解。如 @RequestMapping(value = "toUser",name = "获取用户信息")。

4. method属性 method 属性用于表示该方法支持哪些 HTTP 请求。如果省略 method 属性,则说明该方法支持全部的 HTTP 请求。  @RequestMapping(value = "toUser",method = RequestMethod.GET) 表示该方法只支持 GET 请求。也可指定多个 HTTP 请求,如 @RequestMapping(value = "toUser",method = {RequestMethod.GET,RequestMethod.POST}),说明该方法同时支持 GET 和 POST 请求。

5. params属性 params 属性用于指定请求中规定的参数,代码如下。 @RequestMapping(value = "toUser",params = "type") public String toUser() {          return "showUser"; } 以上代码表示请求中必须包含 type 参数时才能执行该请求。即 http://localhost:8080/toUser?type=xxx 能够正常访问 toUser() 方法,而 http://localhost:8080/toUser 则不能正常访问 toUser() 方法。 @RequestMapping(value = "toUser",params = "type=1") public String toUser() {          return "showUser"; } 以上代码表示请求中必须包含 type 参数,且 type 参数为 1 时才能够执行该请求。即 http://localhost:8080/toUser?type=1 能够正常访问 toUser() 方法,而 http://localhost:8080/toUser?type=2 则不能正常访问 toUser() 方法。

6. header属性 header 属性表示请求中必须包含某些指定的 header 值。  @RequestMapping(value = "toUser",headers = "Referer=http://www.xxx") 表示请求的 header 中必须包含了指定的“Referer”请求头,以及值为“http://www.xxx”时,才能执行该请求。

7. consumers属性 consumers 属性用于指定处理请求的提交内容类型(Content-Type),例如:application/json、text/html。如  @RequestMapping(value = "toUser",consumes = "application/json")。

8. produces属性 produces 属性用于指定返回的内容类型,返回的内容类型必须是 request 请求头(Accept)中所包含的类型。如 @RequestMapping(value = "toUser",produces = "application/json")。  除此之外,produces 属性还可以指定返回值的编码。如 @RequestMapping(value = "toUser",produces = "application/json,charset=utf-8"),表示返回 utf-8 编码。

更多推荐

springboot常用的4个注解