SpringIOC容器(1)

Spring 是一个开源框架.

Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.

Spring 是一个 IOC(DI) AOP 容器框架.

Spring特点轻量级.依赖注入.面向切面编程.容器.一站式

轻量级:Spring 是非侵入性的 - 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API

依赖注入(DI --- dependency injection、IOC)

面向切面编程(AOP --- aspect oriented programming)

容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期

框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. Spring 中可以使用 XML Java 注解组合这些对象

一站式:在 IOC AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC

Spring所需jar包commons-logging,log4j,spring-beans,spring-context,spring-core,spring-expression

前面两个是后边所需依赖jar包

配置xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd ">

</beans>

 

Bean属性ID

IOC 容器中必须是唯一的

id 没有指定,Spring 自动将权限定性类名作为 Bean 的名字

id 可以指定多个名字,名字之间可用逗号、分号、或空格分隔

 <bean id="apple" class="com.zzxtit.spring.ioc.di.Apple">

Spring IOC 容器可以管理 Bean 的生命周期, Spring 允许在 Bean 生命周期的特定点执行定制的任务.

Spring IOC 容器对 Bean 的生命周期进行管理的过程:

通过构造器或工厂方法创建 Bean 实例

Bean 的属性设置值和对其他 Bean 的引用

调用 Bean 的init-method方法

Bean 可以使用了

当容器关闭时, 调用 Bean 的destory-method方法

 <bean id="apple" class="com.zzxtit.spring.ioc.dev.Apple"  init-method="appleInit" destroy-method="appleEaten"></bean>  

 

Spring , 可以在 <bean> 元素的 scope 属性里设置 Bean 的作用域.

 <bean id="apple" class="com.zzxtit.spring.ioc.dev.Apple" scope="prototype" ></bean>  

如果scope属性没有值,则为默认的Singleton,每个bean只有一个对象

ApplicationContext ioc=new ClassPathXmlApplicationContext("config/applicationContext-di.xml");

当赋值为:prototype 表示对象在被获取时IOC容器才实例化对象

ConstructorDI cons=ioc.getBean("constructorDi",ConstructorDI.class);

如果scope属性值为Prototype,则在执行ioc的getbean()方法时创建对象

Banana ba=ioc.getBean("banana",Banana.class);

可以在xml中通过属性注入为对象属性赋值,属性注入使用 <property> 元素, 使用 name 属性指定 Bean 的属性名称,value 属性或 <value> 子节点指定属性值,bean类中需要属性的setter方法通过此方法向对象中注入属性,

(注意,使用属性注入方法时,需要默认的无参构造方法) 

构造器注入构造器注入在 <constructor-arg> 元素里声明属性。

有两种方式1按照参数顺序,使用index属性,从0开始

 

2按照参数类型的顺序,进行注入:使用属性type, 基本数据类型直接写类型名称,String可以写成String 或者java.lang.String, 引用类型需要写全类名。

   <bean id="fruit" class="com.zzxtit.springboot.ioc.di.fruit">
   		<constructor-arg index="0" value="苹果" ></constructor-arg>
   		<constructor-arg index="1" value="香蕉" ></constructor-arg>
   		<constructor-arg index="2" value="橘子" ></constructor-arg>
   </bean>

基本数据类型输入有两种方式

1value属性赋值

2value标签.

   <bean id="fruit" class="com.zzxtit.springboot.ioc.di.fruit">
   		<property name="apple" value="苹果"></property>
   		<property name="banana" >
                <value>香蕉</value>
                </property>
   </bean> 

 

如果数据包含特殊符号可以使用 <![CDATA[]]>把数值包裹起来

   <bean id="fruit" class="com.zzxtit.springboot.ioc.di.fruit">

   		<property name="apple"><value><![CDATA[苹果<符号>测试]]> </value></property>

   </bean>

 

可以通过 <ref> 元素或 ref  属性为 Bean 的属性或构造器参数指定对 Bean 的引用 .
 
 
   <bean id="fruit" class="com.zzxtit.springboot.ioc.di.fruit">
   		<property name="appple" ref="apple"></property>

   </bean>

 

配置 java.util.List 类型或者数组的属性 , 需要指定 <list>  标签。
 
 
   		<property name="fruitList">
   			<list>
   				<value>苹果</value>
   				<value>香蕉皮</value>
   				<value>橙子</value>
   			</list>
   		</property>

 

配置 java.util.Set 需要使用 <set> 标签。
 
   		<property name="fruitset" >
   			<set>
   				<value>苹果</value>
   				<value>香蕉皮</value>
   				<value>橙子</value>
   			</set>
   		</property>

 

 
Java.util.Map 通过 <map> 标签定义 , <map> 标签里可以使用多个 <entry> 作为子标签 . 每个条目包含一个键和一个值。
 
   		<property name="fruitMap">
   			<map>
   				<entry key="apple" value="苹果"></entry>
   				<entry key="banana" value="香蕉"></entry>
   				<entry key="orange" value="橙子"></entry>
   			</map>
   		</property>

 

 
使用 <props> 定义 java.util.Properties , 该标签使用多个 <prop> 作为子标签。
   		<property name="fruits">
   			<props>
   				<prop key="apple">苹果</prop>
   				<prop key="banana">香蕉</prop>
   				<prop key="orange">橘子</prop>
   			</props>
   		</property>
 
 
使用 util schema 里的集合标签定义独立的集合 Bean ,可以被公用。
使用util标签之前,必须加上UTIL命名空间及配置文件
	xmlns:util="http://www.springframework.org/schema/util"
	
    xsi:schemaLocation="
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd
        ">
   <util:list id="stuList">
 		<value>张世娇</value>
		<value>李白</value>
		<value>杜甫</value>
   </util:list>

 

 
Bean 之间的关系。
继承
   <bean id="watermelon" parent="fruit">
   		<property name="watermelon" value="西瓜">
   			
   		</property>
   </bean>
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值