目录
一、概述
1.什么是Spring
2.作用
二.Spring中的Bean配置
1.IOC容器里配置bean
2.Spring容器
3.ApplicationContext
4.从IOC容器中获取Bean
5.依赖注入的方式
(1)属性注入
(2)构造器注入
6.字面值
7.引用其它Bean
8.注入参数:null值和级联属性
9.集合属性(set、list)
10.集合属性(map)
11.使用utility schema定义集合
12.使用p命名空间
一、概述
1.什么是Spring
Spring是一个开源框架,由Rod Johnson创建。简单来说,是一个分层的JavaSE/EE full-stack(一站式)轻量级开源框架。
2.作用
- 轻量级:Spring是非侵入性的
- 依赖注入(IOC)
- 面向切面编程(AOP)
- 容器
- 框架
- 一站式框架:有EE开发的每一层解决方案
- WEB层:SpringMVC
- Service层:Spring的Bean管理,Spring声明式事务
- DAO层:Spring的Jdbc模板,Spring的ORM模块
二.Spring中的Bean配置
1.IOC容器里配置bean
public class HelloWorld {
private String name;
public HelloWorld() {
}
public HelloWorld(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void hello(){
System.out.println("hello,"+name);
}
}
<!-- 通过全类名的方式来配置bean的属性
id:Bean的名称
--- 在IOC容器中必须是唯一的
--- 若id没有指定,Spring自动将权限限定性类名作为Bean的名称
--- id可以指定多个名字,名字之间用逗号、分号或空格隔开
-->
<bean id="helloworld" class="com.itheima.spring.HelloWorld">
<!-- 为属性赋值 -->
<!-- 通过属性注入: 通过 setter 方法注入属性值 -->
<property name="name" value="Spring"></property>
</bean>
2.Spring容器
- 在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前, 必须对它进行实例化. 只有在容器实例化后, 才可以从 IOC 容器里获取 Bean 实例并使用
- Spring 提供了两种类型的 IOC 容器实现
- BeanFactory: IOC 容器的基本实现
- ApplicationContext: 提供了更多的高级特性. 是 BeanFactory 的子接口
- BeanFactory 是 Spring 框架的基础设施,面向 Spring 本身;ApplicationContext 面向使用 Spring 框架的开发者,几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory
3.ApplicationContext
- 实现类
- ClassPathXmlApplicationContext:从类路径下加载配置文件
- FileSystemXmlApplicationContext:从文件系统中加载配置文件
- ApplicationContext在初始化上下文时就实例化了所有单例的Bean对象
- ConfigurableApplicationContext是ApplicationContext的子类,新增了两个方法:
- refresh()
- close()
4.从IOC容器中获取Bean
//创建Spring的IOC容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
//从容器中获取Bean
HelloWorld helloworld = (HelloWorld) applicationContext.getBean("helloworld");
//调用方法
helloworld.hello();
5.依赖注入的方式
(1)属性注入
- 通过setter方法注入Bean的属性值或依赖的对象
- 使用<property>元素指定属性值
<property name="name" value="Spring"></property>
- 使用value属性指定属性值
<bean id="car1" class="com.itheima.spring.Car">
<property name="company"><value>American</value></property>
<property name="brand"><value>aodi</value></property>
<property name="maxSpeed"><value>200</value></property>
<property name="price"><value>180000</value></property>
</bean>
(2)构造器注入
- 按索引匹配
<!-- 通过构造器注入属性值 -->
<!-- 若一个 bean 有多个构造器, 如何通过构造器来为 bean 的属性赋值 -->
<!-- 可以根据 index 和 type 进行更加精确的定位-->
<bean id="car" class="com.itheima.spring.Car">
<constructor-arg value="baoma" index="0"></constructor-arg>
<constructor-arg value="deguo" index="1"></constructor-arg>
<constructor-arg value="3600000" index="2"></constructor-arg>
<constructor-arg value="180.55" index="3"></constructor-arg>
</bean>
- 按类型匹配
<bean id="car2" class="com.itheima.spring.Car">
<constructor-arg value="baoma1" type="java.lang.String"></constructor-arg>
<constructor-arg value="deguo1" type="java.lang.String"></constructor-arg>
<constructor-arg value="3600000" type="int"></constructor-arg>
<constructor-arg value="180.55" type="float"></constructor-arg>
</bean>
- 按name匹配
<bean id="car2" class="com.itheima.spring.Car">
<constructor-arg name="company" value="baoma1" ></constructor-arg>
<constructor-arg name="brand" value="deguo1"></constructor-arg>
<constructor-arg name="price" value="3600000"></constructor-arg>
<constructor-arg name="maxSpeed" value="180.55"></constructor-arg>
</bean>
6.字面值
- 字面值:可用字符串表示的值,可以通过 <value> 元素标签或 value 属性进行注入
- 基本数据类型及其封装类、String 等类型都可以采取字面值注入的方式
- 若字面值中包含特殊字符,可以使用 <![CDATA[]]> 把字面值包裹起来
<bean id="helloworld1" class="com.itheima.spring.HelloWorld">
<constructor-arg >
<value><![CDATA[<你好>]]></value>
</constructor-arg>
</bean>
7.引用其它Bean
- 组成应用程序的 Bean 经常需要相互协作以完成应用程序的功能. 要使 Bean 能够相互访问, 就必须在 Bean 配置文件中指定对 Bean 的引用
- 在 Bean 的配置文件中, 可以通过 <ref> 元素或 ref 属性为 Bean 的属性或构造器参数指定对 Bean 的引用
- 也可以在属性或构造器里包含 Bean 的声明, 这样的 Bean 称为内部 Bean
- 内部 Bean 声明直接包含在 <property> 或 <constructor-arg> 元素里, 不需要设置任何 id 或 name 属性
- 内部 Bean 不能使用在任何其他地方
<!-- 引用外部bean -->
<bean id="person" class="com.itheima.spring.Person">
<constructor-arg value="zhangsan"></constructor-arg>
<constructor-arg value="20"></constructor-arg>
<constructor-arg ref="car2"></constructor-arg>
</bean>
<!-- 引用内部bean -->
<bean id="person" class="com.itheima.spring.Person">
<property name="name" value="张三"></property>
<property name="age" value="20"></property>
<property name="cars">
<bean class="com.itheima.spring.Car">
<property name="company" value="China"></property>
<property name="brand" value="红旗"></property>
<property name="maxSpeed" value="225"></property>
<property name="price" value="5800000"></property>
</bean>
</property>
</bean>
8.注入参数:null值和级联属性
- 可以使用专用的 <null/> 元素标签为 Bean 的字符串或其它对象类型的属性注入 null 值
<property name="dataSource"><null/></property>
- Spring 支持级联属性的配置
9.集合属性(set、list)
- 在 Spring中可以通过一组内置的 xml 标签(例如: <list>, <set> 或 <map>) 来配置集合属性
- 配置 java.util.List 类型的属性, 需要指定 <list> 标签, 在标签里包含一些元素. 这些标签可以通过 <value> 指定简单的常量值, 通过 <ref> 指定对其他 Bean 的引用. 通过<bean> 指定内置 Bean 定义. 通过 <null/> 指定空元素. 甚至可以内嵌其他集合
- 数组的定义和 List 一样, 都使用 <list>
- 配置 java.util.Set 需要使用 <set>
<bean id="user" class="com.itheima.spring.User">
<property name="name" value="张三"></property>
<property name="cars">
<!-- 使用 list 元素来装配集合属性 -->
<list>
<ref bean="car1"/>
<ref bean="car2"/>
</list>
</property>
</bean>
10.集合属性(map)
- Java.util.Map 通过 <map>
- 必须在 <key>
- 因为键和值的类型没有限制, 所以可以自由地为它们指定 <value>, <ref>, <bean> 或 <null>
- 可以将 Map 的键和值作为 <entry> 的属性定义: 简单常量使用 key 和 value 来定义; Bean 引用通过 key-ref 和 value-ref 属性定义
- 使用 <props> 定义 java.util.Properties, 该标签使用多个 <prop> 作为子标签. 每个 <prop> 标签必须定义 key 属性
<bean id="userMap" class="com.itheima.spring.UserMap">
<property name="name" value="张三"></property>
<property name="cars">
<!-- 使用 map 元素来装配集合属性 -->
<map>
<entry>
<key><value>0001</value></key>
<ref bean="car"></ref>
</entry>
<entry>
<key><value>0002</value></key>
<ref bean="car1"></ref>
</entry>
</map>
</property>
</bean>
11.使用utility schema定义集合
<!-- 声明集合类型的 bean -->
<util:list id="cars">
<ref bean="car"/>
<ref bean="car2"/>
</util:list>
<bean id="user2" class="com.itheima.spring.User">
<property name="name" value="Rose"></property>
<!-- 引用外部声明的 list -->
<property name="cars" ref="cars"></property>
</bean>
12.使用p命名空间
- Spring 从 2.5 版本开始引入了一个新的 p 命名空间,可以通过 <bean> 元素属性的方式配置 Bean 的属性
- 使用 p 命名空间后,基于 XML 的配置方式将进一步简化
<bean id="car3" class="com.itheima.spring.Car"
p:brand="aotuo" p:price="50000" p:company="America" p:maxSpeed="120">
</bean>