spring-framework-reference(5.1.1.RELEASE)中文版——Core部分 (9)

静态工厂方法的参数由元素提供,与实际使用的构造函数完全相同。工厂方法返回的类的类型不必与包含静态工厂方法的类的类型相同(尽管在本例中是这样)。实例(非静态)工厂方法可以以一种基本相同的方式使用(除了使用factory-bean属性而不是类属性之外),因此我们在这里不讨论这些细节。

在静态工厂类中的静态方法返回的类可以时任意类型的。

Dependencies and Configuration in Detail

如前一节所述,可以将bean属性和构造函数参数定义为对其他托管bean(协作者)的引用或内联定义的值。为此,Spring基于xml的配置元数据支持其

Straight Values (Primitives, Strings, and so on)

元素的value属性将属性或构造函数参数指定为人类可读的字符串表示形式。Spring的转换服务用于将这些值从字符串转换为属性或参数的实际类型。下面的例子显示了正在设置的各种值:

<bean destroy-method="close"> <!-- results in a setDriverClassName(String) call --> <property value="com.mysql.jdbc.Driver"/> <property value="jdbc:mysql://localhost:3306/mydb"/> <property value="root"/> <property value="masterkaoli"/> </bean>

下面的示例使用p-namespace来实现更简洁的XML配置:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans "> <bean destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/mydb" p:username="root" p:password="masterkaoli"/> </beans>

前面的XML更简洁。但是,在运行时而不是在设计时发现拼写错误,除非使用支持在创建bean定义时自动完成属性的IDE(如IntelliJ IDEA或Spring工具套件)。强烈建议提供这种IDE援助。

您还可以配置java.util.Properties实例如下:

<bean> <!-- typed as a java.util.Properties --> <property> <value> jdbc.driver.className=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mydb </value> </property> </bean>

Spring容器通过使用JavaBeansPropertyEditor机制将

The idref element

idref元素只是将容器中另一个bean的id(字符串值,而不是引用)传递给<构造函数-arg/>或

<bean/> <bean> <property> <idref bean="theTargetBean"/> </property> </bean>

前面的bean定义片段与下面的片段完全等价(在运行时):

<bean /> <bean> <property value="theTargetBean"/> </bean>

第一种形式比第二种更可取,因为使用idref标记可以让容器在部署时验证所引用的命名bean实际存在。在第二个变体中,对传递给客户机bean的targetName属性的值不执行验证。只有当客户机bean实际实例化时,才会发现输入错误(最有可能导致致命的结果)。如果客户机bean是多例,那么这种类型和由此产生的异常可能在部署容器很久之后才会被发现。

4.0 bean XSD不再支持idref元素上的local属性,因为它不再为常规bean引用提供值。升级到4.0架构时,将现有的idref local引用更改为idref bean。

元素带来价值的一个常见地方(至少在Spring 2.0之前的版本中)是ProxyFactoryBean定义中的AOP拦截器配置。在指定拦截器名称时使用元素可以防止对拦截器ID的拼写错误。

References to Other Beans (Collaborators)

ref元素是<construct -arg/>或

通过

<ref bean="someBean"/>

通过父属性指定目标bean将创建对当前容器的父容器中的bean的引用。父属性的值可能与目标bean的id属性或目标bean的name属性中的一个值相同。目标bean必须位于当前bean的父容器中。当您有一个容器层次结构,并且希望使用与父bean同名的代理将现有bean包装在父容器中时,您应该主要使用这个bean引用变体。下面展示如何使用parent属性

<!-- in the parent context --> <bean> <!-- insert dependencies as required as here --> </bean> <!-- in the child (descendant) context --> <bean <!-- bean name is the same as the parent bean -->> <property> <ref parent="accountService"/> <!-- notice how we refer to the parent bean --> </property> <!-- insert other configuration and dependencies as required here --> </bean>

我们要注意如何引用的parent。

Inner Beans <bean> <!-- instead of using a reference to a target bean, simply define the target bean inline --> <property> <bean> <!-- this is the inner bean --> <property value="Fiona Apple"/> <property value="25"/> </bean> </property> </bean>

内部bean定义不需要定义ID或名称。如果指定,容器不会使用这样的值作为标识符。容器在创建时也会忽略范围标志,因为内部bean总是匿名的,并且总是与外部bean一起创建的。不可能独立访问内部bean,也不可能将它们注入协作bean(而不是封闭bean)中。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzdjpw.html