Spring的IOC(1)

来源:互联网 发布:餐厅成本核算软件 编辑:程序博客网 时间:2024/05/21 07:56
Spring的IOC

Spring的核心之IOC

1.IOC的概念:

a.概念
inverse of control

  b.实现方式之一

     DI(Dependency Injection)

       2.  配置bean:

a.配置bean
xml形式或者注解的方式
b.配置bean的方式
类全名(反射)
工厂方法(静态工厂方法和实例工厂方法)
自动装配
c.BeanFactory和ApplicationContext
d.依赖注入的方式:
构造器注入
    属性注入
    接口注入
e.注入属性值
字面值
引用其他的bean
null值和级联属性
List集合属性
Set集合属性
Map集合属性
properties属性
f.bean之间的关系
继承和依赖
g.bean的作用域
singleton,prototype,web环境
h.使用外部属性文件
 i. SpEL(spring的表达式语言)
j.bean的生命周期
k.bean的泛型依赖注入
b.配置bean的方式:
通过类全名反射
<bean  id="student"  class="org.stvrandolph.spring.entity.Student"></bean>

工厂方法     
public class StudentFactory {//获得student对象的静态方法public static Student getStudent1(){Studnet student = new Student(1,"zhangwuji",25,true,"12002310");}return student;
//获得学生对象的普通方法
public Student getStudent2(){
Student student = new Student(2,"zhaomin",25,false,"12002310");
} return student;
}
工厂方法(静态工厂方法)
<bean id="student" class="org.stvrandolph.spring.entity.StudentFactory" factory-method="getStudent1"></bean>
(实例工厂方法:要比静态工厂方法多一个工厂的bean)
<bean id="studentFactory" class="org.stvrandolph.spring.entity.StudentFactory"></bean>
<bean id="studnet" factory-bean="studentFactory" factory-method="getStudent2"></bean>
bean的自动装配:
byname:
根据名称自动装配。如果有一个类里面有一个属性叫student,如果容器中正好有一个名为student的bean,spring就会自动将其装配给这个类的这个属性。


bytype:
根据类型自动装配。如果一个类里面有一个属性的类型是Student,如果容器正好有一个类型是Student的bean,spring将会自动将其装配给这个类的这个属性。



constructor:
与bytype类似,只不过他是针对构造器注入而言的。如果有一个类有一个构造方法,其中包含了一个Student类型的形参,如果容器中有一个Student的bean,spring将自动把这个bean作为这个类的这个构造方法的入参;如果没有就是null,不推荐使用这种方式。
自动装配的确定:
a.自动装配会装配符合条件的所有bean,如果只需要装配部分的bean,就不能满足该需求了。
b.自动装配只能是bytype或者是byname,不能两者兼有。
c.除了是框架自身使用自动装配以外,通常情况下实际项目中很少使用自动装配,可读性没有直接的明确的配置好。


BeanFactory和ApplicationContext
a.在SpringIOC容器读取Bean配置创建bean实例之前,必须要先实例化springIOC容器本身,只有在容器实例化之后,才可以从IOC容器里面获取bean实例并使用。
b.spring提供了两种类型的IOC容器的实现(实际上不止两种,还有好多的接口和抽象类,但是这些抽象类是spring内部自身使用的)
(1)beanfactory(ioc容器的基本实现) 
(2)applicationcontext(提供了更多的高级特性,是beanfactory的子接口)
c.beanfactory是spring框架的最基本的接口,面向spring框架本身,applicationcontext面向spring框架的开发者,几乎所有的应用场合都是直接使用applicationcontext,而并非使用更加底层的beanfactory,但是不管使用哪个接口的实现对象,xml文件配置都是一样的,并没有什么不同。
依赖注入的方式:
1.构造器注入
a.被注入的Javabean必须提供对应的构造方法
b.注意事项
必须提供对应数量类型的构造方法
自动封解箱的功能在此失效
type属性可以被index属性替换,值从0开始。



依赖注入的方式:
构造器注入
<bean  id="student1" class = "org.stvrandolph.spring.entity.Student">
< constructor-arg value="1" type="java.lang.Integer" />
< constructor-arg value="zhangsan" type="java.lang.String">
< constructor-arg value="true" type="java.lang.Boolean">
< constructor-arg value="25" type="java.lang.Integer">
< constructor-arg value="no-1" type="java.lang.String">
</bean>

<bean  id="student2" class = "org.stvrandolph.spring.entity.Student">
< constructor-arg value="1" type="java.lang.Integer" />
< constructor-arg value="lisi" type="java.lang.String">
< constructor-arg value="true" type="java.lang.Boolean">
< constructor-arg value="24" type="java.lang.Integer">
< constructor-arg value="no-2" type="java.lang.String">
</bean>

属性注入:
a.被注入的Javabean必须提供对应的写方法(set方法)
b.属性注入的方式是最简单的方式,也是使用最对的方式
    配置文件:
<?xml version="1.0" encoding ="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLschema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
<bean id="students3" class="org.stvrandolph.spring.entity.Student">
<property name="id" value="1"></property>
<property name="name" value="zhangsan"></property>
<property name="age" value="25"></property>
<property name="gender" value="true" ></property>
<property name="studyNumber" value="no-1"></property>
<property name="address" ref="address"></property>
</bean>
上面这种方法正常使用,
下面的这个方法需要使用p命名空间xmlns:xsi="http://www.w3.org/2001/XMLschema/p"
<bean id="student4" class="org.stvrandolph.spring.entity.Student"
p:id="1"
p:name="zhangsan"
p:age="26"
p:gender="true"
p:studyNumber="no-1"
p:address-ref="address"/>........



接口注入
a.要获取的javabean要实现一个接口
b.客户端获取的时候强行转成接口类型
c.xml的配置与属性注入一模一样

字面值:
1.既可以使用value属性,也可以使用value子元素
2.如果属性值有特殊值,需要借助<![CDATA[<-_->]]>

注入属性的值:
1.引用其他的bean
<bean id="addrsss" class="......Address"><property name="id"  value="1"></property>...</bean><bean id="studnet" class="....Student">....(属性值的配置)    <property name="address" ref="address">

引用其他的bean
引用内部bean
内部bean不需要配置id属性
内部bean不可以被外部其他bean引用
<bean id="student" class="......">student下面的属性配置省略<property name="address"><bean class="....."><property name="id" value="1"><property name="province" value="jiangsu">....</bean></property></bean>

null值和级联属性:
a.使用<null/>标签注入NULL值(直接不配置就是null值)
b.支持级联属性的配置
级联的属性必须先初始化后才能被赋值
<bean id="student" class="...."><property name="id" value="1"></property><property name="studyNumber"><null/></property>...</bean>
list集合属性
使用<list>标签
<value>配置简单的常量值
<ref>配置对其他bean的引用
<bean>配置使用内置bean
<null/>配置空元素
可以内嵌其他集合
数组的定义和list一样,都使用<list>
list集合属性:
<property name="list"><list><value type="int">1</value><value type="double">3.14</value><value type="boolean"> true</value><value type="char">c</value><value type="java.lang.String"> hello world</value><ref bean="address"></list></property>

set集合属性:
使用set标签
具体配置内容与list一样

<map>集合属性:
使用<map>标签
使用<entry>标签作为子标签,每个条目包含一个键和一个值。
key属性:普通的键值
     value属性:普通的值
vlaue-type:value值的类型
key-ref属性:引用其他bean的作为键
值可以使用<null/>标签和内部bean<bean>
<property name="map"><map><entry key="1" value="1"  vlaue-type="int"><entry/><entry key="2" value="2" value-type="java.lang.Integer"><entry key="3" value-ref="address"></entry><entry key="4"><null/><entry/><entry key="address" ><bean class="org.stvrandolph.spring.entity.Address"><property name="id" value="1"></property><property name="province" value="jiangsu"></property></bean></entry><entry key-ref="address" value="5" value-type="int"></entry><entry key-ref="address" value="6"  value-type="java.lang.Integer"/><entry key-ref="address" value-ref="address"></entry></map></property>
Properties属性:
使用props标签
使用prop作为一个子标签表示一个key-value对
prop标签必须定义key属性‘
<property name=properties>
<props>
<prop key="1">1</prop>
<prop key="2">2</prop>
</props>
</property>
集合属性:
使用基本的标签定义集合时,不能将集合作为独立的bean定义,导致其他bean无法引用该集合,所以无法在不同bean之间共享集合。
可以使用util schema里的集合标签定义独立的集合bean,前提需要util命名空间
<>util:constant
<>util:list
<>util:map
<>util:properties
<>util:property-path
<>util:set






原创粉丝点击