• springboot中的常用注解


    个人觉得springboor中常用的注解主要可以分为三种:放入容器型注解、从容器中取出型注解和功能型注解。其中的放入容器型和从容器中取出型就是我们平时所说的控制反转和依赖注入的概念(个人版本- - - 可以看一看,别太当真 0.0)

    放入容器型注解
    简介:个人觉得就是申明一个实例对象,然后将这个对象交给spring管理。

    1、@Component:放在类上,把普通类实例化到spring容器中。可以说很多注解都是基于这个注解的。

    2、@Bean: 放在方法上,用@Bean标注方法等价于XML中配置bean,这个方法一般返回一个实体对象,告诉spring这里产生一个对象,然后这个对象会交给Spring管理。产生这个对象的方法Spring只会调用一次,随后这个Spring将会将这个Bean对象放在自己的容器中。

    3、@Configuration:标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到srping容器中,并且实例名就是方法名。(其实就是靠@Component注解)

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component
    public @interface Configuration {
    String value() default "";
    }
    1
    2
    3
    4
    5
    6
    7
    4、@ConfigurationProperties:将配置文件中的参数映射成一个对象,通过prefix来设定前缀,然后将后面的和对象的属性名一致就能实现注入(当然这个对象需要注入的属性需要提供get和set方法 - - - 因为spring底层其实就是通过反射调用该对象的set方法)

    实例:
    @ConfigurationProperties(prefix = “spring.datasource.test1”)
    public class Datasource1 {
        private String url;
        private String username;
        private String password;
        get - - set方法
    }
    application.properties:
    spring.datasource.test1.url=jdbc:mysql://localhost:3307/multipledatasource1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
    spring.datasource.test1.username=root
    spring.datasource.test1.password=root
    spring.datasource.test1.driver-class-name=com.mysql.cj.jdbc.Driver

    5、@Value : value的作用其实和ConfigurationProperties作用差不多,就是读取配置文件中参数的值,但是value是放在变量上面的,且是单值读取,还有一点就是value标注的变量并不需要和配置文件的参数名字一致。

    语法:
    @Value(“${参数}”)
    private String 变量名字
    application.properties:
    参数=值
    注意:配置文件中的参数和value表达式中的参数名字是要保持一致的

    6、@RestController、@Controller、@Service、@Repository:
            这四个注解熟悉吗?我想大家一定知道这四个注解各自的作用,但是这里需要强调一点就是他们其实是一样的,至少在spring5.x之前,他们实质上是没有什么区别的(当然你非要说@RestController=@Controller+@ResponseBody 这个也对)。我这边主要是要强调,他们实质上都是依靠于@Component注解的,我们可以查看源码。

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component
    public @interface Controller {
    String value() default "";
    }
    1
    2
    3
    4
    5
    6
    7
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component
    public @interface Service {
    String value() default "";
    }
    1
    2
    3
    4
    5
    6
    7
    我们可以看到Service 和Controller 这两个注解里面的东西是一模一样的,没什么区别。我不知道你们有没有做过这样的骚操作:就是在controller层中不用@Controller,而用@Service;在service中不用@Service,而用@Controller。其实这样写对于spring本身来说是没什么区别的,而且项目也能正常运行,这里我是亲自测过的。。。据说分为这三个注解的目的是为了从结构上分层。

    从容器中取出型注解
    简介:个人觉得就是我们平时所说的依赖注入。

    1、@Resource:是按照名称来注入的,当找不到与名称匹配的bean才会按照类型来注入。其实我们平时用的@Resource都是用了他的默认的方式,即都不指定名字和类型。spring通过反射机制使用byName方法自动注入。

    @Resource(type = ShiroService.class, name = "shiroService")
    private ShiroService shiroService;
    1
    2
    @Resource 的装配顺序:
    1. 如果同时指定了 name 属性和 type 属性,那么 Spring 将从容器中找唯一匹配的 bean 进行装配,找不到则抛出异常
    2. 如果指定了 name 属性值,则从容器中查找名称匹配的 bean 进行装配,找不到则抛出异常
    3. 如果指定了 type 属性值,则从容器中查找类型匹配的唯一的 bean 进行装配,找不到或者找到多个都会抛出异常
    4. 如果都不指定,则会自动按照 byName 方式进行装配(我们一般都用的是这个。。。)

    2、@Autowried:默认是按照类型进行装配注入,如果允许 null 值,可以设置它 required 为false。即:当不能确定 Spring 容器中一定拥有某个类的 Bean 时,可以在需要自动注入该类 Bean 的地方可以使用 @Autowired(required = false) ,这等于告诉 Spring:在找不到匹配 Bean 时也不报错。

    @Autowired(required = false)
    private ShiroService shiroService;
    1
    2
    3、@Qualifier:@Autowired是根据类型进行自动装配的。如果当spring上下文中存在不止一个A类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存在A类型的bean,而且我们又使用A类型,也会抛出BeanCreationException异常。针对存在多个A类型的Bean,我们可以联合使用@Qualifier和@Autowired来解决这些问题。

    @Autowried
    @Qualifier("adminDAO")
    private AdminDAO adminDAO;
    1
    2
    3
    简单来说,Qualifier就是规定一下Bean的名字,相当于@Resource规定了name属性。

    功能型注解
    1、@SpringBootApplication:这个注解就是集成了:@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan这三个注解。其中@SpringBootConfiguration:表示这个类为配置类;@EnableAutoConfiguration:表示开启自动配置,我们平时所说springboot无配置就是这个参数起的作用,他读取了springboot默认的配置;@ComponentScan:表示自动扫描,这个扫描默认只能扫同一级的目录。

    2、@EnableConfigurationProperties:将带有@ConfigurationProperties注解的类注入为Spring容器的Bean。如果使用了@ConfigurationProperties但是没有在启动类上增加这个注解,则@ConfigurationProperties将不起作用。

    3、@Async与@EnableAsync:其中@Async表示这个方法为异步方法;@EnableAsync这个注解需要加在启动类上,表示支持异步操作;如果不加,则@Async将不起作用。

    @RestController
    public class AsyncController {
    @Autowired
    private AsyncService asyncservice;

    @RequestMapping("/asyncTest.do")
    public String asyncTest() {
    System.out.println("############asyncTest############");
    System.out.println("############a############");
    asyncservice.test();
    System.out.println("############b############");
    return "success";
    }

    @RequestMapping("/asyncTest2.do")
    public String asyncTest2() {
    System.out.println("############asyncTest2############");
    System.out.println("############a############");
    asyncservice.test2();
    System.out.println("############b############");
    return "success";
    }

    @RequestMapping("/asyncTest3.do")
    public String asyncTest3() {
    System.out.println("############asyncTest3############");
    System.out.println("############a############");
    asyncservice.test3();
    System.out.println("############b############");
    return "success";
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    @Service
    public class AsyncService {

    public void test() {
    System.out.println("############c############");
    for (int i = 0; i < 5; i++) {
    System.out.println(i);
    }
    System.out.println("############d############");
    }

    @Async
    public void test2() {
    System.out.println("############c############");
    for (int i = 0; i < 5; i++) {
    System.out.println(i);
    }
    System.out.println("############d############");
    }

    /**
    * @Async就相当于另起一个线程
    */
    public void test3() {
    new Thread() {
    @Override
    public void run() {
    System.out.println("############c############");
    for (int i = 0; i < 5; i++) {
    System.out.println(i);
    }
    System.out.println("############d############");
    }
    }.start();
    }

    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    实验结果是: asyncTest.do输出为: acdb; asyncTest2.do输出为: abcd; asyncTest3.do输出为: abcd;

    所以简单来说:@Async就相当于另起一个线程。

    4、@Scheduled与@EnableScheduling: 定时任务。@EnableScheduling这个注解需要加在启动类上,表示支持定时任务。

    @Scheduled(cron = "0/5 * * * * ? ")
    public void doTest() {
    System.out.println(new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date()));
    }
    ————————————————
    原文链接:https://blog.csdn.net/tuesdayma/article/details/81029539

  • 相关阅读:
    python面试的100题(2)
    面试题目和地址
    python面试的100题(1)
    no module named系列问题解决
    ubuntu16.04无法打开终端
    Reinforcement Learning,微信公众号:DRL学习
    Java中的I/O操作File
    Java中的Date时间转换【SimpleDateFormat (parse和format)】和Calendar日历表
    重写equals方法
    Java中栈,堆,常量池的简单理解
  • 原文地址:https://www.cnblogs.com/sea520/p/11740770.html
Copyright © 2020-2023  润新知