spring 中bean解释以及bean标签里面的属性

一、bean标签:

英文解释:Defines a single (usually named) bean. A bean definition may contain nested tags for constructor arguments, property values, lookup methods, and replaced methods. Mixing constructor injection and setter injection on the same bean is explicitly supported.

上句翻译:定义一个(通常是命名的)bean。 bean定义可以包含构造函数参数,属性值,查找方法和替换方法的嵌套标记。明确支持在同一个bean上混合构造函数注入和setter注入。

二、bean标签属性(name为属性名,type为=“”里面的类型,default为默认值):

    1、name="id"    type="xsd:ID"

    英文解释:The unique identifier for a bean.

    上句翻译:bean的唯一标识符

    2、name="name"     type="xsd:string"

    英文解释:Can be used to create one or more aliases illegal in an (XML) id. Multiple aliases can be separated by any number of spaces, commas, or semi-colons (or indeed any mixture of the three).

    上句翻译:可用于在(XML)id中创建一个或多个非法别名。多个别名可以用任意数量的空格,逗号或分号(或者实际上三者的任何混合)分隔。

    3、name="class"     type="xsd:string"

    英文解释:The fully qualified name of the bean's class, except if it serves only as a parent definition for child bean definitions.

    上句翻译:bean类的完全合法名称,除非它仅用作子bean定义的父定义。(为Java类的具体路径)

    4、name="parent"     type="xsd:string"

    英文解释:The name of the parent bean definition. Will use the bean class of the parent if none is specified, but can also override it. In the latter case, the child bean class must be compatible with the parent, i.e. accept the parent's property values and constructor argument values, if any. A child bean definition will inherit constructor argument values, property values and method overrides from the parent, with the option to add new values. If init method, destroy method, factory bean and/or factory method are specified, they will override the corresponding parent settings. The remaining settings will always be taken from the child definition: depends on, autowire mode, scope, lazy init.

    上句翻译:父bean定义的名称。如果没有指定父类的bean类,它将使用它的bean类,但也可以覆盖它。在后一种情况下,子bean类必须与父类兼容,即接受父类的属性值和构造函数参数值(如果有)。子bean定义将从父级继承构造函数参数值,属性值和方法覆盖,并具有添加新值的选项。如果指定了init方法,destroy方法,工厂bean和/或工厂方法,它们将覆盖相应的父设置。其余设置将始终从子定义中获取:取决于,autowire模式,范围,延迟初始化。

    5、name="scope"     type="xsd:string"

    英文解释:The scope of this bean: typically "singleton" (one shared instance, which will be returned by all calls to getBean with the given id), or "prototype" (independent instance resulting from each call to getBean). By default, a bean will be a singleton, unless the bean has a parent bean definition in which case it will inherit the parent's scope. Singletons are most commonly used, and are ideal for multi-threaded service objects. Further scopes, such as "request" or "session", might be supported by extended bean factories (e.g. in a web environment). Inner bean definitions inherit the singleton status of their containing bean definition, unless explicitly specified: The inner bean will be a singleton if the containing bean is a singleton, and a prototype if the containing bean has any other scope.

    上句翻译:这个bean的范围:通常是“singleton”(一个共享实例,将由具有给定id的所有getBean调用返回)或“prototype”(每次调用getBean时产生的独立实例)。默认情况下,bean将是一个单独的bean,除非bean具有父bean定义,在这种情况下它将继承父的作用域。单例是最常用的,是多线程服务对象的理想选择。扩展bean工厂可能支持其他范围,例如“请求”或“会话”(例如,在Web环境中)。内部bean定义继承其包含bean定义的单例状态,除非明确指定:如果包含bean是单例,则内部bean将是单例,如果包含bean具有任何其他范围,则内部bean将是原型。

6、name="abstract"     type="xsd:boolean"

英文解释:Is this bean "abstract", that is, not meant to be instantiated itself but rather just serving as parent for concrete child bean definitions? The default is "false". Specify "true" to tell the bean factory to not try to instantiate that particular bean in any case. Note: This attribute will not be inherited by child bean definitions. Hence, it needs to be specified per abstract bean definition.

上句翻译:这个bean是“抽象的”,也就是说,不是要自己实例化,而是仅仅作为具体子bean定义的父级?默认值为“false”。指定“true”以告诉bean工厂在任何情况下都不尝试实例化该特定bean。注意:子bean定义不会继承此属性。因此,需要根据抽象bean定义指定它。

7、name="lazy-init"      default="default"      type="defaultable-boolean"

英文解释:Indicates whether or not this bean is to be lazily initialized. If false, it will be instantiated on startup by bean factories that perform eager initialization of singletons. The default is "false". Note: This attribute will not be inherited by child bean definitions. Hence, it needs to be specified per concrete bean definition.

上句翻译:

指示是否要对此bean进行延迟初始化。如果为false,它将在启动时由执行初始化单例的bean工厂实例化。默认值为“false”。注意:子bean定义不会继承此属性。因此,需要根据具体的bean定义来指定它。

8、name="autowire"     default="default"

取值:

          <xsd:enumeration value="default"/>

          <xsd:enumeration value="no"/>

          <xsd:enumeration value="byName"/>

          <xsd:enumeration value="byType"/>

          <xsd:enumeration value="constructor"/>

英文解释:Controls whether bean properties are "autowired". This is an automagical process in which bean references don't need to be coded explicitly in the XML bean definition file, but rather the Spring container works out dependencies. There are 4 modes: 1. "no" The traditional Spring default. No automagical wiring. Bean references must be defined in the XML file via the <ref/> element (or "ref" attribute). We recommend this in most cases as it makes documentation more explicit. Note that this default mode also allows for annotation-driven autowiring, if activated. "no" refers to externally driven autowiring only, not affecting any autowiring demands that the bean class itself expresses. 2. "byName" Autowiring by property name. If a bean of class Cat exposes a "dog" property, Spring will try to set this to the value of the bean "dog" in the current container. If there is no matching bean by name, nothing special happens. 3. "byType" Autowiring if there is exactly one bean of the property type in the container. If there is more than one, a fatal error is raised, and you cannot use byType autowiring for that bean. If there is none, nothing special happens. 4. "constructor" Analogous to "byType" for constructor arguments. If there is not exactly one bean of the constructor argument type in the bean factory, a fatal error is raised. Note that explicit dependencies, i.e. "property" and "constructor-arg" elements, always override autowiring. Note: This attribute will not be inherited by child bean definitions. Hence, it needs to be specified per concrete bean definition.

上句翻译:控制bean属性是否为“自动装配”。这是一个自动化过程,其中bean引用不需要在XML bean定义文件中显式编码,而是Spring容器计算出依赖项。有4种模式:1。“no”传统的Spring默认值。没有自动接线。必须通过<ref />元素(或“ref”属性)在XML文件中定义Bean引用。在大多数情况下,我们建议这样做,因为它使文档更明确。请注意,此默认模式还允许注释驱动的自动装配(如果已激活)。 “no”仅指外部驱动的自动装配,不影响bean类本身表达的任何自动装配要求。 2.“byName”按属性名称自动装配。如果类Cat的bean暴露了“dog”属性,Spring将尝试将其设置为当前容器中bean“dog”的值。如果名称没有匹配的bean,则不会发生任何特殊情况。 3.“byType”如果容器中只有一个属性类型的bean,则自动装配。如果存在多个,则会引发致命错误,并且您无法对该Bean使用byType自动装配。如果没有,没有什么特别的事情发生。 4.“constructor”类似于构造函数参数的“byType”。如果bean工厂中没有构造函数参数类型的bean,则会引发致命错误。请注意,显式依赖项(即“property”和“constructor-arg”元素)始终会覆盖自动装配。注意:子bean定义不会继承此属性。因此,需要根据具体的bean定义来指定它。

9、name="depends-on"     type="xsd:string"

英文解释:The names of the beans that this bean depends on being initialized. The bean factory will guarantee that these beans get initialized before this bean. Note that dependencies are normally expressed through bean properties or constructor arguments. This property should just be necessary for other kinds of dependencies like statics (*ugh*) or database preparation on startup. Note: This attribute will not be inherited by child bean definitions. Hence, it needs to be specified per concrete bean definition.

上句翻译:此bean依赖于初始化的bean的名称。 bean工厂将保证在这个bean之前初始化这些bean。请注意,依赖关系通常通过bean属性或构造函数参数表示。对于其他类型的依赖项,例如静态(* ugh *)或启动时的数据库准备,此属性应该是必需的。注意:子bean定义不会继承此属性。因此,需要根据具体的bean定义来指定它。

10、name="autowire-candidate"     default="default"     type="defaultable-boolean"

英文翻译:Indicates whether or not this bean should be considered when looking for matching candidates to satisfy another bean's autowiring requirements. Note that this does not affect explicit references by name, which will get resolved even if the specified bean is not marked as an autowire candidate.

上句解释:指示在查找匹配的候选项以满足另一个bean的自动装配要求时是否应该考虑此bean。请注意,这不会影响名称的显式引用,即使指定的bean未标记为autowire候选,也会解析它。

11、name="primary"     type="xsd:boolean"

英文解释:Specifies that this bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. If exactly one 'primary' bean exists among the candidates, it will be the autowired value.

上句翻译:指定当多个候选者有资格自动装配单值依赖项时,应该优先考虑此bean。如果候选者中只存在一个“主”bean,则它将是自动装配的值。

12、name="init-method"     type="xsd:string"

英文解释:The name of the custom initialization method to invoke after setting bean properties. The method must have no arguments, but may throw any exception.

上句翻译:设置bean属性后要调用的自定义初始化方法的名称。该方法必须没有参数,但可能会抛出任何异常。

13、 name="destroy-method"     type="xsd:string"

英文解释:The name of the custom destroy method to invoke on bean factory shutdown. The method must have no arguments, but may throw any exception. Note: Only invoked on beans whose lifecycle is under the full control of the factory - which is always the case for singletons, but not guaranteed for any other scope.

上句翻译:要在bean工厂关闭时调用的自定义destroy方法的名称。该方法必须没有参数,但可能会抛出任何异常。注意:仅在生命周期完全由工厂控制的bean上调用 - 对于单例来说总是如此,但对于任何其他范围都不能保证。

14、name="factory-method"     type="xsd:string"

英文解释:The name of a factory method to use to create this object. Use constructor-arg elements to specify arguments to the factory method, if it takes arguments. Autowiring does not apply to factory methods. If the "class" attribute is present, the factory method will be a static method on the class specified by the "class" attribute on this bean definition. Often this will be the same class as that of the constructed object - for example, when the factory method is used as an alternative to a constructor. However, it may be on a different class. In that case, the created object will *not* be of the class specified in the "class" attribute. This is analogous to FactoryBean behavior. If the "factory-bean" attribute is present, the "class" attribute is not used, and the factory method will be an instance method on the object returned from a getBean call with the specified bean name. The factory bean may be defined as a singleton or a prototype. The factory method can have any number of arguments. Autowiring is not supported. Use indexed constructor-arg elements in conjunction with the factory-method attribute. Setter Injection can be used in conjunction with a factory method. Method Injection cannot, as the factory method returns an instance, which will be used when the container creates the bean.

上句翻译:用于创建此对象的工厂方法的名称。如果采用参数,则使用constructor-arg元素指定工厂方法的参数。自动装配不适用于工厂方法。如果存在“class”属性,则工厂方法将是此bean定义上“class”属性指定的类的静态方法。通常,这将与构造对象的类相同 - 例如,当工厂方法用作构造函数的替代时。但是,它可能属于不同的类别。在这种情况下,创建的对象将*不属于“class”属性中指定的类。这类似于FactoryBean行为。如果存在“factory-bean”属性,则不使用“class”属性,并且factory方法将是对具有指定bean名称的getBean调用返回的对象的实例方法。工厂bean可以定义为单例或原型。工厂方法可以包含任意数量的参数。不支持自动装配。将索引构造函数-arg元素与factory-method属性结合使用。 Setter Injection可与工厂方法结合使用。方法注入不能,因为工厂方法返回一个实例,它将在容器创建bean时使用。

15、name="factory-bean" type="xsd:string"

英文解释:Alternative to class attribute for factory-method usage. If this is specified, no class attribute should be used. This must be set to the name of a bean in the current or ancestor factories that contains the relevant factory method. This allows the factory itself to be configured using Dependency Injection, and an instance (rather than static) method to be used.

上句翻译:替代工厂方法用法的类属性。如果指定了此项,则不应使用类属性。必须将其设置为包含相关工厂方法的当前或祖先工厂中的bean的名称。这允许使用依赖注入配置工厂本身,并使用实例(而不是静态)方法。

三、bean标签下面的标签(ref为标签的名称,minOccurs为最小出现次数,maxOccurs为最多出现次数):

1、<xsd:element ref="description" minOccurs="0"/>

        description标签,和beans里面文章一样

2、choice为多选一

<xsd:choice minOccurs="0" maxOccurs="unbounded">

        <xsd:element ref="meta"/>

        <xsd:element ref="constructor-arg"/>

        <xsd:element ref="property"/>

        <xsd:element ref="qualifier"/>

        <xsd:element ref="lookup-method"/>

        <xsd:element ref="replaced-method"/>

</xsd:choice>

(1)、meta标签:

英文解释:Arbitrary metadata attached to a bean definition.

上句翻译:附加到bean定义的任意元数据。

meta标签属性:

      name="key"    type="xsd:string"      use="required"

        英文解释:The key name of the metadata attribute being defined.

        上句翻译:正在定义的元数据属性的密钥名称。

      name="value"        type="xsd:string"       use="required"

        英文解释:The value of the metadata attribute being defined (as a simple String).

        上句翻译:正在定义元数据属性的值(作为简单的String)。

(2)、constructor-arg标签:

英文解释:Bean definitions can specify zero or more constructor arguments. This is an alternative to "autowire constructor". Arguments correspond to either a specific index of the constructor argument list or are supposed to be matched generically by type. Note: A single generic argument value will just be used once, rather than potentially matched multiple times (as of Spring 1.1). constructor-arg elements are also used in conjunction with the factory-method element to construct beans using static or instance factory methods.

上句翻译:Bean定义可以指定零个或多个构造函数参数。这是“autowire构造函数”的替代方法。参数对应于构造函数参数列表的特定索引,或者应该按类型一般地匹配。注意:单个泛型参数值将只使用一次,而不是可能多次匹配(从Spring 1.1开始)。 constructor-arg元素还与factory-method元素一起使用,以使用静态或实例工厂方法构造bean。

constructor-arg标签属性:

        name="index"    type="xsd:string"

        英文解释:The exact index of the argument in the constructor argument list. Only needed to avoid ambiguities, e.g. in case of 2 arguments of the exact same type.

        上句翻译:构造函数参数列表中参数的确切索引。只需要避免含糊不清,例如如果是2个完全相同类型的参数。

       name="type"       type="xsd:string"

        英文解释:The exact type of the constructor argument. Only needed to avoid ambiguities, e.g. in case of 2 single argument constructors that can both be converted from a String.

        上句翻译:构造函数参数的确切类型。只需要避免含糊不清,例如在2个单个参数构造函数的情况下,它们都可以从String转换。

        name="name"     type="xsd:string"

        英文解释:The exact name of the argument in the constructor argument list. Only needed to avoid ambiguities, e.g. in case of 2 arguments of the exact same type. Note: This requires debug symbols to be stored in the class file in order to introspect argument names!

        上句翻译:构造函数参数列表中参数的确切名称。只需要避免含糊不清,例如如果是2个完全相同类型的参数。注意:这需要将调试符号存储在类文件中,以便内省参数名称!

         name="ref"      type="xsd:string" 

        英文解释:A short-cut alternative to a nested "<ref bean='...'/>" element.

        上句翻译:嵌套“<ref bean ='...'/>”元素的快捷替代方法。

          name="value"        type="xsd:string"

        英文解释:A short-cut alternative to a nested "<value>...<value/>" element.

        上句翻译:嵌套“<value> ... <value />”元素的快捷替代方法。

constructor-arg标签下层标签:(请看property的)

<xsd:element ref="description" minOccurs="0"/>

<xsd:choice minOccurs="0" maxOccurs="1">

         <xsd:element ref="bean"/>

          <xsd:element ref="ref"/>

          <xsd:element ref="idref"/>

          <xsd:element ref="value"/>

           <xsd:element ref="null"/>

           <xsd:element ref="array"/>

           <xsd:element ref="list"/>

           <xsd:element ref="set"/>

           <xsd:element ref="map"/>

          <xsd:element ref="props"/>

</xsd:choice>

(3)、property标签:(具体看我的关于property标签的文章)

(4)、qualifier标签:

英文解释:Bean definitions can provide qualifiers to match against annotations on a field or parameter for fine-grained autowire candidate resolution.

上句翻译:Bean定义可以提供限定符以匹配字段或参数上的注释,以进行细粒度的自动线候选解析。

qualifier标签属性:

      name="type"     type="xsd:string"     default="org.springframework.beans.factory.annotation.Qualifier"

      name="value"     type="xsd:string"

qualifier标签下一层标签:

      ref="attribute"       minOccurs="0"         maxOccurs="unbounded"

       英文解释;A qualifier element may contain attribute child elements as key-value pairs. These will be available for matching against attributes of a qualifier annotation on an autowired field or parameter if present.

      上句翻译:限定符元素可以包含属性子元素作为键值对。这些将可用于匹配自动装配字段或参数(如果存在)上的限定符注释的属性。

           attribute标签是和mete一样属性为key,value

(5)、lookup-method标签:

英文解释:A lookup method causes the IoC container to override the given method and return the bean with the name given in the bean attribute. This is a form of Method Injection. It is particularly useful as an alternative to implementing the BeanFactoryAware interface, in order to be able to make getBean() calls for non-singleton instances at runtime. In this case, Method Injection is a less invasive alternative.

上句翻译:查找方法使IoC容器覆盖给定方法,并返回bean属性中给出的名称的bean。这是方法注入的一种形式。它作为实现BeanFactoryAware接口的替代方法特别有用,以便能够在运行时对非单例实例进行getBean()调用。在这种情况下,Method Injection是一种侵入性较小的替代方案。

lookup-method标签属性:

      name="name"       type="xsd:string"

        英文解释:The name of the lookup method. This method must take no arguments.

        上句翻译:找方法的名称。此方法必须不参数。

      name="bean"       type="xsd:string"

        英文解释:The name of the bean in the current or ancestor factories that the lookup method should resolve to. Often this bean will be a prototype, in which case the lookup method will return a distinct instance on every invocation. This is useful for single-threaded objects.

         上句翻译:查找方法应解析的当前或祖先工厂中bean的名称。通常这个bean将是一个原型,在这种情况下,lookup方法将在每次调用时返回一个不同的实例。这对单线程对象很有用。

(6)、replaced-method标签:

英文解释:Similar to the lookup method mechanism, the replaced-method element is used to control IoC container method overriding: Method Injection. This mechanism allows the overriding of a method with arbitrary code.

上句翻译:与查找方法机制类似,被替换的方法元素用于控制IoC容器方法覆盖:方法注入。此机制允许使用任意代码覆盖方法。

replaced-method标签的属性:

          name="name"           type="xsd:string"

            英文解释:The name of the method whose implementation must be replaced by the IoC container. If this method is not overloaded, there is no need to use arg-type subelements. If this method is overloaded, arg-type subelements must be used for all override definitions for the method.

            上句翻译:必须由IoC容器替换其实现的方法的名称。如果此方法未重载,则无需使用arg类型的子元素。如果此方法被重载,则必须将arg-type子元素用于该方法的所有覆盖定义。

         name="replacer"          type="xsd:string"

           英文解释:Bean name of an implementation of the MethodReplacer interface in the current or ancestor factories. This may be a singleton or prototype bean. If it is a prototype, a new instance will be used for each method replacement. Singleton usage is the norm.

            上句翻译:当前或祖先工厂中MethodReplacer接口的实现的Bean名称。这可能是单例或原型bean。如果它是原型,则每个方法替换将使用新实例。单身人士的使用是常态。

replaced-method标签的下层标签:

<xsd:choice minOccurs="0" maxOccurs="unbounded">

            <xsd:element ref="arg-type"/>

</xsd:choice>

          arg-type标签:

             英文解释:Identifies an argument for a replaced method in the event of method overloading.

             上句翻译:在方法重载的情况下标识替换方法的参数。

         arg-type标签属性:

                   name="match"           type="xsd:string"

                        英文解释:Specification of the type of an overloaded method argument as a String. For convenience, this may be a substring of the FQN. E.g. all the following would match "java.lang.String": - java.lang.String - String - Str As the number of arguments will be checked also, this convenience can often be used to save typing.

                          上句翻译:将重载方法参数的类型指定为String。为方便起见,这可能是FQN的子字符串。例如。以下所有内容都匹配“java.lang.String”: - java.lang.String - String - Str由于还将检查参数的数量,这种便利通常可用于保存输入。

 

猜你喜欢

转载自blog.csdn.net/xiao1_1bing/article/details/81084111