依赖注入的详细配置
Dependencies and configuration in detail直接代码 <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- results in a setDriverClassName(String) call --> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mydb"/> <property name="username" value="root"/> <property name="password" value="masterkaoli"/> </bean> <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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/mydb" p:username="root" p:password="masterkaoli"/> </beans> <bean id="mappings" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- typed as a java.util.Properties --> <property name="properties"> <value> jdbc.driver.className=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mydb </value> </property> </bean> <bean id="theTargetBean" class="..."/> <bean id="theClientBean" class="..."> <property name="targetName"> <idref bean="theTargetBean" /> </property> </bean> <bean id="theTargetBean" class="..." /> <bean id="client" class="..."> <property name="targetName" value="theTargetBean" /> </bean> <property name="targetName"> <!-- a bean with id theTargetBean must exist; otherwise an exception will be thrown --> <idref bean="theTargetBean"/> </property> References to other beans (collaborators)引用其他bean(collaborators)
<ref bean="someBean"/> <!-- in the parent context --> <bean id="accountService" class="com.foo.SimpleAccountService"> <!-- insert dependencies as required as here --> </bean> <!-- in the child (descendant) context --> <bean id="accountService" <-- bean name is the same as the parent bean --> class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target"> <ref parent="accountService"/> <!-- notice how we refer to the parent bean --> </property> <!-- insert other configuration and dependencies as required here --> </bean> Inner beansA <bean id="outer" class="..."> <!-- instead of using a reference to a target bean,simply define the target bean inline --> <property name="target"> <bean class="com.example.Person"> <!-- this is the inner bean --> <property name="name" value="Fiona Apple"/> <property name="age" value="25"/> </bean> </property> </bean> An inner bean definition does not require a defined id or name; the container ignores these values. It also ignores the CollectionsIn the <bean id="moreComplexObject" class="example.ComplexObject"> <!-- results in a setAdminEmails(java.util.Properties) call --> <property name="adminEmails"> <props> <prop key="administrator">administrator@example.org</prop> <prop key="support">support@example.org</prop> <prop key="development">development@example.org</prop> </props> </property> <!-- results in a setSomeList(java.util.List) call --> <property name="someList"> <list> <value>a list element followed by a reference</value> <ref bean="myDataSource" /> </list> </property> <!-- results in a setSomeMap(java.util.Map) call --> <property name="someMap"> <map> <entry key="an entry" value="just some string"/> <entry key ="a ref" value-ref="myDataSource"/> </map> </property> <!-- results in a setSomeSet(java.util.Set) call --> <property name="someSet"> <set> <value>just some string</value> <ref bean="myDataSource" /> </set> </property> </bean> The value of a map key or value,or a set value,can also again be any of the following elements: bean | ref | idref | list | set | map | props | value | null Collection mergingThe Spring container also supports themergingof collections. An application developer can define a parent-style This section on merging discusses the parent-child bean mechanism. Readers unfamiliar with parent and child bean definitions may wish to read therelevant sectionbefore continuing. The following example demonstrates collection merging: <beans> <bean id="parent" abstract="true" class="example.ComplexObject"> <property name="adminEmails"> <props> <prop key="administrator">administrator@example.com</prop> <prop key="support">support@example.com</prop> </props> </property> </bean> <bean id="child" parent="parent"> <property name="adminEmails"> <!-- the merge is specified on the child collection definition --> <props merge="true"> <prop key="sales">sales@example.com</prop> <prop key="support">support@example.co.uk</prop> </props> </property> </bean> <beans> Notice the use of the administrator=administrator@example.com sales=sales@example.com support=support@example.co.uk The child This merging behavior applies similarly to the Limitations of collection mergingYou cannot merge different collection types (such as a Strongly-typed collectionWith the introduction of generic types in Java 5,you can use strongly typed collections. That is,it is possible to declare a public class Foo { private Map<String,Float> accounts; public void setAccounts(Map<String,Float> accounts) { this.accounts = accounts; } } <beans> <bean id="foo" class="x.y.Foo"> <property name="accounts"> <map> <entry key="one" value="9.99"/> <entry key="two" value="2.75"/> <entry key="six" value="3.99"/> </map> </property> </bean> </beans> When the Null and empty string valuesSpring treats empty arguments for properties and the like as empty <bean class="ExampleBean"> <property name="email" value=""/> </bean> The preceding example is equivalent to the following Java code: <bean class="ExampleBean"> <property name="email"> <null/> </property> </bean> The above configuration is equivalent to the following Java code: XML shortcut with the p-namespaceThe p-namespace enables you to use the Spring supports extensible configuration formatswith namespaces,which are based on an XML Schema definition. The The following example shows two XML snippets that resolve to the same result: The first uses standard XML format and the second uses the p-namespace. <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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean name="classic" class="com.example.ExampleBean"> <property name="email" value="foo@bar.com"/> </bean> <bean name="p-namespace" class="com.example.ExampleBean" p:email="foo@bar.com"/> </beans> The example shows an attribute in the p-namespace called email in the bean definition. This tells Spring to include a property declaration. As previously mentioned,the p-namespace does not have a schema definition,so you can set the name of the attribute to the property name. This next example includes two more bean definitions that both have a reference to another bean: <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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean name="john-classic" class="com.example.Person"> <property name="name" value="John Doe"/> <property name="spouse" ref="jane"/> </bean> <bean name="john-modern" class="com.example.Person" p:name="John Doe" p:spouse-ref="jane"/> <bean name="jane" class="com.example.Person"> <property name="name" value="Jane Doe"/> </bean> </beans> As you can see,this example includes not only a property value using the p-namespace,but also uses a special format to declare property references. Whereas the first bean definition uses
XML shortcut with the c-namespaceSimilar to thethe section called “XML shortcut with the p-namespace”,thec-namespace,newly introduced in Spring 3.1,allows usage of inlined attributes for configuring the constructor arguments rather then nested Let’s review the examples fromthe section called “Constructor-based dependency injection”with the <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="bar" class="x.y.Bar"/> <bean id="baz" class="x.y.Baz"/> <!-- traditional declaration --> <bean id="foo" class="x.y.Foo"> <constructor-arg ref="bar"/> <constructor-arg ref="baz"/> <constructor-arg value="foo@bar.com"/> </bean> <!-- c-namespace declaration --> <bean id="foo" class="x.y.Foo" c:bar-ref="bar" c:baz-ref="baz" c:email="foo@bar.com"/> </beans> The For the rare cases where the constructor argument names are not available (usually if the bytecode was compiled without debugging information),one can use fallback to the argument indexes: <!-- c-namespace index declaration --> <bean id="foo" class="x.y.Foo" c:_0-ref="bar" c:_1-ref="baz"/>
In practice,the constructor resolutionmechanismis quite efficient in matching arguments so unless one really needs to,we recommend using the name notation through-out your configuration. Compound property namesYou can use compound or nested property names when you set bean properties,as long as all components of the path except the final property name are not <bean id="foo" class="foo.Bar"> <property name="fred.bob.sammy" value="123" /> </bean> The 4.4.3Using depends-onIf a bean is a dependency of another that usually means that one bean is set as a property of another. Typically you accomplish this with the <bean id="beanOne" class="ExampleBean" depends-on="manager"/> <bean id="manager" class="ManagerBean" /> To express a dependency on multiple beans,supply a list of bean names as the value of the <bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao"> <property name="manager" ref="manager" /> </bean> <bean id="manager" class="ManagerBean" /> <bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |