内容包括:配置Bean、spring容器、获取bean 、属性注入 、bean的作用域 、spring使用外部属性文件
一:配置Bean
class:bean的全类名,通过反射的方式在IOC容器中创建bean,所以要求Bean中必须有无参数的构造器。
id:标识容器中的bean的id
<bean id="HelloWorld" class="com.wh.spring.beans.HelloWorld">
<property name="name2" value="spring"></property>
二:spring容器
beanFactory是IOC的基本实现,是Spring框架的基础设施,面向spring本身。
ApplicationContext是beanFactory的子接口,面向spring框架的开发者。
无论使用何种方式,配置文件时相同的。
•ConfigurableApplicationContext 扩展于 ApplicationContext,新增加两个主要方法:refresh() 和 close(), 让 ApplicationContext 具有启动、刷新和关闭上下文的能力
•ApplicationContext 在初始化上下文时就实例化所有单例的 Bean。
•WebApplicationContext 是专门为 WEB 应用而准备的,它允许从相对于 WEB 根目录的路径中完成初始化工作
–ClassPathXmlApplicationContext:从 类路径下加载配置文件
–FileSystemXmlApplicationContext: 从文件系统中加载配置文件
三:获取bean
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
HeloWord helloword=(HeloWord) ctx.getBean("HelloWorld");
也可以使用getBean(HelloWorld.class),这时候不需要强制类型转换。
//根据类型来获取 bean 的实例: 要求在 IOC 容器中只有一个与之类型匹配的 bean, 若有多个则会抛出异常.
//一般情况下, 该方法可用, 因为一般情况下, 在一个 IOC 容器中一个类型对应的 bean 也只有一个.
// HelloWorld helloWorld1 = ctx.getBean(HelloWorld.class);
四:属性注入
setter方法注入。
<property name="user" value="Tom"></property>
constructor-arg注入
<constructor-arg value="Mike"></constructor-arg>
可以通过构造器属性的类型和参数的位置区分重载的构造器。
<bean id="car" class="com.wh.spring.beans.Car">
<constructor-arg type="java.lang.String">
<value><![CDATA[<SHANGHAI>]]></value>
</constructor-arg>
</bean>
字面值包含特殊字符,使用<![CDATA]>包含。
bean引用:
<!-- 通过 ref 属性值指定当前属性指向哪一个 bean! -->
<property name="dao" ref="dao5"></property>
内部bean
<!-- 声明使用内部 bean -->
<bean id="service2" class="com.atguigu.spring.ref.Service">
<property name="dao">
<!-- 内部 bean, 类似于匿名内部类对象. 不能被外部的 bean 来引用, 也没有必要设置 id 属性 -->
<bean class="com.atguigu.spring.ref.Dao">
<property name="dataSource" value="c3p0"></property>
</bean>
</property>
</bean>
赋空值:
<constructor-arg><null/></constructor-arg>
为级联属性赋值:
<property name="service" ref="service2"></property>
<!-- 设置级联属性(了解) -->
<property name="service.dao.dataSource" value="DBCP2"></property>
装配集合属性:
spring可以通过list,map,set装配集合属性。
<!-- 装配集合属性 -->
<bean id="user" class="com.atguigu.spring.helloworld.User">
<property name="userName" value="Jack"></property>
<property name="cars">
<!-- 使用 list 元素来装配集合属性 -->
<list>
<ref bean="car"/>
<ref bean="car2"/>
</list>
</property>
</bean>
util声明:
<!-- 声明集合类型的 bean -->
<util:list id="cars">
<ref bean="car"/>
<ref bean="car2"/>
</util:list>
自动装配:
byType: 根据类型进行自动装配. 但要求 IOC 容器中只有一个类型对应的 bean, 若有多个则无法完成自动装配.
byName: 若属性名和某一个 bean 的 id 名一致, 即可完成自动装配. 若没有 id 一致的, 则无法完成自动装配
在 Bean 配置文件里设置 autowire 属性进行自动装配将会装配 Bean 的所有属性. 然而, 若只希望装配个别属性时, autowire 属性就不够灵活了.
autowire 属性要么根据类型自动装配, 要么根据名称自动装配, 不能两者兼而有之.
使用bean的parent属性进行配置继承,。
<bean id="parent" class="xx.xx.xx"></bean>
<bean id="child" parent="parent"></bean>
Spring 允许继承 bean 的配置, 被继承的 bean 称为父 bean. 继承这个父 Bean 的 Bean 称为子 Bean
子 Bean 从父 Bean 中继承配置, 包括 Bean 的属性配置
子 Bean 也可以覆盖从父 Bean 继承过来的配置
父 Bean 可以作为配置模板, 也可以作为 Bean 实例. 若只想把父 Bean 作为模板, 可以设置 <bean> 的abstract 属性为 true, 这样 Spring 将不会实例化这个 Bean
并不是 <bean> 元素里的所有属性都会被继承. 比如: autowire, abstract 等.
也可以忽略父 Bean 的 class 属性, 让子 Bean 指定自己的类, 而共享相同的属性配置. 但此时 abstract 必须设为 true
五:bean的作用域
单例bean:scope="singleton"(默认情况)
每次向容器获取bean,都返回同一个bean,创建容器时创建bean.
原型bean:scope="prototype"
每次向容器获取bean,都返回一个新创建的bean,创建容器时不创建bean
六:spring使用外部属性文件
Spring 提供了一个 PropertyPlaceholderConfigurer 的 BeanFactory 后置处理器, 这个处理器允许用户将 Bean 配置的部分内容外移到属性文件中. 可以在 Bean 配置文件里使用形式为 ${var} 的变量, PropertyPlaceholderConfigurer 从属性文件里加载属性, 并使用这些属性来替换变量.
<context:property-placeholder
location="classpath:db.properties"/>