码迷,mamicode.com
首页 > 编程语言 > 详细

12、springboot注解

时间:2017-04-25 00:55:44      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:定义   函数   异常处理   ***   repr   结合   class   handler   product   

@RestController和@Controller

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Component;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
    String value() default "";
}
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
    String value() default "";
}

结论,@RestController是@Controller和@ResponseBody的结合体。

@EnableAutoConfiguration

这个注解是springboot根据添加的jar依赖猜测你想如何配置spring,

你可以通过将 @EnableAutoConfiguration 或 @SpringBootApplication 注解添加到一个 @Configuration 类上来选择自动配置。
注:你只需要添加一个 @EnableAutoConfiguration 注解。我们建议你将它添加到主 @Configuration 类上。

如果发现应用了你不想要的特定自动配置类,你可以使用 @EnableAutoConfiguration 注解的排除属性来禁用它们。

@Configuration

Spring Boot提倡基于Java的配置。尽管你可以使用一个XML源来调用 SpringApplication.run() ,我们通常建议你使用 @Configuration 类作为主要源。一般定义 main 方法的类也是主要 @Configuration 的一个很好候选。你不需要将所有的 @Configuration 放进一个单独的类。 @Import 注解可以用来导入其他配置类。另外,你也可以使用 @ComponentScan 注解自动收集所有的Spring组件,包括 @Configuration 类。

如果你绝对需要使用基于XML的配置,我们建议你仍旧从一个 @Configuration 类开始。你可以使用附加的 @ImportResource 注解加载XML配置文件。

@Configuration注解该类,等价 与XML中配置beans;用@Bean标注方法等价于XML中配置bean

@ComponentScan(basePackages = "com.hyxt",includeFilters = {@ComponentScan.Filter(Aspect.class)}) 

@SpringBootApplication

很多Spring Boot开发者总是使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 注解他们的main类。由于这些注解被如此频繁地一块使用(特别是你遵循以上最佳实践时),Spring Boot提供一个方便的 @SpringBootApplication 选择。
该 @SpringBootApplication 注解等价于以默认属性使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 。

import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan  
public class Application {  
    public static void main(String[] args) {  
        SpringApplication.run(Application.class, args);  
    }  
}  

@ConfigurationProperties

Spring Boot将尝试校验外部的配置,默认使用JSR-303(如果在classpath路径中)。你可以轻松的为你的@ConfigurationProperties类添加JSR-303 javax.validation约束注解:

connection.username=admin 
connection.password=kyjufskifas2jsfs
connection.remoteAddress=192.168.1.1


@Component @ConfigurationProperties(prefix
="connection") public class ConnectionSettings { @NotNull private String username;

private String remoteAddress;

private String password ;
// ... getters and setters }

@Profiles

Spring Profiles提供了一种隔离应用程序配置的方式,并让这些配置只能在特定的环境下生效。任何@Component或@Configuration都能被@Profile标记,从而限制加载它的时机。
@Configuration  
@Profile("production")  
public class ProductionConfiguration {  
// ...  
}  

@ResponseBody

表示该方法的返回结果直接写入HTTP response body中

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

@Component
泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。一般公共的方法我会用上这个注解
@AutoWired
byType方式。把配置好的Bean拿来用,完成属性、方法的组装,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。当加上(required=false)时,就算找不到bean也不报错。

@RequestParam
用在方法的参数前面。

@RequestParam String a =request.getParameter("a")。 

@PathVariable

RequestMapping("user/get/mac/{macAddress}")  
public String getByMacAddress(@PathVariable String macAddress){  //参数与大括号里面一样;  
}  

@ControllerAdvice

全局处理异常的:包含@Component。可以被扫描到。
统一处理异常。

@ExceptionHandler(Exception.class):
用在方法上面表示遇到这个异常就执行以下方法。

/** 
 * 全局异常处理 
 */  
@ControllerAdvice  
class GlobalDefaultExceptionHandler {  
    public static final String DEFAULT_ERROR_VIEW = "error";  
  
    @ExceptionHandler({TypeMismatchException.class,NumberFormatException.class})  
    public ModelAndView formatErrorHandler(HttpServletRequest req, Exception e) throws Exception {  
        ModelAndView mav = new ModelAndView();  
        mav.addObject("error","参数类型错误");  
        mav.addObject("exception", e);  
        mav.addObject("url", RequestUtils.getCompleteRequestUrl(req));  
        mav.addObject("timestamp", new Date());  
        mav.setViewName(DEFAULT_ERROR_VIEW);  
        return mav;  
    }}  

@value

通过@value注解来读取application.properties里面的配置

face_api_key = R9Z3Vxc7ZcxfewgVrjOyrvu1d-qR****  
face_api_secret =D9WUQGCYLvOCIdsbX35uTH******** 


@Value("${face_api_key}")  
   private String API_KEY;  
  
   @Value("${face_api_secret}")  
   private String API_SECRET; 

来源:http://blog.csdn.net/lafengwnagzi/article/details/53034369

 

 

12、springboot注解

标签:定义   函数   异常处理   ***   repr   结合   class   handler   product   

原文地址:http://www.cnblogs.com/gudulijia/p/6759685.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!