Spring Bean的三种配置方式
- 基于XML配置
- 基于注解配置
- 基于java类配置
XML配置Spring Bean
Spring Bean的两种容器
- BeanFactory
它在org.springframework.beans.factory.BeanFactory中
其用于加载XML文件中配置的bean,将它们集合起来,当有请求的时候分配
常用XMLBeanFactory接口来加载XML文件
XmlBeanFactory beanFact = new XmlBeanFactory(new ClassPathResouce("Beans.xml"));
- ApplicationContext
它在org.springframework.context.ApplicationContext中
其功能和BeanFactory一样,但是增加了许多企业级功能,更加强大。现在我们一般都采用ApplicationContext,除非是用于轻量级的应用中,如移动端应用、基于applet的应用。
常用三个接口加载XML文件:- FileSystemXmlApplicationContext
ApplicationContext filesys = new FileSystemXmlApplicationContext("filepath");
1 - ClassPathXmlApplicationContext
ApplicationContext classpath = new ClassPathXmlApplicationContext("beans.xml");
2 - WebXmlApplicationContext
ApplicationContext web = new WebXmlApplicationContext("beans.xml");
- FileSystemXmlApplicationContext
Spring Bean的属性
-
class:用于指定bean加载的类,即定义bean的类。
-
id/name:用于赋予bean一个唯一的名字。
-
scope:定义作用域。
<bean id="" class="" scope="singleton">
- singleton:唯一一个bean,不会反复创建。
- prototype:在每次调用getBean方法时,都会创建一个新的bean。
- request:在每次收到请求的时候创建,只可以用于WebApplicationContext环境。
- session:一个会话共用同一个session bean,只可以用于WebApplicationContext环境。
- globalsession:一般用于portlet中,只可以用于WebApplicationContext环境。4. lazy-init:是否延迟bean的创建,即:不在启动时创建,而是在对其请求的时候才创建。
<bean id="" class="" lazy-init="false">
3
-
init-method:定义初始化bean的方法4,也可以用回调函数但不推荐。
xml文件中 <bean id="" class="" init-method="init"> 在bean类中 public void init(){ System.out.print("bean初始化"); }
回调接口:void afterPropertiesSet()5 throws Exception;
在bean类中实现InitalizingBean接口,然后定义afterPropertiesSet()。 -
destroy-method:定义销毁bean的方法,也可以用回调函数但不推荐。
xml文件中 <bean id="" class="" destroy-method="destroy"> 在bean类中 public void destroy(){ System.out.print("bean销毁"); }
回调接口:void destroy()6 throws Exception;
在bean类中实现DisposableBean接口,然后定义destroy()。
Spring Bean依赖注入
依赖注入分为:
- 基于构造方法的依赖注入
- 基于成员方法的依赖注入
构造方法依赖注入
- 直接传值
public Person(int age,String name){}; 方法一 <bean id="" class=""> <constructor-arg type="int" value="12"></constructor-arg> <constructor-arg type="String" value="zhangsan"></constructor-arg> </bean> 方法二 <bean id="" class=""> <constructor-arg index="0" value="12"></constructor-arg> <constructor-arg index="1" value="zhangsan"></constructor-arg> </bean>
- 传递引用
public Person(int age,Family family){}; <bean id="" class=""> <constructor-arg type="int" value="12"></constructor-arg> <constructor-arg name="family" ref="Family"></constructor-arg> </bean> <bean id="Family" class=""> </bean>
成员方法依赖注入
- 直接传值
public void setPerson(int age,String name){}; 方法一 <bean id="" class=""> <property name="age" value="12"></property> <property name="name" value="zhangsan"></property> </bean> 方法二 <bean id="" class="" p:age="12" p:name="zhangsan"> </bean>
- 传递引用
方法一 public void setPerson(int age,Family family){}; <bean id="" class=""> <property name="age" value="12"></property> <property name="family" ref="Family"></property> </bean> <bean id="Family" class=""> </bean> 方法二 public void setPerson(int age,Family family){}; <bean id="" class="" p:age="12" p:family-ref="Family"> </bean> <bean id="Family" class=""> </bean>
传递集合
SpringBean中可以用<list>、<set>、<map>、<prop>
四个标签分别传递List、Set、Map、Properties类型的引用。
<list>
传递的是线性的值,可以有重复。
<set>
传递的是线性的值,但是不可以有重复。
<map>
传递的是键值对,键和值可以为任意类型。
<prop>
传递的是键值对,但是键和值只能为字符串类型。
List
<bean id="" class="">
<property name="">
<list>
<value>123</value> <!--结果为[123,123,456]-->
<value>123</value>
<value>456</value>
</list>
</property>
</bean>
Set
<bean id="" class="">
<property name="">
<set>
<value>123</value> <!--结果为[123,456]-->
<value>123</value>
<value>456</value>
</set>
</property>
</bean>
Map
<bean id="" class="">
<property name="">
<map>
<entry key="1" value="123"/> <!--结果为[{1=123,2=456}]-->
<entry key="2" value="456"/>
</map>
</property>
</bean>
Properties
<bean id="" class="">
<property name="">
<props>
<prop key="one">123</prop> <!--结果为[{one=123,two=456}]-->
<prop key="two">456</prop>
</props>
</property>
</bean>