spring boot注解

@SpringBootApplication:包含了:

              -----@ComponentScan

              -----@Configuration

              -----@EnableAutoConfiguration

@ComponentScan 组件扫描,可自动发现和装配一些Bean,扫描到有@Component、@Controller、@Service等这些注解的类

@Configuration 等同于spring的XML配置文件;使用Java代码可以检查类型安全。

@EnableAutoConfiguration 自动配置

@RestController注解是@Controller和@ResponseBody的合集,表示这是个控制器bean,并且是将函数的返回值直 接填入HTTP响应体中,是REST风格的控制器。

@ResponseBody:表示该方法的返回结果直接写入HTTP response body中,一般在异步获取数据时使用,用于构建RESTful的api。

在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。比如异步获取json数据,加上@responsebody后,会直接返回json数据。该注解一般会配合@RequestMapping一起使用。示例代码:

@RequestMapping(“/test”) 
@ResponseBody 
public String test(){ 
return”ok”; 
}

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

示例代码:

@Controller 
@RequestMapping(“/demoInfo”) 
public class 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";
}
}

@RestController:用于标注控制层组件(如struts中的action),@ResponseBody和@Controller的合集。

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

@RequestParam:获取url参数中的值,格式为xxx?name=123&pwd=45

   @RequestMapping(value = "/loginbypost", method = RequestMethod.POST)  
    public String loginByPost(@RequestParam(value = "name", required = true) String name,  
            @RequestParam(value = "pwd", required = true) String pwd) {  
        System.out.println("hello post");  
        return login4Return(name, pwd);  
    }  

@RequestBody  

    /** 
     * 请求内容是一个json串,spring会自动把他和我们的参数bean对应起来,不过要加@RequestBody注解 
     *  
     * @param name 
     * @param pwd 
     * @return 
     */  
    @RequestMapping(value = "/loginbypost2", method = { RequestMethod.POST, RequestMethod.GET })  
    public String loginByPost2(@RequestBody RequestLoginBean loginBean) {  
        if (null != loginBean) {  
            return login4Return(loginBean.getName(), loginBean.getPwd());  
        } else {  
            return "error";  
        }  
    }  

    1、@RequestBody需要把所有请求参数作为json解析,因此,不能包含key=value这样的写法在请求url中,所有的请求参数都是一个json

    2、直接通过浏览器输入url时,@RequestBody获取不到json对象,需要用java编程或者基于ajax的方法请求,将Content-Type设置为application/json

@Autowired:自动导入依赖的bean,当加上(required=false)时,就算找不到bean也不报错。

@Resource(name=”name”,type=”type”):没有括号内内容的话,默认byName。与@Autowired干类似的事。

@Service:一般用于修饰service层的组件

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

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

@Value:注入Spring boot application.properties配置的属性的值。示例代码:

@Value(value = “#{message}”) 
private String message;

@Component:用来将某个类声明为组件类达到持久化目的,然后即可使用Autowired进行获取

@Bean:相当于XML中的,放在方法的上面,而不是类,意思是产生一个bean,并交给spring管理。

params:指定request中必须包含某些参数值是,才让该方法处理。 
headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。 
value:指定请求的实际地址,指定的地址可以是URI Template 模式 
method:指定请求的method类型, GET、POST、PUT、DELETE等 
consumes:指定处理请求的提交内容类型(Content-Type),如application/json,text/html; 
produces:指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回

@PathVariable:路径变量,获取路径中的变量值。如

RequestMapping(“user/get/mac/{macAddress}”) 
public String getByMacAddress(@PathVariable String macAddress){ 
//do something; 
} 

 @Conditional 条件化注解,

只有当JdbcTemplateCondition类的条件成立时才会创建MyService这个Bean:

@Conditional(JdbcTemplateCondition.class) 
public MyService myService() { 
... 
}

 

 

 

posted @ 2018-04-03 11:40  林被熊烟岛  阅读(189)  评论(0编辑  收藏  举报