为了账号安全,请及时绑定邮箱和手机立即绑定

【金秋打卡】第6天 使用XML方式实现Spring Ioc

标签:
Spring

课程信息

● 学习课程:Java工程师2022版
● 章节名称:Spring Ioc容器与Bean管理-使用XML方式实现Spring Ioc
● 讲师:悟空

课程内容

ioc容器就是通过配置的方式让我们在不需要new关键字的情况下,对对象进行创建,创建对象是ioc容器最基本的工作
1.配置pom.xml
添加framework依赖和阿里云镜像

<repositories>
  <repository>
    <id>aliyun</id>
    <name>aliyun</name>
    <url>https://maven.aliyun.com/repository/public</url>
  </repository>
</repositories>
<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.6.RELEASE</version>
  </dependency>
</dependencies>

spring-core最核心的代码,最底层的代码
spring-beans对java对象进行管理,实例化,设置关联
spring-expression表达式模块
spring-jcl日志交互模块
spring-aop面向切面编程模块
spring-context上下文模块
2.创建配置文件ApplicationContext.xml

<bean id="sweetApple" class="com.imooc.spring.ioc.entity.Apple">
  <property name="title" value="红富士"></property>
  <property name="origin" value="欧洲"></property>
  <property name="color" value="红色"></property>
</bean>

<bean id="sourApple" class="com.imooc.spring.ioc.entity.Apple">
  <property name="title" value="青苹果"></property>
  <property name="origin" value="中亚"></property>
  <property name="color" value="绿色"></property>
</bean>

<bean id="softApple" class="com.imooc.spring.ioc.entity.Apple">
  <property name="title" value="金帅"></property>
  <property name="origin" value="中国"></property>
  <property name="color" value="黄色"></property>
</bean>

<bean id="lily" class="com.imooc.spring.ioc.entity.Child">
  <property name="name" value="莉莉"/>
  <property name="apple" ref="sweetApple/>
</bean>

<bean id="andy" class="com.imooc.spring.ioc.entity.Child">
  <property name="name" value="安迪"/>
  <property name="apple" ref="sourApple/>
</bean>

<bean id="luna" class="com.imooc.spring.ioc.entity.Child">
  <property name="name" value="露娜"/>
  <property name="apple" ref="softApple/>
</bean>

3.创建SpringApplication类主方法
启动ioc容器:springioc容器启动了才能对ApplicationContext.xml配置文件里的bean进行实例化,context对象本身就指代了springioc容器,通过ApplicationContext加载指定xml配置文件来初始化类对象,通过其 getbean方法提取想要的对象修改程序时,只需要修改配置文件,无需修改源代码,程序上线之后,可维护性大大增加。

public void main(String[] args){
    //创建Spring Ioc容器,并根据配置文件在容器中实例化对象
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Apple sweetApple = context.getBean("sweetApple", Apple.class);
    System.out.println(sweetApple.getTitle);
    // 从Ioc容器中提取beanId=lily对象
    Child lily = context.getBean("lily", Child.class);
    lily.eat();
    Child andy = context.getBean("andy", Child.class);
    andy.eat();
    Child luna = context.getBean("luna", Child.class);
    luna.eat();
}

XML管理对象(Bean)
三种配置方式(基于XML配置Bean,基于注解配置Bean,基于Java代码配置Bean)
基于XML配置Bean

<bean id="softApple" class="com.imooc.spring.ioc.entity.Apple">
  <property name="title" value="金帅"></property>
  <property name="origin" value="中国"></property>
  <property name="color" value="黄色"></property>
</bean>
  ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

XML实例化Bean的配置方式
基于构造方法实例化对象
ApplicationContext是一个接口,所有实现这个接口的类的唯一职责就是帮我们初始化IoC容器并且实例化对象
bean里不写任何配置信息,默认构造方法

<!-- 利用构造方法参数名实例化(更推荐):-->
<bean id="softApple" class="com.imooc.spring.ioc.entity.Apple">
  <constructor-arg name="title" value="金帅"></constructor-arg>
  <constructor-arg name="origin" value="中国"></constructor-arg>
  <constructor-arg name="color" value="黄色"></constructor-arg>
</bean>
<!-- 利用构造方法参数位置实例化(不推荐) -->
<bean id="softApple" class="com.imooc.spring.ioc.entity.Apple">
  <constructor-arg index="1" value="金帅"></constructor-arg>
  <constructor-arg index="2" value="中国"></constructor-arg>
  <constructor-arg index="3" value="黄色"></constructor-arg>
</bean>

<property name="" value="" /> 和 <constructor-arg name="" value="" /> 有啥区别:
两者都是给bean注入属性的,区别如下:
constructor-arg:通过构造函数(构造方法)注入。
property:通过setter对应的方法注入。
基于静态工厂实例化对象(好处是可以在创建对象的代码体内增加bean无法实现的功能,比如日志)创建对象的工厂的方法是static,只属于静态工厂类。方法内部通过new关键字,组装想要的对象。
工厂类方法中通过静态方法使用new关键构建对象,工厂类隐藏了如何构建的对象

<!-- 利用静态工厂获取对象 -->
<Bean id="apple4" class="com.imooc.spring.ioc.factory.AppleStaticFactory" 
factory-method="createSweetApple"/>

基于工厂实例方法实例化对象

<!-- 利用工厂实例方法获取对象 -->
<bean id="factoryInstance" class="com.imooc.spring.ioc.factory.AppleFactoryInstance"/>
<bean id="apple5" factory-bean="factoryInstance" factory-method="createSweetApple"/>

从Ioc容器获取bean

Apple sweetApple = context.getBean("sweetApple", Apple.class):
Apple sweetApple = (Apple) context.getBean("sweetApple");
System.out.println(sweetApple);

id与name属性相同点
bean id与name都是设置对象在Ioc容器中唯一标识
两者在同一个配置文件都不允许出现重复
两者允许在多个配置文件中出现重复,新对象覆盖旧对象
id与name属性不同点
id要求更为严格,一次只能定义一个对象标识(推荐)
name更为宽松,一次允许定义多个对象标识,用逗号分隔,
tips:id与name的命名要求有意义,按驼峰命名书写
没有id 和name的 用类全名称表示

在源代码下面的resources并不是类路径,真实的运行环境是在target目录中有一个classes,实际上maven工程加载的时候所谓的类路径指定其实就是classes目录

学习收获

学习了使用xml实现spring Ioc,基于XML方式创建bean,XML实例化bean三种方式,基于构造方法实例化对象,基于静态工厂实例化对象,基于工厂实例方法实例化对象,如何从Ioc中获取bean对象,以及加载配置文件context.xml的路径表达式相关说明

打卡截图

图片描述
图片描述

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
0
获赞与收藏
0

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消