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 ...
随机推荐
- centos6.6安装hadoop-2.5.0(一、本地模式安装)
操作系统:centos6.6(一台服务器) 环境:selinux disabled:iptables off:java 1.8.0_131 安装包:hadoop-2.5.0.tar.gz hadoop ...
- ChinaCock界面控件介绍-CCNewsSilder
上图是控件包里的Demo运行效果,轮播新闻图片. 这个控件用起来简单,拖放一个CCNewsSiler到Form上,设置Align为Top,再设置好高度,然后用代码加载图片: procedure TFo ...
- kbmMW SmartService控制返回类型
- Spring、Commons的BeanUtils.copyProperties用法
如果两个对象A.B的大部分属性的名字都一样,此时想将A的属性值复制给B,一个一个属性GET\SET代码量太大,可以通过复制属性的方式减小工作量,同时代码看起来更加简洁明了,复制属性可以用Spring或 ...
- js- 类数组对象
JavaScript中,数组是一个特殊的对象,其property名为正整数,且其length属性会随着数组成员的增减而发生变化,同时又从Array构造函数中继承了一些用于进行数组操作的方法. 而对于一 ...
- SQL注入之Sqli-labs系列第二关
废话不在多说 let's go! 继续挑战第二关(Error Based- Intiger) 同样的前奏,就不截图了 ,and 1=1和and 1=2进行测试,出现报错 还原sql语句 查看源代 ...
- options.html:1 Refused to load the script 'xxxx' because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:".
/********************************************************************************* * options.html:1 ...
- Twisted 安装
1,官网: https://www.twistedmatrix.com/trac/ 2,下载 https://twistedmatrix.com/Releases/Twisted/18.7/Twist ...
- HihoCoder - 1807:好的数字串 (KMP DP)
Sample Input 6 1212 Sample Output 298 给定一个数字字符串S,如果一个数字字符串(只包含0-9,可以有前导0)中出现且只出现1次S,我们就称这个字符串是好的. 例如 ...
- lecture4特征提取-七月在线-cv
霍夫变换 http://blog.csdn.net/sudohello/article/details/51335237 http://blog.csdn.net/glouds/article/det ...