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容器(一)的更多相关文章

  1. Spring IoC容器的初始化过程

    Spring IoC容器的初始化包括 BeanDefinition的Resource定位.载入和注册 这三个基本的过程.IoC容器的初始化过程不包含Bean依赖注入的实现.Bean依赖的注入一般会发生 ...

  2. 【Spring】非Spring IOC容器下获取Spring IOC上下文的环境

    前言 在Spring Web项目中,有些特殊的时候需要在非Spring IOC容器下获取Spring IOC容器的上下文环境,比如获取某个bean. 版本说明 声明POM文件,指定需引入的JAR. & ...

  3. 学习Spring(一) 实例化Spring IoC容器

    实例化Spring IoC容器 1,读取其配置来创建bean实例 2,然后从Spring IoC容器中得到可用的bean实例 Spring提供两种IoC容器实现类型 a,一种为bean工厂 b,应用程 ...

  4. MyEclipse Spring 学习总结一 Spring IOC容器

    一.Spring IOC容器---- Spring AllicationContext容器 程序的结构如下: 1.首先在MyEclipse 创建创建Java Project 2.创建好后,添加spin ...

  5. 对Spring IoC容器实现的结构分析

    本文的目标:从实现的角度来认识SpringIoC容器. 观察的角度:从外部接口,内部实现,组成部分,执行过程四个方面来认识SpringIoC容器. 本文的风格:首先列出SpringIoC的外部接口及内 ...

  6. spring IOC容器实例化Bean的方式与RequestContextListener应用

    spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype 每次从容器中调用Bean时, ...

  7. 解读Spring Ioc容器设计图

    在Spring Ioc容器的设计中,有俩个主要的容器系列:一个是实现BeanFactory接口的简单容器系列,这系列容器只实现了容器最基本的功能:另外一个是ApplicationContext应用上下 ...

  8. 纯注解快速使用spring IOC容器

    使用spring的ioc容器实现对bean的管理与基本的依赖注入是再经典的应用了.基础使用不在详述. 这里主要介绍下使用注解实现零配置的spring容器.我相信你也会更喜欢使用这种方式.Spring ...

  9. Spring IOC容器分析(2) -- BeanDefinition

    上文对Spring IOC容器的核心BeanFactory接口分析发现:在默认Bean工厂DefaultListableBeanFactory中对象不是以Object形成存储,而是以BeanDefin ...

  10. Spring IOC容器分析(4) -- bean创建获取完整流程

    上节探讨了Spring IOC容器中getBean方法,下面我们将自行编写测试用例,深入跟踪分析bean对象创建过程. 测试环境创建 测试示例代码如下: package org.springframe ...

随机推荐

  1. mysql的sql语句的性能诊断分析

    1> explain SQL,类似于Oracle中explain语句 例如:explain select * from nad_aditem; 2> select benchmark(co ...

  2. Microsoft Project 常用快捷键

    任务升级 : ALT  +  SHIFT + 向左键 任务降级: ALT  +  SHIFT + 向右键 滚动到表头(第一个任务):Ctrl + HOME 滚动到表尾(最后一个任务):Ctrl + E ...

  3. 性能测试-9.Controller功能介绍

    Controller功能介绍 设计场景 配置场景 场景监控 一.设计场景 二.配置场景 规划场景(场景名.调度方式.运行方式.Initialize方式.Start Vusers方式.Duration方 ...

  4. endnote插入文献的一些问题

    1. 文末参考文献前面加上序号 eg.[1] 设置如下图,参考网址https://zhidao.baidu.com/question/360201207639069172.html edit-> ...

  5. input默认显示当前时间

    方法一: // 获取当天的年月日 new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().get ...

  6. CompletableFuture

    若你的意图是并发,而非并行,或者你的主要目标是在同一个CPU上执行几个松耦合的任务,充分利用CPU的核,让其足够忙碌,从而最大化程序的吞吐量,那么其实真正想做的避免因为等待远程服务的返回,或对数据库的 ...

  7. 前端笔记 (2.CSS)

    知识点借鉴于慕课网,菜鸟教程和w3shool CSS方面: CSS全称为“层叠样式表”,它主要是用于定义HTML内容在浏览器内的显示样式,如文字大小.颜色.字体加粗等. 使用CSS样式的一个好处是通过 ...

  8. SpringBatch Sample (一)(Hello World)

    通过前面两篇关于Spring Batch文章的介绍,大家应该已经对Spring Batch有个初步的概念了.这篇文章,将通过一个”Hello World!”实例,和大家一起探讨关于Spring Bat ...

  9. 【转载】 详解BN(Batch Normalization)算法

    原文地址: http://blog.csdn.net/hjimce/article/details/50866313 作者:hjimce ------------------------------- ...

  10. 小白入门photoscan

    1.安装 我装的是photoscanPro 1.4.5版本.[注]:刚开始是在官网上下载的,要收费就点了试用,结果当我等了一天把将近200张图片处理完后,告诉我试用版不能保存文件...(绝望-_- - ...