zoukankan      html  css  js  c++  java
  • SpringBoot进阶教程(六十四)注解大全

    在Spring1.x时代,还没出现注解,需要大量xml配置文件并在内部编写大量bean标签。Java5推出新特性annotation,为spring的更新奠定了基础。从Spring 2.X开始spring将xml配置中的对象ioc过程转化成了注解。Spring Boot之所以能够轻松地实现应用的创建及与其他框架快速集成,最核心的原因就在于它极大地简化了项目的配置,最大化地实现了“约定大于配置”的原则。但是注解种类之繁多,还能容易引起混淆,这才有了本文《SpringBoot进阶教程(六十四)注解大全》。

    要想对SpringBoot注解有个更全面更清晰的认识,就需要分个类,分别是Spring注解、Spring Web注解、Spring Boot注解、Spring Scheduling注解和注解集合。大致可以将注解分为5大类,其中前4类是为了便于理解,分别从4个类别中抽取了一些单独介绍。而最后一个为注解集合,即可能会包含前面4种注解。

    vSpring注解

    在Spring Core注解中,主要讨论Spring DI和Spring IOC中使用的Spring核心注释。众所周知,Spring DI和Spring IOC是Spring框架的核心概念。所以介绍 org.springframework.beans.factory.annotation org.springframework.context.annotation 包中的注解。这两个包中注解有很多,就抽取其中的15个注解。

    Spring Core Annotations:

    • @Autowired
    • @Qualifier
    • @Bean
    • @Required
    • @Value
    • @DependsOn
    • @Lazy
    • @Lookup
    • @Primary
    • @Scope
    • @Profile
    • @Import
    • @ImportResource
    • @PropertySource
    • @PropertySources

    单单 org.springframework.context.annotation 这个包下面,注解就有这老些,所以很难列出所有注解举例,只能抽一些常用的。文末会给出其它注解的作用和定义(尽量给全)。

    1.1 @Autowired

    @Autowired是一种注解,可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作,@Autowired标注可以放在成员变量上,也可以放在成员变量的set方法上,也可以放在任意方法上表示,自动执行当前方法,如果方法有参数,会在IOC容器中自动寻找同类型参数为其传值。

    这里必须明确:@Autowired是根据类型进行自动装配的,如果需要按名称进行装配,则需要配合@Qualifier使用;

    1.1.1 构造器注入

    @RestController
    public class UserController {
        private UserService userService;
     
        @Autowired
        public UserController(UserService userService) {
            this.userService = userService;
        }
    }

    1.1.2 setter方法注入

    @RestController
    public class UserController {
        private UserService userService;
     
        @Autowired
        public void setUserService(UserService userService) {
            this.userService = userService;
        }
    }

    1.1.3 field反射注入

    @RestController
    public class UserController {
        @Autowired
        private UserService userService;
    }

    1.2 @Qualifier

    上面已经说到@Autowired按类型装配Spring Bean。如果容器中有多个相同类型的bean,则框架将抛出NoUniqueBeanDefinitionException, 以提示有多个满足条件的bean进行自动装配。程序无法正确做出判断使用哪一个,通过将@Qualifier注解与我们想要使用的特定Spring bean的名称一起进行装配,Spring框架就能从多个相同类型并满足装配要求的bean中找到我们想要的,

    @Component("studentInfo")
    public class StudentInfo implements UserInfo {
        public String userName() {
            return "student";
        }
    }
    
    @Component("teacherInfo")
    public class TeacherInfo implements UserInfo {
        public String userName {
            return "teacher";
        }
    }
    
    @Component
    public class UserService {
        @Autowired
        @Qualifier("studentInfo")
        private UserInfo userInfo;
    
        //todo 
    }

    1.3 @Bean

    @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里。添加的bean的id为方法名。

    @Configuration
    public class BeanConfig {
    
        @Bean
        public Person userInfo() {
            return new UserInfo("toutou", 18);
        }
    }

    这个配置就等同于之前在xml里的配置:

    <bean id="userInfo" class="com.test.UserInfo">
        <property name="age" value="18"/>
        <property name="name" value="请叫我头头哥 http://toutou.cnblogs.com"/>
    </bean>

    1.4 @Required

    @Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。

    @Required
    void setUserName(String name) {
        this.name = name;
    }
    <bean class="com.test.UserInfo">
        <property name="name" value="请叫我头头哥 https://www.cnblogs.com/toutou/" />
    </bean>

    1.5 @Value

    @Value将外部的值动态注入到Bean中。"注入外部的值"可以有很多种,它可以注入普通字符串、注入java 系统变量、注入表达式结果、注入其他Bean属性、将配置文件 *.properties 或 *. yml 里 配置的 属性 注入、注入文件资源和注入url资源等。

    1.6 @DependsOn

    Spring容器载入bean顺序是不确定的,Spring框架也没有约定特定载入顺序逻辑规范。@DependsOn注解可以定义在类和方法上,比如说A组件要依赖于B组件,那就是B组件需要比A组件先注册到IOC容器中。

    public class FirstBean {
    
        @Autowired
        private SecondBean secondBean;
    }
    
    public class SecondBean {
        public SecondBean() {
            System.out.println("SecondBean init");
        }
    }
    @Configuration
    public class BeanConfig {
    
        @Bean("firstBean")
        @DependsOn(value = {
            "secondBean"
        })
        public FirstBean firstBean() {
            return new FirstBean();
        }
    
        @Bean("secondBean")
        public SecondBean secondBean() {
            return new SecondBean();
        }
    }

    1.7 @Lazy

    @Lazy注解用于标识bean是否需要延迟加载。Spring IoC容器一般都会在启动的时候实例化所有单实例bean,如果想要Spring在启动的时候延迟加载A,即在调用B的时候再去初始化,则可以使用@Lazy注解。

    public class FirstBean {
        public void test() {
            System.out.println("FirstBean Class");
        }
    }
    public class SecondBean {
        public void test() {
            System.out.println("SecondBean Class");
        }
    }
    @Configuration
    public class AppConfig {
    
        @Lazy(value = true)
        @Bean
        public FirstBean firstBean() {
            return new FirstBean();
        }
    
        @Bean
        public SecondBean secondBean() {
            return new SecondBean();
        }
    }

    1.8 @Lookup

    @Lookup的注解是一个作用在方法上的注解,被其标注的方法会被重写,然后根据其返回值的类型,容器调用BeanFactory的getBean()方法来返回一个bean。

    1.9 @Primary

    @Primary与@Qualifier类似,都是解决@Autowired时容器中有多个相同类型bean的问题,Primary可以理解为默认优先选择,同时不可以同时设置多个。

    @Component("studentInfo")
    public class StudentInfo implements UserInfo {
        public String userName() {
            return "student";
        }
    }
    
    @Component("teacherInfo")
    @Primary
    public class TeacherInfo implements UserInfo {
        public String userName {
            return "teacher";
        }
    }
    
    @Component
    public class UserService {
        @Autowired
        @Qualifier("studentInfo")
        private UserInfo userInfo;
    
        //todo
    }

    1.10 @Scope

    @Scope注解是springIoc容器中的一个作用域,在 Spring IoC 容器中具有以下几种作用域:基本作用域singleton(单例)(默认作用域)、prototype(多例),Web 作用域(reqeust、session、globalsession),自定义作用域

    1.11 @Profile

    @profile注解的作用是为了应对多环境开发,比如开发环境使用dev, 生产环境使用prod,就可以使用@Profile注解实现不同的开发环境使用不同的数据源。spring3.2之前 @Profile注解用在类上,spring3.2 之后 @Profile注解用在方法上

    1.12 @Import

    @Import用于注入指定的类,导入组件id默认是组件的全类名。

    @Configuration
    public class ConfigA {
    
        @Bean
        public A a() {
            return new A();
        }
    }
    
    @Configuration
    @Import(ConfigA.class)
    public class ConfigB {
    
        @Bean
        public B b() {
            return new B();
        }
    }

    1.13 @ImportResource

    @ImportResource注解用于导入Spring的配置文件,让配置文件里面的内容生效;(就是以前写的springmvc.xml、applicationContext.xml)

    @Configuration
    @ImportResource({"classpath*:applicationContext.xml"})
    public class XmlConfiguration {
    }

    1.14 @PropertySource

    @PropertySource注解加载指定的配置文件

    @Configuration
    @PropertySource("classpath:config.properties")
    public class ProperySourceDemo implements InitializingBean {
    
        @Autowired
        Environment env;
    
        @Override
        public void afterPropertiesSet() throws Exception {
            setDatabaseConfig();
        }
    
        private void setDatabaseConfig() {
            DataSourceConfig config = new DataSourceConfig();
            config.setDriver(env.getProperty("jdbc.driver"));
            config.setUrl(env.getProperty("jdbc.url"));
            config.setUsername(env.getProperty("jdbc.username"));
            config.setPassword(env.getProperty("jdbc.password"));
            System.out.println(config.toString());
        }
    }

    1.15 @PropertySources

    @PropertySources顾名思义就是可以指定多个@PropertySource来导入配置文件。

    @PropertySources({
      @PropertySource("classpath:config.properties"),
      @PropertySource("classpath:db.properties")
     })
     public class AppConfig {
      //todo...
     }

    vSpring Web注解

    2.1 @RequestBody

    主要用来接收前端传递给后端的json字符串中的数据的。

    @RestController
    @RequestMapping("/api/v1")
    public class UserController {
        @Autowired
        private UserService userService;
    
        @PostMapping("/user")
        public UserInfo createUser(@Valid @RequestBody UserInfo user) {
            return userService.save(user);
        }
    }

    2.2 @RequestMapping

    @RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。也就是通过它来指定控制器可以处理哪些URL请求。

    @Controller
    class UserController {
        @RequestMapping(value = "/user/index", method = RequestMethod.GET)
        String index() {
            return "index";
        }
    }

    2.3 @GetMapping

    @GetMapping注释将Http GET请求映射到特定的处理程序方法。 它是一个组合的注释,@RequestMapping(method = RequestMethod.GET)的快捷方式。

        @GetMapping("/users")
        public List<user> getAllUser() {
            //...
        }
        @GetMapping("/users/{id}")
        public ResponseEntity<User> getUserById(@PathVariable(value = "id") Long userId)
                throws ResourceNotFoundException {
            user user = userRepository.findById(userId)
                    .orElseThrow(() -> new ResourceNotFoundException("user not found for this id :: " + userId));
            return ResponseEntity.ok().body(user);
        }

    2.4 @PathVariable

    @PathVariable是spring3.0的一个新功能:接收请求路径中占位符的值

    2.5 @PostMapping

    @PostMapping注释将HTTP POST请求映射到特定的处理程序方法。 它是一个组合的注释,@RequestMapping(method = RequestMethod.POST)的快捷方式。

    @PostMapping("/user/add")
    public User addUser(@Valid @RequestBody User user) {
     return userRepository.save(user);
    }

    其它扩展: @PutMapping、@DeleteMapping、@PatchMapping(这三种相对用的比较少,一笔带过。)

    2.6 @ControllerAdvice

    增强型控制器,对于控制器的全局配置放在同一个位置。可以用于定义@ExceptionHandler、@InitBinder、@ModelAttribute,可处理全局异常处理、全局数据绑定和全局数据预处理。

    @ControllerAdvice(basePackages = {"com.toutou.controller"} )
    public class GlobalControllerAdvice {
        @InitBinder
        public void dataBinding(WebDataBinder binder) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            dateFormat.setLenient(false);
            binder.registerCustomEditor(Date.class, "dob", new CustomDateEditor(dateFormat, true));
        }
        @ModelAttribute
        public void globalAttributes(Model model) {
            model.addAttribute("msg", "Hello World!");
        }
        @ExceptionHandler(FileNotFoundException.class)
        public ModelAndView myError(Exception exception) {
            ModelAndView mav = new ModelAndView();
            mav.addObject("exception", exception);
            mav.setViewName("error");
            return mav;
        }
    }

    2.7 @ExceptionHandler

    @ExceptionHandler统一处理某一类异常,从而能够减少代码重复率和复杂度。

    2.8 @InitBinder

    @InitBinder只在@Controller中注解方法来为这个控制器注册一个绑定器初始化方法,方法只对本控制器有效。

    2.9 @ModelAttribute

    @ModelAttribute注解用于将方法的参数或方法的返回值绑定到指定的模型属性上,并返回给Web视图。

    2.10 @ResponseBody

    @ResponseBody将controller里方法返回的对象通过适当的转换器转换为Json写入到response对象的body区.

    2.11 @Controller

    @Controller用于标记在一个类上,使用它标记的类就是一个SpringMvc Controller对象,分发处理器会扫描使用该注解的类的方法,并检测该方法是否使用了@RequestMapping注解。

    2.12 @RestController

    @RestController在Spring中的作用等同于@Controller + @ResponseBody。

    2.13 @RequestParam

    @RequestParam将请求参数绑定到你控制器的方法参数上(是springmvc中接收普通参数的注解)

    2.14 @CrossOrigin

    @CrossOrigin支持跨域,可用于Controller上,也可用于方法上。

    @CrossOrigin(origins = "http://toutou.com", maxAge = 3600)
    @RequestMapping("/index")
    String index() {
        return "Hello World!";
    }

    vSpring Boot注解

    3.1 @SpringBootApplication

    @SpringBootApplication是Sprnig Boot项目的核心注解,目的是开启自动配置。由于@Configuration,@EnableAutoConfiguration和@ComponentScan三个注解一般都是一起使用,于是spring boot提供了一个统一的注解@SpringBootApplication。即:@SpringBootApplication=@Configuration + @EnableAutoConfiguration + @ComponentScan。

    @SpringBootApplication // 等于:@Configuration @EnableAutoConfiguration @ComponentScan
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

    如上代码,是不是简洁了不少。

    3.2 @EnableAutoConfiguration

    可以根据classpath中的jar依赖,自动注册bean,一般用于类或接口上,它尝试根据您添加的jar依赖项自动配置Spring应用程序。自动载入应用程序所需的所有Bean——这依赖于Spring Boot在类路径中的查找。

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

    3.3 @ConditionalOnClass、@ConditionalOnMissingClass

    3.4 @ConditionalOnBean、@ConditionalOnMissingBean

    @ConditionalOnBean         //    当给定的在bean存在时,则实例化当前Bean
    @ConditionalOnMissingBean  //    当给定的在bean不存在时,则实例化当前Bean
    @ConditionalOnClass        //    当给定的类名在类路径上存在,则实例化当前Bean
    @ConditionalOnMissingClass //    当给定的类名在类路径上不存在,则实例化当前Bean

    3.5 @ConditionalOnProperty

    @ConditionalOnProperty可以通过配置文件中的属性值来判定configuration是否被注入。

    3.6 @ConditionalOnResource

    @ConditionalOnResource是注解在Configuration bean上,在其加载之前对指定资源进行校验,是否存在,如果不存在,抛出异常;该注解支持传入多个变量,

    3.7 @ConditionalOnWebApplication、@ConditionalOnNotWebApplication

    @ConditionalOnWebApplication主要的用处是: 当Spring为web服务时,才使注解的类生效;通常是配置类;@ConditionalOnNotWebApplication不是web应用。

    3.8 @Conditional

    @Conditional的作用是按照一定的条件进行判断,满足条件给容器注册bean。

    vSpring Scheduling注解

    4.1 @Scheduled

    @Scheduled可以作为一个触发源添加到一个方法中。

    @Scheduled(fixedDelay=1000)
    public void doSomething() {
        //...
    }

    4.2 @EnableScheduling

    @EnableScheduling 在配置类上使用,开启计划任务的支持(类上)。

    @Configuration
    @EnableScheduling //通过@EnableScheduling注解开启对计划任务的支持
    public class TaskScheduleConfig {
        //...
    }

    4.3 @Async

    @Async标注的方法,称之为异步方法;这些方法将在执行的时候,将会在独立的线程中被执行,调用者无需等待它的完成,即可继续其他的操作。有时候我们会调用一些特殊的任务,任务会比较耗时,重要的是,我们不管他返回的后果。这时候我们就需要用这类的异步任务啦。

    4.4 @EnableAsync

    @EnableAsync注解启用了Spring异步方法执行功能

    @Schedules

    @Schedules作用跟@Scheduled一样,@Schedules内部包含多个@Scheduled注解,可以表示一个方法可以存在多个调度设置。

    v注解集合

    @ComponentScan:表示将该类自动发现扫描组件。个人理解相当于,如果扫描到有@Component、@Controller、@Service等这些注解的类,并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。如果没有配置的话,Spring Boot会扫描启动类所在包下以及子包下的使用了@Service,@Repository等注解的类。

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

    @Inject:等价于默认的@Autowired,只是没有required属性;

    @Component:泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

    @JsonBackReference:解决嵌套外链问题。

    @JsonIgnore:作用是json序列化时将Java bean中的一些属性忽略掉,序列化和反序列化都受影响。

    @ConfigurationProperties:Spring Boot可使用注解的方式将自定义的properties文件映射到实体bean中,比如config.properties文件。

    @ConditionalOnSingleCandidate:组合@Conditional注解,当指定的class在容器中只有一个Bean,或者同时有多个但为首选时才开启配置。

    @ConditionalOnCloudPlatform:组合 @Conditional 注解,当指定的云平台激活时才开启配置。

    @ConditionalOnJndi:组合 @Conditional 注解,当指定的 JNDI 存在时才开启配置。

    @ConditionalOnJava:组合@Conditional 注解,当运行的 Java JVM 在指定的版本范围时才开启配置。

    @ConditionalOnExpression:组合 @Conditional 注解,当 SpEL 表达式为 true 时才开启配置。

    @WiselyConfiguration: 组合注解可以替代@Configuration和@ComponentScan

    @Transcational: 事务处理

    @Target (ElementType.TYPE):元注解,用来指定注解修饰类的那个成员 -->指定拦截规则

    @Cacheable: 数据缓存

    @ActiveProfiles: 用来声明活动的 profile

    @RunWith: 运行器

    其他参考/学习资料:

    v源码地址

    https://github.com/toutouge/javademosecond/tree/master/hellospringboot


    作  者:请叫我头头哥
    出  处:http://www.cnblogs.com/toutou/
    关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!
    版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
    特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信
    声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!

  • 相关阅读:
    js 的一些兼容性写法
    浏览器 Event对象 及 属性 的兼容处理
    js 三元表达式 复杂写法
    Angular.js中使用$watch监听模型变化
    Android学习之——ViewPager及应用引导页的开发
    Android开发中常用的库总结(持续更新)
    Android学习之——GridView
    Android开发工具——Android studio1.0正式版使用技巧
    Android学习之——ListView下拉刷新
    Android学习之——ListView
  • 原文地址:https://www.cnblogs.com/toutou/p/spring_boot_annotations.html
Copyright © 2011-2022 走看看