首页 > 其他分享 >Spring IOC官方文档学习笔记(九)之基于注解的容器配置

Spring IOC官方文档学习笔记(九)之基于注解的容器配置

时间:2023-02-06 22:58:32浏览次数:44  
标签:xml String Spring 配置 文档 str 注解 IOC public

1.基于注解的配置与基于xml的配置

(1) 在xml配置文件中,使用context:annotation-config</context:annotation-config>标签即可开启基于注解的配置,如下所示,该标签会隐式的向容器中添加ConfigurationClassPostProcessor,AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor这5个后置处理器,用于注解配置

<beans ....>

    <!-- 开启基于注解的配置,该标签不常用,常用下面的<context:component-scan />标签 -->
    <context:annotation-config></context:annotation-config>

    <!-- 开启注解扫描,它不仅有着 <context:annotation-config />标签相同的效果,还提供了一个base-package属性用来指定包扫描路径,将路径下所扫描到的bean注入到容器中 -->
    <!-- <context:component-scan base-package="cn.example.spring.boke"></context:component-scan> -->
</beans>

(2) Spring同时支持基于注解的配置与基于xml的配置,可以将两者混合起来使用;注解配置会先于xml配置执行,因此,基于xml配置注入的属性值会覆盖掉基于注解配置注入的属性值,如下所示

//定义一个普通bean
@Component(value = "exampleA")
public class ExampleA {
    
    //通过注解,注入属性值
    @Value("Annotation injected")
    private String str;

    public void setStr(String str) {
        this.str = str;
    }

    public String getStr() {
        return str;
    }
}

<!-- xml配置文件 -->
<beans ....>

    <context:component-scan base-package="cn.example.spring.boke"></context:component-scan>

    <!-- 通过xml,注入属性值,注意:这里bean的id与上面基于注解所提供的bean的id是一致的 -->
    <bean id="exampleA" class="cn.example.spring.boke.ExampleA">
        <property name="str" value="xml inject"></property>
    </bean>
</beans>

//测试,打印结果为 xml inject,证明注解方式会先于xml方式执行
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("boke/from.xml");
System.out.println(((ExampleA) ctx.getBean("exampleA")).getStr());

2.@Required

(1) @Required注解用于setter方法上,表示某个属性值必须被注入,若未注入该属性值,则容器会抛出异常,从Spring 5.1版本开始,该注解已被弃用,Spring目前推荐使用构造函数注入来注入这些非空依赖项,如下所示

//ExampleA有一个非空属性str
public class ExampleA {

    private String str;

    @Required
    public void setStr(String str) {
        this.str = str;
    }
}

<!-- xml配置文件 -->
<beans ....>
    <context:annotation-config></context:annotation-config>

    <bean id="exampleA" class="cn.example.spring.boke.ExampleA">
        <!-- 必须要设置str属性值,如果将下面这条标签注释掉,那么启动时容器会抛出异常 -->
        <property name="str" value="must"></property>
    </bean>
</beans>

3.@Autowired

未完待续...

标签:xml,String,Spring,配置,文档,str,注解,IOC,public
From: https://www.cnblogs.com/shame11/p/17053881.html

相关文章

  • SpringBoot 使用 @ConfigurationProperties 异常 Not registered via @EnableConfigur
    最近,我们在使用 @ConfigurationProperties 进行注解的时候,遇到了Notregisteredvia@EnableConfigurationProperties,markedasSpringcomponent,orscannedvia@C......
  • springboot:java实现邮件及附件发送、HTML正文的三种方式(一)【附带源码】
    0.引言邮件发送是我们日常开发中比较常见的功能,常用于预警信息提醒、统计数据定期发送等需求。一般该方法会由前人封装好,实际开发时只需要调用即可,但具体怎么实现的,如何从......
  • springboot:java实现邮件及附件发送、HTML正文的三种方式(三)【附带源码】
    0.引言邮件发送是我们日常开发中比较常见的功能,常用于预警信息提醒、统计数据定期发送等需求。一般该方法会由前人封装好,实际开发时只需要调用即可,但具体怎么实现的,如何从......
  • Spring—事务
    前言一觉醒来下雨了,今天还是蛮冷的,坐标无锡,及时添衣服啊。话说最近喜欢上了看衬衫,对格子衬衫感兴趣了(据说格子衬衫是程序员的标配,是我越来越像程序员了么...)。早上从床上......
  • Spring循环依赖
    Spring循环依赖面试中也会被常常问到。但是它的整个过程很多人都不知道,什么叫循环依赖呢。多个Bean之间相互依赖,形成一个闭环。如下图(A,B,C分别为Spring容器中3个Bean)就......
  • RCU-1——内核文档翻译——RCU-tasks
    一、TheRCU-taskssubsystem:https://lwn.net/Articles/607117/翻译读取-复制-更新(RCU)机制负责保留旧版本的数据结构,直到它知道没有CPU可以保存对它们的引用;一旦发生......
  • SpringBoot2.5.6集成mybatis
    1.应用依赖<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.0</version......
  • Spring在Filter中记录Web请求Request和返回Response的内容及时长
    1简介在SpringMVC中,我们有时需要记录一下请求和返回的内容,方便出现问题时排查。比较Header、RequestBody等。这些在Controller也可以记录,但在Filter中会更方便。而我们......
  • SpringBoot响应Json数据乱码通过配置解决
    场景实现把SpringBoot的response编码设置为utf-8找到application.properties配置文件添加如下:#设置响应为utf-8spring.http.encoding.force-response=true 再次刷新浏览器......
  • SpringBoot中自定义消息转化器
    场景1.SpringBoot自动配置了消息转化器。2.自定义消息转化器,只需要在类中添加消息转化器的@Bean,就会被SpringBoot自动加入到容器中。实现新建Controllerpackagecom.exampl......