Detailed explanation of Spring bean properties

1.parent

Indicates the inherited parent class

If there are many BEANs that inherit the same parent class

Then you can omit the properties injected by the parent class when you instantiate those BEANs in the configuration file

A bean definition inherits a parent bean definition, which can override some values ​​of the parent bean, or values ​​it needs.

Then you can omit the properties injected by the parent class when you instantiate those BEANs in the configuration file

<bean id="carnetMgr" parent="txProxyTemplate">

<property name="target">

<bean class="ecustoms.carnet.app.biz.CarnetMgr">

<property name="extensionHistoryDAO" ref="extensionHistoryDAO"/>

<property name="stockTransactionDAO" ref="stockTransactionDAO"/>

<property name="carnetDAO" ref="carnetDAO"/>

             </bean>

</property>

</bean>

 

2. The list, set, map and props elements are used to set property values ​​of type List, Set, Map and Properties respectively. Used to pass in collection values ​​for beans respectively.

 An example of the corresponding spring configuration file is as follows:

< bean  id ="chinese"  class ="Chinese" >     

  < property  name ="friends" >     

             < list >     

                 < value > 张三 </ value >     

                 < value > 李四 </ value >     

                 < value > 王五 </ value >     

             </ list >     

   </ property >     

   < property  name ="score" >     

             < map >     

                 < entry  key ="数学" >     

                     < value > 60 </ value >     

                 </ entry >     

                 < entry key = "English" >     

                     < value > 70 </ value >     

                 </ entry >     

                 < entry key="language" >     

                     < value > 80 </ value >     

                 </ entry >     

                 < entry  key ="物理" >     

                     < value > 90 </ value >     

                 </ entry >     

                 < entry  key ="化学" >     

                     < value > 95 </ value >     

                 </ entry >     

             </ map >     

   </ property >     

   < property  name ="basicInfo" >     

             < props >     

                 < prop  key ="身高" > 165 </ prop >     

                 < prop  key ="体重" > 45kg </ prop >     

                 < prop  key ="学历" > 大学本科 </ prop >     

             </ props >     

    </ property >     

    < property  name ="interest" >     

             < set >     

                 < value > 唱歌 </ value >     

                 < value > 跳舞 </ value >     

                 < value > 书法 </ value >     

             </ set >     

    </ property >     

 </ bean >     

对应的java代码如下:

public   class  Chinese  implements  People   ...{    

     private  List friends  =   new  ArrayList();    

     private  Map score  =   new  HashMap();    

     private  Properties basicInfo  =   new  Properties();    

     private  Set interest  =   new  HashSet();    

     // 省略对应set方法     

}   

3.maxSize表示每页显示数据的最大值

<property name="maxSize"><value>20</value></property>

Bean元素出了上面的两个属性之外,还有很多其它属性。说明如下:

<bean

    id="beanId"(1)

    name="beanName"(2)

    class="beanClass"(3)

    parent="parentBean"(4)

    abstract="true | false"(5)

    singleton="true | false"(6)

    lazy-init="true | false | default"(7)

    byName | byType | constructor | autodetect | default"(8)

    dependency-check = "none | objects | simple | all | default"(9)

    depends-on="dependsOnBean"(10)

    init-method="method"(11)

    destroy-method="method"(12)

    factory-method="method"(13)

  factory-bean="bean">(14)

</bean>

 

4.id: Bean的唯一标识名。它必须是合法的XML ID,在整个XML文档中唯一。

5.name: 用来为id创建一个或多个别名。它可以是任意的字母符合。多个别名之间用逗号或空格分开。

6.class: 用来定义类的全限定名(包名+类名)。只有子类Bean不用定义该属性。

7.abstract(默认为”false”):用来定义Bean是否为抽象Bean。它表示这个Bean将不会被实例化,一般用于父类Bean,因为父类Bean主要是供子类Bean继承使用。

8.singleton(默认为“true”):定义Bean是否是Singleton(单例)。如果设为“true”,则在BeanFactory作用范围内,只维护此Bean的一个实例。如果设为“flase”,Bean将是Prototype(原型)状态,BeanFactory将为每次Bean请求创建一个新的Bean实例。

9.lazy-init(默认为“default”):用来定义这个Bean是否实现懒初始化。如果为“true”,它将在BeanFactory启动时初始化所有的Singleton Bean。反之,如果为“false”,它只在Bean请求时才开始创建Singleton Bean。

10.autowire(自动装配,默认为“default”):它定义了Bean的自动装载方式。

    1、“no”:不使用自动装配功能。

    2、“byName”:通过Bean的属性名实现自动装配。

    3、“byType”:通过Bean的类型实现自动装配。

    4、“constructor”:类似于byType,但它是用于构造函数的参数的自动组装。

    5、“autodetect”:通过Bean类的反省机制(introspection)决定是使用“constructor”还是使用“byType”。

 

11.dependency-check(依赖检查,默认为“default”):它用来确保Bean组件通过JavaBean描述的所以依赖关系都得到满足。在与自动装配功能一起使用时,它特别有用。

1、 none:不进行依赖检查。

2、 objects:只做对象间依赖的检查。

3、 simple:只做原始类型和String类型依赖的检查

4、 all:对所有类型的依赖进行检查。它包括了前面的objects和simple。

 

12.depends-on(依赖对象):这个Bean在初始化时依赖的对象,这个对象会在这个Bean初始化之前创建。

13.init-method:用来定义Bean的初始化方法,它会在Bean组装之后调用。它必须是一个无参数的方法。

14.destroy-method:用来定义Bean的销毁方法,它在BeanFactory关闭时调用。同样,它也必须是一个无参数的方法。它只能应用于singleton Bean。

15.factory-method:定义创建该Bean对象的工厂方法。它用于下面的“factory-bean”,表示这个Bean是通过工厂方法创建。此时,“class”属性失效。

16.factory-bean:定义创建该Bean对象的工厂类。如果使用了“factory-bean”则“class”属性失效。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326453661&siteId=291194637