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 ...
随机推荐
- 漫步Java------接口
接口 一.定义 具有相同行为(方法),但是不相关的类 二.特点 只是提供方法,不定义方法的具体实现. 一个类只能继承一个父类,但是接口却可以继承多个接口. 接口是一个引用类型的变量 接口没有构造方法, ...
- 使用std::map和std::list存放数据,消耗内存比实际数据大得多
使用std::map和std::list存放数据,消耗内存比实际数据大得多 场景:项目中需要存储一个结构,如下程序段中TEST_DATA_STRU,结构占24B.但是使用代码中的std::list&l ...
- ios隐藏头部状态栏级tableview头部控件
- (BOOL)prefersStatusBarHidden { return YES; } self.tableView.separatorColor = [UIColor colorWithRed ...
- python scrapy爬虫存储数据库方法带去重步骤
import pymongo import requests import random import time import pymysql db = pymongo.MongoClient()[' ...
- xargs用法
xargs是一个很有用的命令,它可以实现并行,同&有异曲同工之妙,在大批量管理服务器时非常有用 xargs命令是给其他命令传递参数的一个过滤器,也是组合多个命令的一个工具.它擅长将标准输入数据 ...
- 网络流Dinic算法
我的模板 例题: https://vjudge.net/problem/HDU-4280 struct Edge { int lst; int from; int to; int cap; int f ...
- selenium和PhantomJS的使用
利用selenium来进行爬取数据 import time from selenium import webdriver # 创建phantomjs浏览器对象 driver = webdriver.P ...
- JS中的变量与常量
变量 1.创建变量 1.先声明,后赋值 使用var关键字进行变量的声明 使用=进行变量的赋值 自定义变量名 2.声明的同时赋值 var age = 20: 2.命名规范 1.由数字,字母,下划线和$组 ...
- [LeetCode&Python] Problem 167. Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- Oracle中nvl()、instr()、及执行多条sql事务操作
Oracle的Nvl函数 nvl( ) 函数 从两个表达式返回一个非null 值. 语法 NVL(eExpression1, eExpression2) 参数 eExpression1, eExpre ...