美文网首页
Spring中的注解

Spring中的注解

作者: 从零开始的程序猿生活 | 来源:发表于2020-12-14 13:26 被阅读0次
@Component

标注该类是一个普通的bean类

@Controller

标注该类是一个控制器组件类

@Service

标注该类是一个业务逻辑组件类

@Repository

标注该类是一个数据库组件类

@Component、@Controller、@Service、@Repository四个注解的功能是一样的,只是组件的类型不同,@Controller、@Service、@Repository这三个注解中都包含@Component注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {

    /**
     * The value may indicate a suggestion for a logical component name,
     * to be turned into a Spring bean in case of an autodetected component.
     * @return the suggested component name, if any (or empty String otherwise)
     */
    @AliasFor(annotation = Component.class)
    String value() default "";

}
@ComponentScan(basePackages = {"com.test","com.demo.*"})

配置扫描组件的基本包、会扫描com.test和com.demo下面的@Component、@Controller、@Service、@Repository注解

@MapperScan({"com.demo.mapper","com.demo.*.mapper"})

配置扫描myBatis中的mapper文件路径

@SpringBootApplication

项目的入口,启动类的标识。该类中会定义一个main方法,运行main方法的时候会自动读取配置文件并且加载指定资源,并且进行初始化。
@SpringBootApplication注解被@EnableAutoConfiguration、@SpringBootConfiguration、@ComponentScan这三个注解修饰。


@EnableAutoConfiguration:启动自动配置。
@SpringBootConfiguration:允许上下文注册额外的Bean或者导入其他的配置类。
@ComponentScan:在指定包上启动@Component扫描。

@Autowired

告诉spring为属性注入一个值。
注意:使用@Autowired注入的时候,首先会去容器中查找对应类型的Bean:

  • 如果没有查到,抛出空指针异常。可以配置@Autowired(required = false)让注入的类可以为null
  • 如果查到一个,直接将该Bean返回并装载。
  • 如果查到多个,会按照名称来查找。
    可以通过 @Qualifier("CommonService")注解指定查找的类名称。
    如果没有手动指定会默认查找类名首字母小写的类。
    例如:CommonService 会按 commonService来查找。
@Primary 和 @Qualifier

@Primary
这个注解可以理解为,如果一个接口有多个实现类,那么这个接口使用@Autowired 进行自动注入的时候,Spring无法确定你要用哪个实现类,在其中一个实现类加上这个注解,那么在有多个选择的时候就会优先注入这个类。

@Qualifier
这个注解可以理解为,如果一个接口有多个实现类,那么这个接口使用@Autowired 进行自动注入的时候,Spring无法确定你要用哪个实现类,上面的@Primary 相当于给了个默认是,但是不够灵活,因为我们可以不同的地方要选择不同的实现类,这时就可以使用@Autowied + @Qualifier的组合,定位到具体的实现类,
需要注意的是@Qualifier 指定的name 要和 bean容器里的 bean name一直,不然会报错。找不到这个类。

    @Autowired
    @Qualifier("redisTemplate5")
    RedisTemplate<String,String> redisTemplate;
@PostConstruct 和 @PreDestroy

这两个注解不是Spring提供的,而是Java提供的。

从Java EE5规范开始,Servlet中增加了两个影响Servlet生命周期的注解,@PostConstruct和@PreDestroy,这两个注解被用来修饰一个非静态的void()方法。

  • @PostConstruct:被这个注解修饰的方法会在Servlet加载的时候被执行,并且只会被执行一次,可以用来做一些初始化操作,@PostConstruct 修饰的方法在构造函数后执行,在init()方法之前执行。@PreDestroy修饰的方法在destory()方法之后执行。
    (下图为引用)


    image.png

在Spring 中 ,Constructor、@Autowired、@PostConstruct的执行顺序为:
Constructor >> @Autowired >> @PostConstruct
那么@PostConstruct的妙用在哪里呢?
例如:有两个实例 A,B
我们在初始化A的时候需要使用B的实例进行一些操作,可以如果使用@Autowired注解注入B的话,只有在Constructor 步骤完成,也就是A,B都初始化之后才可以进行注入,所以没法使用@Autowired进行一些初始化操作。
这时候就可以使用@PostConstruct注解了,使用@Autowired注入后再将需要初始化的值进行赋值。

下面问题演示:

问题描述:我需要在这个类初始化的时候为 staticA 进行初始化,因为这个变量是static修饰的,所以在类第一次被加载的时候就会进行注入,那时A的实例注入的一定是null,所以就需要对staticA进行初始化。但是在执行到@Autowired的时Constructor 步骤已经完成,这时候怎么样进行初始化呢?
解决:使用@PostConstruct 对 staticA进行初始化。

    @Autowired
    private A a;
    @Autowired
    private static A staticA;

    @PostConstruct
    public void init() {
        staticA = a;
    }

相关文章

  • Spring注解

    Spring注解 Spring可以通过XML文件和注解来配置bean,本文就Spring中的注解进行简要学习 概述...

  • Eable 注解

    Spring 中 Enable 注解: SpringBoot 中的 Enable 注解: SpringDataJP...

  • Spring(二) 注解

    Spring(二) 注解 Spring中常用注解介绍 Spring中的注解配置和xml配置要实现的功能是一样的 注...

  • Spring注解注入

    Spring注解注入 Spring2.5引入注解方式去定义Bean @Component 描述Spring框架中的...

  • Spring_9 Spring基于AspectJ AOP 注解实

    Spring AOP 使用注解方式增强 配置spring.xml文件 在 spring.xml 中开启AOP注解。...

  • Spring(二)

    Spring的Bean管理(注解方式) 导入包 在Spring的注解AOP中需要导入spring-aop的jar包...

  • SSM框架注解总结

    Spring中的注解: 注解含义: 用于创建对象的注解: 相当于: ...

  • Spring 框架中的注解

    Spring 框架中的 @Autowired 注解 原文地址 ​ 在使用注解进行 Spring 项目的开发时,我们...

  • Spring组合注解与@Enable*注解

    Spring组合注解与@Enable*注解 这篇文章主要谈论Spring中的注解,在近端发型的新版本的都是以注解为...

  • Spring AOP编程实战

    Spring框架的IOC功能之注解的方式 Spring框架的IOC之注解方式的快速入门 Spring框架中Bean...

网友评论

      本文标题:Spring中的注解

      本文链接:https://www.haomeiwen.com/subject/yizmgktx.html