(一)Spring的IOC学习

  在applicationContext.xml来配置bean,通过该接口,在主程序中,可以指定初始化的对象,不需要在进行赋值操作,直接在xml里配置好。

接下来分享的是bean配置的多种方法:

①通过类的id获取bean

<bean id="user" class="com.author.bean.user">
<property name="name" value="Spring"></property>
<property name="age" value="18"></property>
<property name="sex" value="男"></property>
</bean>

在主函数中,通过容器里的id来获取bean对象

ApplicationContext aContext=new ClassPathXmlApplicationContext("applicationContext.xml");
user beanUser=(user)aContext.getBean("user");
beanUser.print();

输出该对象的信息:

②通过类型获取bean,缺点是只能是一个该类型的bean对象,如果有多个则会出错,多个可以通过id的方式来进行获取

例如上面的user对象,在主函数中可以这样获取:

ApplicationContext aContext=new ClassPathXmlApplicationContext("applicationContext.xml");
user beanUser=aContext.getBean(user.class);
beanUser.print();

得到的结果和上面的一样

二、给bean的属性赋值多种操作

①直接通过<property name="name" value="Spring"></property>利用name,value的方式进行赋值

②通过构造器进行赋值,前提在bean类中有该构造函数

<bean id="user03" class="com.author.bean.user">
<constructor-arg name="name" value="小明"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="sex" value="男"></constructor-arg>
</bean>

还可以直接省略name属性:

<!-- 省略name属性 -->
<bean id="user04" class="com.author.bean.user">
<constructor-arg value="校花"></constructor-arg>
<constructor-arg value="10"></constructor-arg>
<constructor-arg value="女"></constructor-arg>
</bean>

通过索引值指定参数位置

<bean id="book" class="com.atguigu.spring.bean.Book" >
           <constructor-arg value= "10010" index ="0"/>
           <constructor-arg value= "Book01" index ="1"/>
           <constructor-arg value= "Author01" index ="2"/>
           <constructor-arg value= "20.2" index ="3"/>
     </bean >

通过类型不同区分重载的构造器

<bean id="book" class="com.atguigu.spring.bean.Book" >
      <constructor-arg value= "10010" index ="0" type="java.lang.Integer" />
      <constructor-arg value= "Book01" index ="1" type="java.lang.String" />
      <constructor-arg value= "Author01" index ="2" type="java.lang.String" />
      <constructor-arg value= "20.2" index ="3" type="java.lang.Double" />
</bean >

p名称空间:为了简化XML文件的配置,越来越多的XML文件采用属性而非子元素配置信息。

需要导入:xmlns:p="http://www.springframework.org/schema/p"

<bean
id="studentSuper"
class="com.atguigu.helloworld.bean.Student"
p:studentId="2002" p:stuName="Jerry2016" p:age="18" />

三、内部bean对象以及引用对象

通过<null/>指定空元素

引用对象的使用:ref=“引用外部对象的id”

<bean id="car01" class="com.author.bean.car">
<constructor-arg value="宝马" ></constructor-arg>
<constructor-arg value="蓝白色"></constructor-arg>
<constructor-arg value="30000"></constructor-arg>
</bean> <bean id="user05" class="com.author.bean.user">
<property name="name">
<null></null>
</property>
<!-- ref引用的是外部的bean -->
<property name="car" ref="car01"></property>
</bean>

内部bean对象:相当于car=new car(),car对象在user对象里

<bean id="car01" class="com.author.bean.car">
<constructor-arg value="宝马" ></constructor-arg>
<constructor-arg value="蓝白色"></constructor-arg>
<constructor-arg value="30000"></constructor-arg>
</bean> <bean id="user05" class="com.author.bean.user">
<property name="name">
<null></null>
</property>
<!-- 相当于car=new car() 引用内部bean -->
<property name="car">
<bean class="com.author.bean.car">
<property name="name" value="自行车"></property>
</bean>
</property>
</bean>

四、集合属性

①List和map

List集合里包括:一个自定义的对象,一个引用对象。

  配置java.util.List类型的属性,需要指定<list>标签,在标签里包含一些元素。

  这些标签可以通过<value>指定简单的常量值,通过<ref>指定对其他Bean的引用。

  通过<bean>指定内置bean定义。通过<null/>指定空元素。

Map集合里包括:2个value,一个引用,一个自定义对象,都对应着不同的key值

  <map>标签里可以使用多个<entry>作为子标签。每个条目包含一个键和一个值。

  必须在<key>标签里定义键。

  因为键和值的类型没有限制,所以可以自由地为它们指定<value>、<ref>、<bean>或<null/>元素。

  可以将Map的键和值作为<entry>的属性定义:简单常量使用key和value来定义;bean引用通过key-ref和value-ref属性定义。

<bean id="refbook" class="com.author.bean.Book">
<property name="name" value="西厢记"></property>
</bean> <bean id="user06" class="com.author.bean.user">
<property name="books">
<list>
<!-- list标签体中添加每一个元素 -->
<bean class="com.author.bean.Book">
<property name="name" value="西游记"></property>
<property name="author" value="吴承恩"></property>
<property name="price" value="80"></property>
</bean>
<ref bean="refbook"></ref>
</list>
</property>
<property name="maps">
<map>
<entry key="key01" value="张三"></entry>
<entry key="key02" value="18"></entry>
<entry key="key03" value-ref="refbook"></entry>
<entry key="key04">
<bean class="com.author.bean.car">
<property name="name" value="宝马">
</property>
</bean>
</entry>
</map>
</property>
</bean>

在主函数中,输出结果:

user beanUser2=(user)aContext.getBean("user06");
System.out.println(beanUser2.books);
System.out.println(beanUser2.maps);

结果为:

②Properties

使用<props>定义java.util.Properties,该标签使用多个<prop>作为子标签。每个<prop>标签必须定义key属性

<bean class="com.atguigu.spring.bean.DataSource" id="dataSource">
<property name="properties">
<props>
<prop key="userName">root</prop>
<prop key="password">root</prop>
<prop key="url">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>

③集合类型的bean

如果只能将集合对象配置在某个bean内部,则这个集合的配置将不能重用。我们需要将集合bean的配置拿到外面,供其他bean引用。

配置集合类型的bean需要引入util名称空间:xmlns:util="http://www.springframework.org/schema/util"

<util:map id="myMap">
<entry key="key01" value="张三"></entry>
<entry key="key02" value="18"></entry>
<entry key="key03" value-ref="refbook"></entry>
<entry key="key04">
<bean class="com.author.bean.car">
<property name="name" value="宝马">
</property>
</bean>
</entry>
</util:map> <util:list id="myList">
<bean class="com.author.bean.Book"></bean>
<value>12</value>
<ref bean="myMap"></ref>
</util:list>

五、级联属性

含义:可以控制属性的属性

可以改变user07下的car的price属性,如果car为引用对象,那么引用对象也会被修改

<!-- 级联属性可以控制属性的属性 -->
<bean id="user07" class="com.author.bean.user">
<property name="car" ref="car01"></property>
<property name="car.price" value="900000"></property>
</bean>

--------------这就是今天分享的bean的配置操作。

明天学习:利用工厂来创建bean

Spring的IOC容器学习笔记的更多相关文章

  1. spring学习笔记三:Component注解(把POJO类实例化到spring的IOC容器中)

    Component注解:把普通的POJO 类实例化到spring的IOC容器中,就是定义成<bean id="" class=""> 项目目录树: ...

  2. Spring实战第一章学习笔记

    Spring实战第一章学习笔记 Java开发的简化 为了降低Java开发的复杂性,Spring采取了以下四种策略: 基于POJO的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: 基于切面 ...

  3. Unity(IOC)学习笔记

    原文:Unity(IOC)学习笔记 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/details/79432 ...

  4. Spring的IOC容器第一辑

    一.Spring的IOC容器概述 Spring的IOC的过程也被称为依赖注入(DI),那么对象可以通过构造函数参数,工厂方法的参数或在工厂方法构造或返回的对象实例上设置的属性来定义它们的依赖关系,然后 ...

  5. 好莱坞原则—Spring的IOC容器

    IOC容器的概念,之前在学习SSH的时候,就有接触过.但那时候也仅仅是知道这么个概念,认为它非常难理解.事实上并非它难理解,而是我并没有停下来好好对它总结梳理过. IOC(Inversion of C ...

  6. Spring的IoC容器(转)BeanFactory

    Spring的IoC容器 Spring读书笔记-----Spring的Bean之Bean的基本概念 加菲猫 Just have a little faith. Spring的IoC容器 (用户持久化类 ...

  7. Spring Cloud微服务学习笔记

    Spring Cloud微服务学习笔记 SOA->Dubbo 微服务架构->Spring Cloud提供了一个一站式的微服务解决方案 第一部分 微服务架构 1 互联网应用架构发展 那些迫使 ...

  8. Spring 深入——IoC 容器 01

    IoC容器的实现学习--01 目录 IoC容器的实现学习--01 简介 IoC 容器系列的设计与实现:BeanFactory 和 ApplicationContext BeanFactory load ...

  9. Spring框架IOC容器和AOP解析

    主要分析点: 一.Spring开源框架的简介  二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置  一.S ...

随机推荐

  1. Git 相关问题分享,git reset与git revert的区别?

    1.如果我在git add 后想要撤销操作,该怎么做? 使用 git rm --cache [文件名/ *] 或者 git reset HEAD, 为什么这个命令也会有效果呢,实际上reset将 HE ...

  2. Angular 1 深度解析:脏数据检查与 angular 性能优化

    TL;DR 脏检查是一种模型到视图的数据映射机制,由 $apply 或 $digest 触发. 脏检查的范围是整个页面,不受区域或组件划分影响 使用尽量简单的绑定表达式提升脏检查执行速度 尽量减少页面 ...

  3. C++泛化双向链表

    泛型双向链表 双向链表(doublyLinkedList.h) /******************************************************************* ...

  4. js 面向对象中,定义一个函数的过程

    定义一个函数做的两件事:1: 实例化一个Function对象:2: 实例化一个Object对象,并给该函数扩展prototype属性指向这个构造函数 大致过程如图所示: 每一种引用类型(函数,对象,数 ...

  5. webpack知识锦集(一)

    ebpack是一个javascript应用吃那个程序的静态模块打包器(module bundler).处理时候会递归构建一个依赖关系图,包含每个模块,将模块打包成一个或者多个bundle. 核心概念: ...

  6. ARC中__bridge, __bridge__transfer, __bridge_retained 关系

    总结于 IOS Tuturial 中 ARC两章,详细在dropbox pdf 文档. Toll-Free Bridging 当你在 Objective-C 和 Core Foundation 对象之 ...

  7. 2019-2020-3 20174318张致豪《网络对抗技术》Exp2 后门原理与实践

    Exp2 后门原理与实践 前期准备 一.实验目标与基础知识 1.1 实践目标 使用netcat获取主机操作Shell,cron启动 使用socat获取主机操作Shell,任务计划启动 使用MSF  m ...

  8. c++第一周测验

    本次得分为:14.00/14.00, 本次测试的提交时间为:2020-03-08, 如果你认为本次测试成绩不理想,你可以选择再做一次. 1 单选(1分) 下面程序片段哪个没错? 得分/总分 A. in ...

  9. js中字符串 stringObject 的 replace() 方法

    一.定义 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的字符串. 二.语法 stringObject.replace(regexp/substr,repl ...

  10. macOS Catalina 升级软件问题

    最近升级macOS Catalina系统,升级失败时多尝试几次就可以执行成功了,在使用过程中发现以下问题,大家谨慎升级!!! 存在软件启动不兼容,不存在已软件激活失效问题. 有道词典不兼容,启动异常 ...