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>
<bean id="fruit" class="com.zzxtit.springboot.ioc.di.fruit">
<property name="appple" ref="apple"></property>
</bean>
<property name="fruitList">
<list>
<value>苹果</value>
<value>香蕉皮</value>
<value>橙子</value>
</list>
</property>
<property name="fruitset" >
<set>
<value>苹果</value>
<value>香蕉皮</value>
<value>橙子</value>
</set>
</property>
<property name="fruitMap">
<map>
<entry key="apple" value="苹果"></entry>
<entry key="banana" value="香蕉"></entry>
<entry key="orange" value="橙子"></entry>
</map>
</property>
<property name="fruits">
<props>
<prop key="apple">苹果</prop>
<prop key="banana">香蕉</prop>
<prop key="orange">橘子</prop>
</props>
</property>
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 id="watermelon" parent="fruit">
<property name="watermelon" value="西瓜">
</property>
</bean>