1、@SpringBootApplication :
之前用户使用的是3个注解注解他们的main类。分别是@Configuration,@EnableAutoConfiguration,@ComponentScan。由于这些注解一般都是一起使用,spring boot提供了一个统一的注解@SpringBootApplication。
1-1:@Configuration:
提到@Configuration就要提到它的搭档@Bean。使用这两个注解就可以创建一个简单的spring配置类,可以用来代替相应的xml配置文件。
相当于:
@Configuration
public class Conf {
@Bean
public Car car() {
Car car = new Car();
car.setWheel(wheel());
return car;
}
@Bean
public Wheel wheel() {
return new Wheel();
}
}
@Configuration public class Conf { @Bean public Car car() { Car car = new Car(); car.setWheel(wheel()); return car; } @Bean public Wheel wheel() { return new Wheel(); } }
@Configuration的注解类标识这个类可以使用Spring Ioc容器作为Bean定义的来源。@Bean注解告诉Spring,一个带有@Bean的注解将返回一个对象,该对象应该被注册在Spring应用程序上下文中的Bean。
1-2:@EnableAutoConfiguration:
能够自动配置Spring的上下文,试图猜测和配置你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置。
1-3:@ComponentScan:
会自动扫描指定包下的全部标有@Component的类,并注册成bean,当然包括@Component下的子注解@Service、@Repository、@Controller。
2、@Repository:
用于标注数据访问组件,即DAO组件。
3、@Service:
用于标注业务层组件。
4、@RestController:
用于标注控制层组件(如struts中的action),包括@Controller和@ResponseBody。
@ResponseBody:
表示该方法的返回结果直接写入HTTP response body中
一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,
加上@Responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。
比如异步获取json数据,加上@Responsebody后,会直接返回json数据。
5、@Controller:
用于标注控制层组件,需要返回页面时请用@Controller而不是@RestController。
6、@RequestMapping:
提供路由信息、负责URL到Controller中的具体函数的映射,该注解有六个属性:
6 - 1:params:
指定request中包括某些参数值,true/false(必须和非必须)。
6 - 2: headers:
指定request中必须包含某些指定的header值,才能让该方法处理请求。
6 - 3 :value:
指定请求的实际路径,指定的地址可以是URI Template模式。
6 - 4:method:
制定请求的method类型,GET、POST、PUT、DELETE等。
6 - 5:consumes:
指定处理请求的提交内容类型(Content-Type),如applocation/json,text/html;
6 - 6:produces:
指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回。
7、@Import:
用来导入其它配置类
8、@ImportResource:
用来加载xml配置文件。
9、@Autowired:
自动导入依赖的Bean。
10、@Value:
注入Springboot application.properties配置的属性的值。
11、@Component:
泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
12、@Qualifier:
当有多个同一类型的Bean时,可以用@Qualifier限定描述符除了能根据名字进行注入,但能进行更细粒度的控制如何选择候选者,具体使用方式如下:
@Autowired @Qualifier(value = “demoInfoService”)
private DemoInfoService demoInfoService;
13、@Resource(name="name",type="type"):
没有括号内容的话,默认byName。与@Autowired干一样的事情。
14、@PathVariable:
路径变量。如
RequestMapping(“user/get/mac/{macAddress}”)
public String getByMacAddress(@PathVariable String macAddress){ //do something; }
参数与大括号里的名字一样要相同。
15、
@ControllerAdvice/@RestControllerAdvice:
包含@Component。可以被扫描到。统一处理异常。
@ExceptionHandler(Exception.class):用在方法上面表示遇到这个异常就执行以下方法。如下:
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ThirdPartyAccessException.class)
public String Exception(HttpServletRequest request, ThirdPartyAccessException e){
String message = e.getMessage();
String exceptionPath = e.getClass().getName();
String id = e.getMessageId();
String ex ="[id:{"+id+"}&message:{"+message+"}&exceptionPath:{"+exceptionPath+"}]";
return ex;
}
@ExceptionHandler(YcsException.class)
public String Exception(HttpServletRequest request, YcsException e){
String message = e.getMessage();
String exceptionPath = e.getClass().getName();
String id = e.getMessageId();
String ex ="[id:{"+id+"}&message:{"+message+"}&exceptionPath:{"+exceptionPath+"}]";
return ex;
}
}