zoukankan      html  css  js  c++  java
  • spring boot学习 ---- spring boot 之注解(持续更新)

    这里介绍spring boot 中一些常用的注解,关于spring boot 的学习建议还是去看官方文档。https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/

    @Controller

    (类)声明控制器,它是springMVC中的控制器,底层是servlet,添加它可以接收前端的请求,返回数据或资源。

    @ResponseBody

    (方法、类)响应json类型的数据。spring boot会自动将对象转成json格式。

    @RestController

    (类)相当于同时添加@ResponseBody和@Controller这两个控制器,被这个声明的控制器告诉Spring将结果字符串直接呈现给调用者。

    @RequestMapping

    (类、方法)提供“路由”信息。它告诉Spring请求路径。

    @EnableAutoConfiguration

    (类)这个注释告诉SpringBoot根据您添加的JAR依赖项“猜测”如何配置Spring。它将会自动配置您的spring boot程序。配置属性 exclude 可以将自动配置移除

    @ComponentScan

    (类)这个注释springBoot会扫描所有的组件,添加这个可以发现托给spring容器管理的类。

    @SpringBootApplication

    (类)通常在启动类上添加这个注解。它包括@EnableAutoConfiguration、@ComponentScan和@Configuration

    ps:相当于@EnableAutoConfiguration和@ComponentScan的行为

    @Configuration

    (类)Spring Boot支持基于Java的配置。尽管与XML源一起使用SpringApplication是可行的,但通常建议您的主源是一个单一的@Configuration类。通常,定义主方法的类是一个很好的候选对象。大多数配置是以Enable开头的开关配置

    @Import

    (类)通常我们不必将所有配置放在一个类中,我们可以通过@Import导入其他配置类,或者使用@ComponentScan自动获取所有Spring组件,包括@Configuration类。

    @ImportResource

    (类)导入XML配置文件。同时这个类需要被声明@Configuration

    @PropertySource

    (类)加载配置文件到项目中。

    @Component @Service @Repository @Controller

    (类)使用这些均是将该类声明成一个组件。程序会将这个组件交给spring的IoC容器来管理这些组件的生命周期。我们可以是用@Autowried来取到被这些组件声明的对象。

    其中component是最原始的组件声明方式,其他的是他的子注解。关于他们的区别请查看这篇博文。

    @Autowried

    (字段、构造方法、方法、注解、参数)被改注解声明的变量将会被自动注入一个对象。

    @Before、@After、@Around、@AfterReturn、@AfterThrowing

    (方法)被这几个注解标记的方法是用在AOP(面向切面编程)中,它配合@Aspect一起使用,@Before(切面之前执行的方法),@After(切面之后执行的方法),@Around(切面之前之后均执行的方法),@AfterReturn(方法返回值后执行),@AfterThrowing(方法抛出错误后执行)

    @PointCut

    (方法)被该注解标记的方法被声明成一个切入点,这样我们可以简化切入点的编写。

    @Aspect

    (类)该注解表示该类是一个能够使用AOP编程方式

    @Value

    (字段,方法,注解,参数)从配置文件中载入数据,也可加载远程配置

    格式:@Value("${xxxx.xxx}")

    @ConfigurationProperties

    (类、方法)加载配置文件,可以批量注入配置到对象中,需要额外添加maven依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    

    使用例子:

    @ConfigurationProperties(prefix="acme")
    @Validated
    public class AcmeProperties {
    
    	@NotNull
    	private InetAddress remoteAddress;
    
    	@Valid
    	private final Security security = new Security();
    
    	// ... getters and setters
    
    	public static class Security {
    
    		@NotEmpty
    		public String username;
    
    		// ... getters and setters
    
    	}
    
    }
    

    @Profile

    (类、方法)spring 多环境配置中指定使用哪个环境的配置,这个可以结合配置文件中的配置使用。

    @JsonComponent

    (类)如果使用Jackson序列化和反序列化JSON数据,可能需要编写自己的JsonSerializer和JsonDeserializer类。定制序列化器通常通过模块向Jackson注册,但是Spring Boot提供了一个替代的@JsonComponent注释,使直接注册Spring bean变得更容易。

    @Bean

    (方法、注解)Spring MVC使用WebBindingInitializer为特定请求初始化WebDataBinder。如果您创建了自己的ConfigurableWebBindingInitializer @Bean, Spring Boot会自动配置Spring MVC来使用它。

    @ControllerAdvice、@ExceptionHandler

    (类)您还可以定义一个用@ControllerAdvice注释的类,以自定义JSON文档以针对特定的控制器和/或异常类型返回

    (方法)@ExceptionHandler

    • 一个Controller下多个@ExceptionHandler上的异常类型不能出现一样的,否则运行时抛异常.
    • 方法返回值类型支持多种,常见的ModelAndView,@ResponseBody注解标注,ResponseEntity等类型都OK.

    例如:

    @ControllerAdvice(basePackageClasses = AcmeController.class)
    public class AcmeControllerAdvice extends ResponseEntityExceptionHandler {
    
    	@ExceptionHandler(YourException.class)
    	@ResponseBody
    	ResponseEntity<?> handleControllerException(HttpServletRequest request, Throwable ex) {
    		HttpStatus status = getStatus(request);
    		return new ResponseEntity<>(new CustomErrorType(status.value(), ex.getMessage()), status);
    	}
    
    	private HttpStatus getStatus(HttpServletRequest request) {
    		Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
    		if (statusCode == null) {
    			return HttpStatus.INTERNAL_SERVER_ERROR;
    		}
    		return HttpStatus.valueOf(statusCode);
    	}
    
    }
    

    @CrossOrigin

    (类、方法)允许跨域,但一般会写成配置文件的格式:

    @Configuration
    public class MyConfiguration {
    
    	@Bean
    	public WebMvcConfigurer corsConfigurer() {
    		return new WebMvcConfigurer() {
    			@Override
    			public void addCorsMappings(CorsRegistry registry) {
    				registry.addMapping("/api/**");
    			}
    		};
    	}
    }
    

    @EnableWebFlux

    (类) 开启webFlux 注意 :webFlux不能与springMVC同时存在

    @Path

    webFlux中的资源访问路径用法相当于@RequestMapping

  • 相关阅读:
    Java线程池
    Servlet实现网页十天免登陆功能
    代码实现QQ消息轰炸
    数组模拟栈数据结构
    约瑟夫问题以及环形链表的解决方案
    ServletConfig中的方法
    Servlet的生命周期
    数组模拟环形队列
    数组模拟队列
    多线程实现奇偶数的依次输出
  • 原文地址:https://www.cnblogs.com/bananafish/p/13288636.html
Copyright © 2011-2022 走看看