Spring 基础教程之四:JavaBean基本配置详解

一:xml 装配JavaBean属性含义:

1.id:指定该Bean 的唯一标识。

2.class:指定该Bean 的全限定名。

3.name:为该Bean 指定一到多个别名。多个别名可以用“,”和“;”分割。

4autowire:指定该Bean 的属性的装配方式。

所谓自动装配是指在<BEAN>标签中不用指定其依赖的BEAN,而是通过配置的自动装配来自动注入依赖的BEAN,这种做法让我们的配置更加简单

1no:不使用自动装配。必须通过ref 元素指定依赖,这是默认设置。由于显式指定协作者可以使配置更灵活、更清晰,因此对于较大的部署配置,推荐采用该设置。而且在某种程度上,它也是系统架构的一种文档形式。


   
   
  1. <span style="font-size: 18px;"> <bean id="bean1" class="cn.csdn.service.Bean1"
  2. scope= "singleton">
  3. <property name="studentDaoImpl"
  4. ref= "studentDaoImpl">
  5. </property>
  6. </bean> </span>

备注:有property 属性指定ref

2byName:根据属性名自动装配。此选项将检查容器并根据

名字查找与属性完全一致的bean,并将其与属性自动装配。例如,在

bean 定义中将autowire 设置为by name,而该bean 包含master 属性(同时提供setMaster(..)方法),Spring 就会查找名为master 的bean 定义,并用它来装配给master 属性。

<bean id="bean1" class="cn.csdn.service.Bean1"

scope="singleton" autowire="byName"/>

备注:没有property 属性

3byType:果容器中存在一个与指定属性类型相同的

bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么

将会抛出异常,并指出不能使用byType 方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。如果你不希望这样,那么可以通过设置dependency-check="objects"让Spring 抛出异常。

备注:spring3.0 以上不抛异常。

<bean id="bean1" class="cn.csdn.service.Bean1"

scope="singleton" autowire="byType"/>

备注:没有property 属性

4Constructor:与byType 的方式类似,不同之处在于它应用

于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。

<bean id="bean1" class="cn.csdn.service.Bean1"

scope="singleton"autowire="constructor"/>

备注:没有property 属性

5autodetect:通过bean 类的自省机制(introspection)来决定是使用constructor 还是byType 方式进行自动装配。如果发现默认的

构造器,那么将使用byType 方式。

<bean id="bean1" class="cn.csdn.service.Bean1"

scope="singleton" autowire="autodetect"/>

5.scope:指定该Bean 的生存范围

scope用来声明IOC容器中的对象应该处的限定场景或者说该对象的存活空间,即在IOC容器在对象进入相应的scope之前,生成并装配这些对象,在该对象不再处于这些scope的限定之后,容器通常会销毁这些对象。

1) singleton类型bean定义,在一个容器中只存在一个实例,所有对该类型bean的依赖都引用这一单一实例

2) scopeprototypebean,容器在接受到该类型的对象的请求的时候,会每次都重新生成一 个新的对象给请求方,虽然这种类型的对象的实例化以及属性设置等工作都是由容器负责的,但是只要准备完毕,并且对象实例返回给请求方之后,容器就不在拥有 当前对象的引用,请求方需要自己负责当前对象后继生命周期的管理工作,包括该对象的销毁

3) request sessionglobal session

这三个类型是spring2.0之后新增的,他们只适用于web程序,通常是和XmlWebApplicationContext共同使用

request

<bean id ="requestPrecessor" class="...RequestPrecessor"   scope="request" />

Spring容器,即XmlWebApplicationContext 会为每个HTTP请求创建一个全新的RequestPrecessor对象,当请求结束后,,该对象的生命周期即告结束

session

<bean id ="userPreferences" class="...UserPreferences"   scope="session" />

Spring容器会为每个独立的session创建属于自己的全新的UserPreferences实例,他比request scopebean会存活更长的时间,其他的方面真是没什么区别。

global session:

<bean id ="userPreferences" class="...UserPreferences"   scope="globalsession" /> 

global session只有应用在基于porlet的web应用程序中才有意义,他映射到porlet的global范围的session,如果普通的servlet的web 应用中使用了这个scope,容器会把它作为普通的session的scope对待。

6.init-method:指定该Bean 的初始化方法。destroy-method:指定该Bean 的销毁方法。这个就像servletinitdestroy方法一样,只不过这里在配置文件配置的

7.abstract:指定该Bean 是否为抽象的。如果是抽象的,则

spring 不为它创建实例。

8.parent 

如果两个Bean 的属性装配信息很相似,那么可以利用继

承来减少重复的配置工作。

<!-- 装配Bean 的继承

父类作为模板,不需要实例化,设置abstract=”true”-->

` <bean id=”parent” class=”cn.csdn.service.Parent”

abstract=”true”>

<property name=”name” value=”z_xiaofei168”/>

<property name=”pass” value=”z_xiaofei168”/>

</bean>

<!-- 装配Bean 的继承

子类中用parent 属性指定父类标识或别名

子类可以覆盖父类的属性装配,也可以新增自己的属性装配

-->

` <bean id=”child” class=”cn.csdn.service.Chlid”

parent=”parent”>

<property name=”pass” value=”123123”/>

<property name=”age” value=”22”/>

</bean>


二:装配Bean 的各种类型属性值

1..简单类型属性值的装配


   
   
  1. <bean id="bean1" class="cn.csdn.domain.Bean1">
  2. <property name="name" value="z_xiaofei168"/>
  3. <property name="age">
  4. <value>22 </value>
  5. </property>
  6. </bean>

2.引用其他Bean 的装配




   
   
  1. <bean id="bean1" class="cn.csdn.domain.Bean1">
  2. ...
  3. </bean>
  4. <bean id="bean2" class="cn.csdn.domain.Bean2">
  5. <!-- 引用自其他Bean 的装配-->
  6. <property name="bean1" ref="bean1"/>
  7. </bean>

另外一种不常使用的配置方式是在property 元素中嵌入

一个bean 元素来指定所引用的Bean.


   
   
  1. <bean id="bean1" class="cn.csdn.domain.Bean1">
  2. ...
  3. </bean>
  4. <bean id="bean2" class="cn.csdn.domain.Bean2">
  5. <!-- 引用自其他Bean 的装配-->
  6. <property name="bean1">
  7. <bean id="bean1"
  8. class= "cn.csdn.domain.Bean1"/>
  9. </property>
  10. </bean>

3.集合的装配

其实集合的装配并不是复杂,反而感觉到很简单,用一个例子来说明问题吧:


   
   
  1. package com.bebig.dao.impl;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.Properties;
  5. import java.util.Set;
  6. import com.bebig.dao.UserDAO;
  7. import com.bebig.model.User;
  8. public class UserDAOImpl implements UserDAO {
  9. private Set <String> sets;
  10. private List <String> lists;
  11. private Map <String, String> maps;
  12. private Properties props;
  13. public Set <String> getSets() {
  14. return sets;
  15. }
  16. public void setSets(Set <String> sets) {
  17. this.sets = sets; }
  18. public List <String> getLists() {
  19. return lists; }
  20. public void setLists(List <String> lists) {
  21. this.lists = lists; }
  22. public Map <String, String> getMaps() {
  23. return maps;
  24. }
  25. public void setMaps(Map <String, String> maps) {
  26. this.maps = maps;
  27. }
  28. public Properties getProps() {
  29. return props;
  30. }
  31. public void setProps(Properties props) {
  32. this.props = props;
  33. }
  34. public void save(User u) {
  35. System.out.println("a user saved!");
  36. }
  37. @Override
  38. public String toString() {
  39. return "sets.size:" + sets.size() + " lists.size:" + lists.size()
  40. + " maps.size:" + maps.size() + " props.size:" + props.size();
  41. }
  42. }

配置如下:


   
   
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "http://www.springframework.org/schema/context"
  4. >
  5. <!-- a service object; we will be profiling its methods -->
  6. <bean name="u" class="com.bebig.dao.impl.UserDAOImpl">
  7. <!-- set -->
  8. <property name="sets">
  9. <set>
  10. <value>1 </value>
  11. <value>2 </value>
  12. </set>
  13. </property>
  14. <!-- list -->
  15. <property name="lists">
  16. <list>
  17. <value>a </value>
  18. <value>b </value>
  19. </list>
  20. </property>
  21. <!-- map -->
  22. <property name="maps">
  23. <map>
  24. <entry key="1" value="aa"> </entry>
  25. <entry key="2" value="bb"> </entry>
  26. </map>
  27. </property>
  28. <!-- properties -->
  29. <property name="props">
  30. <props>
  31. <prop key="a">haha </prop>
  32. <prop key="b">hi </prop>
  33. </props>
  34. </property>
  35. </bean>
  36. <bean id="userService" class="com.bebig.service.UserService"
  37. scope= "prototype">
  38. <constructor-arg>
  39. <ref bean="u" />
  40. </constructor-arg>
  41. </bean>
  42. <!-- this switches on the load-time weaving -->
  43. <!-- <context:load-time-weaver /> -->
  44. </beans>


猜你喜欢

转载自blog.csdn.net/wangliang369/article/details/83381807