spring-IOC容器(一)
ApplicationContext 代表IOC容器(控制反转)
ApplicationContext的主要实现类:
——ClassPathXmlApplicationContext:从类路径下加载配置文件
——FileSystemXmlApplicationContext:从文件系统中加载配置文件
依赖注入的方式:
——属性注入(通过setter方法注入Bean的属性值或依赖对象,使用<property>元素,使用name属性指定Bean的属性名称)
——构造方法注入(构造器注入在<constructor-arg>元素里声明属性,<constructor-arg>中没有name属性)
.xml中bean配置:
一、说明:1、如果字面值包含特殊字符可以使用<![CDATA[]]>包裹起来
2、可以使用property中的ref属性建立bean之间的引用关系
3、注入null值时,可使用专用的 <null/>元素标签
<!--配置
class:bean的全类名,通过反射的方式在IOC中创建Bean,所以要求Bean中必须有无参数的构造器
id:标识容器中的bean
-->
<bean id="helloworld" class="com.atguigu.spring.beans.HelloWorld">
<property name="name" value="1234123"></property>
</bean> <!-- 使用构造器注入属性值可以指定参数的位置或类型!以区分重载的构造器! -->
<bean id="car" class="com.atguigu.spring.beans.Car">
<constructor-arg value="benchi" index="0"></constructor-arg>
<constructor-arg value="508000" type="int"></constructor-arg>
<constructor-arg value="2" type="java.lang.Double"></constructor-arg>
<!-- 如果字面值包含特殊字符可以使用<![CDATA[]]>包裹起来 -->
<constructor-arg type="java.lang.String">
<value><![CDATA[<guangqi~shangqi>]]></value>
</constructor-arg>
</bean> <!-- 可以使用property中的ref属性建立bean之间的引用关系 -->
<bean id="person" class="com.atguigu.spring.beans.Person">
<property name="name" value="lee"></property>
<property name="age" value="24"></property>
<property name="car" ref="car"></property>
<!-- 或
<property name="car">
<ref bean="car"></ref>
</property>
-->
<!-- 或内部bean -->
<property name="car">
<bean class="com.atguigu.spring.beans.Car">
<!-- 省略。。。 -->
</bean>
</property>
</bean>
二、通过<list>、<map>、<property>、<util>集合标签进行XML文件的配置:
<!-- 使用list节点 为List类型的属性赋值 -->
<bean id="person3" class="com.atguigu.spring.beans.collection.Person">
<property name="name" value="zf"></property>
<property name="age" value="26"></property>
<property name="cars">
<list>
<ref bean="car"/>
</list>
</property>
</bean>
<!-- 使用map节点 及map的entry子节点配置Map类型的成员变量 -->
<bean id="newperson" class="com.atguigu.spring.beans.collection.NewPerson">
<property name="name" value="nn"></property>
<property name="age" value="1"></property>
<property name="cars">
<map>
<entry key="AA" value-ref="car"></entry>
</map>
</property>
</bean>
<!-- 配置Properties -->
<bean id="dataSource" class="com.atguigu.spring.beans.collection.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">1234</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean> <!-- 配置单例的集合bean,以供多个bean进行引用 ,需导入util命名空间-->
<util:list id="cars">
<ref bean="car"/>
</util:list>
<bean id="person4" class="com.atguigu.spring.beans.collection.Person">
<property name="name" value="gg"></property>
<property name="age" value="0"></property>
<property name="cars" ref="cars"></property>
</bean> <!-- 通过p命名空间为bean的属性赋值,需先导入p命名空间 -->
<bean id="person5" class="com.atguigu.spring.beans.collection.Person"
p:name="ljd" p:age="24" p:cars-ref="cars"></bean>
</beans>
三、可以使用autowire属性指定自动装配的方式:
——byName 根据bean的名字和当前bean的setter风格的属性名进行自动装配
——byType 根据bean的类型和当前bean的属性的类型进行自动装配
四、bean的继承(parent)和依赖(depends-on)
<!-- 抽象bean:bean的abstract属性为true的bean.这样的bean不能被IOC容器实例化,只用来被继承配置
若某一个bean的class属性没有指定,则该bean必须是一个抽象(abstract)的bean -->
<bean id="address" class="com.atguigu.spring.beans.relation.Address"
p:city="zhengzhou" p:street="yihaoxian" abstract="true"></bean>
<!-- bean配置的继承:使用bean的parent属性指定继承哪个bean 的配置 -->
<bean id="address2" class="com.atguigu.spring.beans.relation.Address"
parent="address" p:street="zibailu"></bean> <bean id="car" class="com.atguigu.spring.beans.collection.Car" p:carName="luhu"
p:carPrice="750000" p:carType="suv" p:carCount="1"></bean>
<!--要求再配置person时,必须有一个关联的car,换句话说Person这个bean依赖于Car这个bean -->
<bean id="person" class="com.atguigu.spring.beans.relation.Person"
p:name="lee" p:home-ref="address2" depends-on="car"></bean>
五、使用bean的 scope属性来配置作用域 :
singleton:默认值.容器初始化时创建bean实例,在整个容器的生命周期内只创建一个bean,单例的
prototype:原型的.容器初始化时不创建bean实例,而在每次请求时都创建一个新的bean实例,并返回
<bean id="person6" class="com.atguigu.spring.beans.collection.Person"
scope="prototype">
<property name="name" value="jim"></property>
<property name="age" value="10"></property>
</bean>
.java:
//创建spring的IOC容器对象
//ApplicationContext 代表IOC容器
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
//从IOC容器中获取Bean实例
//通过id获取IOC容器中的bean
HelloWorld helloWorld = (HelloWorld)act.getBean("helloworld");
//利用类型返回IOC容器中的bean(要求IOC容器中必须只有一个该类型的Bean)
HelloWorld helloWorld2 = act.getBean(HelloWorld.class);
获取配置文件bean中的值
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mybatis.xml");
ComboPooledDataSource pool= (ComboPooledDataSource) ctx.getBean("dataSource");
jdbcUser = pool.getUser();
jdbcPassword = pool.getPassword();
jdbcUrl = pool.getJdbcUrl();
driverClass = pool.getDriverClass();
initialPoolSize = pool.getInitialPoolSize();
maxPoolSize = pool.getMaxPoolSize();
spring-IOC容器(一)的更多相关文章
- Spring IoC容器的初始化过程
Spring IoC容器的初始化包括 BeanDefinition的Resource定位.载入和注册 这三个基本的过程.IoC容器的初始化过程不包含Bean依赖注入的实现.Bean依赖的注入一般会发生 ...
- 【Spring】非Spring IOC容器下获取Spring IOC上下文的环境
前言 在Spring Web项目中,有些特殊的时候需要在非Spring IOC容器下获取Spring IOC容器的上下文环境,比如获取某个bean. 版本说明 声明POM文件,指定需引入的JAR. & ...
- 学习Spring(一) 实例化Spring IoC容器
实例化Spring IoC容器 1,读取其配置来创建bean实例 2,然后从Spring IoC容器中得到可用的bean实例 Spring提供两种IoC容器实现类型 a,一种为bean工厂 b,应用程 ...
- MyEclipse Spring 学习总结一 Spring IOC容器
一.Spring IOC容器---- Spring AllicationContext容器 程序的结构如下: 1.首先在MyEclipse 创建创建Java Project 2.创建好后,添加spin ...
- 对Spring IoC容器实现的结构分析
本文的目标:从实现的角度来认识SpringIoC容器. 观察的角度:从外部接口,内部实现,组成部分,执行过程四个方面来认识SpringIoC容器. 本文的风格:首先列出SpringIoC的外部接口及内 ...
- spring IOC容器实例化Bean的方式与RequestContextListener应用
spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype 每次从容器中调用Bean时, ...
- 解读Spring Ioc容器设计图
在Spring Ioc容器的设计中,有俩个主要的容器系列:一个是实现BeanFactory接口的简单容器系列,这系列容器只实现了容器最基本的功能:另外一个是ApplicationContext应用上下 ...
- 纯注解快速使用spring IOC容器
使用spring的ioc容器实现对bean的管理与基本的依赖注入是再经典的应用了.基础使用不在详述. 这里主要介绍下使用注解实现零配置的spring容器.我相信你也会更喜欢使用这种方式.Spring ...
- Spring IOC容器分析(2) -- BeanDefinition
上文对Spring IOC容器的核心BeanFactory接口分析发现:在默认Bean工厂DefaultListableBeanFactory中对象不是以Object形成存储,而是以BeanDefin ...
- Spring IOC容器分析(4) -- bean创建获取完整流程
上节探讨了Spring IOC容器中getBean方法,下面我们将自行编写测试用例,深入跟踪分析bean对象创建过程. 测试环境创建 测试示例代码如下: package org.springframe ...
随机推荐
- python实现图的遍历(递归和非递归)
class graph: def __init__(self,value): self.value=value self.neighbors=None # 图的广度优先遍历 # 1.利用队列实现 # ...
- 初识linux------用户和用户组
事先说明 本Linux的版本为Ubuntu. 为避免一些初学者由于权限问题特此事先说明,在非root权限下时,所有的代码加sudo:如下 (1)不在root权限 sudo useradd -m 用户名 ...
- java学习笔记12(final ,static修饰符)
final: 意思是最终的,是一个修饰符,有时候一个功能类被开发好了,不想被子类重写就用final定义, 用final修饰的最终数据成员:如果一个类的数据成员用final修饰符修饰,则这个数据成员就被 ...
- 2--Jmeter 4.0--Excel 数据驱动 接口测试
Excel 模板 通过jmeter的csv data set config 读取 Jmeter注意事项 (1)数据驱动 1..JDBC :SQL 存储在excel中,无法将where条件对应的jmet ...
- 链表 c实现
linklist.h #ifndef _LINKLIST_H_ #define _LINKLIST_H_ typedef int data_t; typedef struct node{ data_t ...
- JavaScript中HTML DOM focus()与onblur() setSelectionRange()用法
今天在写一个todolist待办事项项目,需要单击编辑待办事项的内容,百度搜了一下这几个方法的用法,总结一下 focus()方法:获得键盘焦点,单击之后就调用绑定的js方法,在span标签里面加一个输 ...
- zedboard开发板上移植opencv代码(立体匹配)
前言 公司要做立体匹配相关的项目,已有matlab和c++版本,可是不能做到实时显示立体信息,想要硬件实现实时,无奈本渣也是个硬件的新手,先按照实验室lyq同学的思路在zedboard开发板的纯ARM ...
- Gym - 101806T: Touch The Sky(贪心)
Figure: The house floats up in the sky by balloons. This picture is also used in 2018 KAIST RUN Spri ...
- 软件安装配置笔记(二)——SQL Server安装
客户端安装: 服务器端安装:
- 洛谷P1070 道路游戏(dp+优先队列优化)
题目链接:传送门 题目大意: 有N条相连的环形道路.在1-M的时间内每条路上都会出现不同数量的金币(j时刻i工厂出现的金币数量为val[i][j]).每条路的起点处都有一个工厂,总共N个. 可以从任意 ...