Spring:一个拥有控制反转(IOC)和面向切面(AOP)的容器框架,它使用俩种方式将Bean类及控制类装载入IOC容器里,
XML文件方式和注解方式,如下介绍的是XML文件方式。
目录
1、标签:用来绑定Bean类,生成一个Bean对象。一个Bean类可和多个绑定,即生成不同的对象。
2、添加外部集合标签(与同级),在xml的里添加如下属性
3、可以使用命名空间进行快速配置Bean类属性:在xml的里添加如下属性
4、<Bean>的配置继承:通过parent属性子Bean来继承父Bean的配置, 即父Bean已配置的属性可以不用再次配置。
抽象Bean(即属性abstract="true")不能被实例化,只能被继承配置
5、<Bean>的配置依赖:Spring同意用户通过depends-on属性设定Bean关联(前置依赖的Bean,关联Bean必须在本Bean实例化之前创建好(即配置文件要存在这个);假设前置依赖于多个Bean,则能够通过逗号或空格或的方式配置Bean的名称 ,类似UML图里的关联关系。
6、引用外部属性文件:可以在xml的里添加如下属性
7.SpringEL表达式(了解):可用来赋值、引用bean对象(通过id)、引用属性、引用方法、引用静态方法、计算表达式的值、 使用正则表达式。
8、Bean的后置处理器:允许在调用初始化方法前后对bean对象进行处理并返回。
9、工厂方法配置Bean的方法(了解)
一般在类路径下新建xml文件(即Bean文件)。
属性注入:即给属性赋值,可使用<properties>或<constructor-arg>
1、<bean>标签:用来绑定Bean类,生成一个Bean对象。一个Bean类可和多个<Bean>绑定,即生成不同的对象。
id:当前标签的id,唯一标识一个标签
class:Bean类的全类名,设置当前标签绑定的Bean类
scope:设置对象的启动模式,
singleton:单例模式,每次只返回同一个Bean对象,在容器初始化时就创建bean对象实例。
prototype:原型模式,每次返回不同Bean对象,在第一次调用getBean()方法时创建bean对象实例。
init-method:设置对象的初始化方法
destroy-method:设置对象的销毁方法,销毁方法会在IOC容器关闭后调用
关闭IOC容器的代码:((AbstractApplicationContext) context).close();
<properties>:调用Bean类的get和set方法给属性赋值,也可以包含<bean>来生成匿名内部类对象. 不能被外
部的 bean 来引用, 也没有必要设置 id 属性。
name:属性名(Get和Set方法的get或set后的英文,一般Bean类该英文一般需要与属性名一致)
value / <value>:属性值 ,和ref不能同时存在
ref / <ref>:引用外部Bean对象或通用List对象,指向某个Bean对象(填的是<Bean>的id属性),
适用于当前Bean对象包含其它Bean对象属性,也可以指向外部通用List(<util:list>
的id属性),适用于当前Bean属性包含其它List属性,和value属性不能同时存在。
<list>:Bean对象List属性或数组属性标签,表明当前<properties>或<constructor-arg>里当前属性为List
<ref>:设置List里的对应Bean一个对象
bean:生成List里的Bean的一个对象(<bean>的id值)
<set> :Bean对象Set属性
<ref>:设置Set里的对应Bean一个对象
bean:生成Set里的Bean的一个对象(<bean>的id值)
<map>:Bean对象Map属性标签,表明当前<properties>或<constructor-arg>里当前属性为Map
<entry>:设置Map里的对应Entry一个对象,包含键和值
key:设置Entry对象的键
value:设置Entry对象的值
key-ref:设置Entry对象的键(引用外部Bean对象)
value-ref:设置Entry对象的值(引用外部Bean对象)
<props>:Bean对象里有Properties对象映射标签
<prop>:Properties的一个键值对
key:设置键名,值使用标签包含
<constructor-arg>:调用Bean类的构造方法给属性赋值,构造方法按<constructor-arg>顺序赋值,当有多个
index设置索引对应参数个数不同的构造方法,若个数相同,使用type
设置参数的类型对应相同参数个数但参数类型不同的构造方法。
value / <value>:属性值 ,和ref不能同时存在
ref / <ref>:引用外部Bean对象或通用List对象,指向某个Bean对象(填的是<Bean>的id属性),
适用于当前Bean对象包含其它Bean对象属性,也可以指向外部通用List(<util:list>
的id属性),适用于当前Bean属性包含其它List属性,和value属性不能同时存在。
index:构造方法的参数索引(从0开始)
type:构造方法的参数类型
<list>:Bean对象List属性或数组属性标签,表明当前<properties>或<constructor-arg>里当前属性为List
<ref>:设置List里的对应Bean一个对象
bean:生成List里的Bean的一个对象(<bean>的id值)
<set> :Bean对象Set属性
<ref>:设置Set里的对应Bean一个对象
bean:生成Set里的Bean的一个对象(<bean>的id值)
<map>:Bean对象Map属性标签,表明当前<properties>或<constructor-arg>里当前属性为Map
<entry>:设置Map里的对应Entry一个对象,包含键和值
key:设置Entry对象的键
value:设置Entry对象的值
key-ref:设置Entry对象的键(引用外部Bean对象)
value-ref:设置Entry对象的值(引用外部Bean对象)
<props>:Bean对象里有Properties对象映射标签
<prop>:Properties的一个键值对
key:设置键名,值使用标签包含
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="first" class="Bean.Person">
<property name="name" value="张三"></property>
</bean>
<bean id="pig1" class="Bean.Pig">
<constructor-arg value="1" index="0"></constructor-arg>
<constructor-arg value="小张猪" index="1"></constructor-arg>
<constructor-arg value="1" index="2"></constructor-arg>
</bean>
<bean id="pig2" class="Bean.Pig">
<constructor-arg value="2" index="0"></constructor-arg>
<constructor-arg value="小李猪" index="1"></constructor-arg>
<constructor-arg value="2" index="2" type="int"></constructor-arg>
</bean>
</beans>
2、添加外部集合标签(与<bean>同级),在xml的<beans>里添加如下属性
xmlns:util="http://www.springframework.org/schema/util"
或者在xml文件的namespace选中util选项
<util:list>:定义一个通过外部集合标签
<ref>:外部引用设置List里的对应Bean一个对象
bean:生成List里的Bean的一个对象(<bean>的id值)
3、可以使用命名空间进行快速配置Bean类属性:在xml的<beans>里添加如下属性
xmlns:p="http://www.springframework.org/schema/p"
或者在xml文件的namespace选中p选项
格式:p:属性名:设置属性 p:属性名-ref:引用外部Bean作为属性
<bean id="SevenPerson" class="Bean.Person" p:name="李四" p:pig-ref="pig1"
p:pigList-ref="pl"
></bean>
4、<Bean>的配置继承:通过parent属性子Bean来继承父Bean的配置, 即父Bean已配置的属性可以不用再次配置。
抽象Bean(即属性abstract="true")不能被实例化,只能被继承配置
<bean id="EightPerson" class="Bean.Person" p:name="祖宗" p:pig-ref="pig1" p:pigList-ref="pl">
</bean>
<bean id="NinePerson" p:name="次祖" parent="EightPerson">
</bean>
5、<Bean>的配置依赖:Spring同意用户通过depends-on属性设定Bean关联(前置依赖的Bean,关联Bean必须在本Bean实例化之前创建好(即配置文件要存在这个<Bean>);假设前置依赖于多个Bean,则能够通过逗号或空格或的方式配置Bean的名称 ,类似UML图里的关联关系。
<bean id="TenPerson" class="Bean.Person" p:name="祖宗" p:pig-ref="pig1" p:pigList-ref="pl">
</bean>
<bean id="ElevenPerson" class="Bean.Person" p:name="次祖" depends-on="TenPerson">
</bean>
6、引用外部属性文件:可以在xml的<beans>里添加如下属性
xmlns:context="http://www.springframework.org/schema/context"
或者在xml文件的namespace选中context选项
<context:property-placeholder> :导入外部文件标签
location:文件的路径,类路径下使用classpath:文件名.后缀
以c3p0数据库连接池配置为例
新建一个配置文件:
<!-- 导入外部的资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置c3p0数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
ComboPooledDataSource dataSource=(ComboPooledDataSource)context.getBean("dataSource");
try {
System.out.println(dataSource.getConnection());
} catch (SQLException e) {
throw new RuntimeException(e);
}
7.SpringEL表达式(了解):可用来赋值、引用bean对象(通过id)、引用属性、引用方法、引用静态方法、计算表达式的值、 使用正则表达式。
赋值:#{值},为String类型可加单引号
引用bean对象(通过id):#{对象的id属性值}
引用属性:#{对象的id属性值.属性名}
引用方法:#{对象的id属性值.方法名().方法名()...}
引用静态方法/属性:#{T(全类名).方法()/属性},如:#{T(java.lang.Math).PI}
计算表达式的值:#{表达式}
符号 | 在EL中使用 | 常规 | |
1 | 等于 | eq | == |
2 | 不等于 | ne | != |
3 | 大于 | gt | > |
4 | 小于 | lt | < |
5 | 大于等于 | ge | >= |
6 | 小于等于 | le | <= |
使用正则表达式:#{表达式?值1:值2} 如:p:price="#{T(java.lang.Math).PI >=3 ? 3:0}
8、Bean的后置处理器:允许在调用初始化方法前后对bean对象进行处理并返回。
第一步:在xml文件里添加后置处理器:使用<bean>标签和class属性指定后置处理器类
<!-- 配置 bean 后置处理器: 不需要配置 id 属性, IOC 容器会识别到他是一个 bean 后置处理器, 并调用其方法 -->
<bean class="Bean.MyBeanPostProcessor"></bean>
第二步:创建一个 后置处理器类继承接口,实现方法。此时对id为pig4的Bean对象Name属性改为陈小猪
package Bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
//初始化后调用方法:Object为bean对象实例,String为xml文件里的id值
@Override
public Object postProcessAfterInitialization(Object bean, String id) throws BeansException {
System.out.println(bean+" , "+id);
if (id.equals("pig4")) {
Pig pig=(Pig)bean;
pig.setName("陈小猪");
return pig;
}
return bean;
}
//初始化前调用方法:Object为bean对象实例,String为xml文件里的id值
@Override
public Object postProcessBeforeInitialization(Object bean, String id) throws BeansException {
System.out.println(bean+" , "+id);
return bean;
}
}
9、工厂方法配置Bean的方法(了解)
XML文件里绑定工厂Bean类,也可以通过<properties>来给工厂Bean类属性赋值
<!-- 配置通过 FactroyBean 的方式来创建 bean 的实例(了解) -->
<bean id="factoryBean" class="Bean.MyFactoryBean"></bean>
继承FactoryBean,实现对应的方法。
package Bean;
import org.springframework.beans.factory.FactoryBean;
public class MyFactoryBean<T> implements FactoryBean<T> {
//获得对象
@Override
public T getObject() throws Exception {
Person person=new Person();
person.setName("我是工厂Bean");
return (T) person;
}
//返回对象类型
@Override
public Class<?> getObjectType() {
return Person.class;
}
//判断是否单例
@Override
public boolean isSingleton() {
return true;
}
}
获取Bean对象
Person person=(Person) context.getBean("factoryBean");
System.out.println(person);