spring 例子 2

Spring依赖注入配置详解

博客分类:

Spring学习笔记

SpringBeanMySQLJDBCXML

bean的属性及构造器参数既可以引用容器中的其他bean,也可以是内联(inline)bean。在spring的XML配置中使用<property/>和<constructor-arg/>元素定义。

1.直接变量(基本类型、Strings 类型等。)

JavaBean PropertyEditor将用于把字符串从java.lang.String类型转化为实际的属性或参数类型。

Xml代码

  1. <beanid="myDataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">

  2. <!--resultsinasetDriverClassName(String)call-->

  3. <propertyname="driverClassName">

  4. <value>com.mysql.jdbc.Driver</value>

  5. </property>

  6. <propertyname="url">

  7. <value>jdbc:mysql://localhost:3306/mydb</value>

  8. </property>

  9. <propertyname="username">

  10. <value>root</value>

  11. </property>

  12. <propertyname="password">

  13. <value>masterkaoli</value>

  14. </property>

  15. </bean>

<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</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/mydb</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>masterkaoli</value> </property></bean>

可以在<property/> 和<constructor-arg/> 元素内部使用'value' 属性,这样会使我们的配置更简洁,比如下面的配置:

Xml代码

  1. <beanid="myDataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">

  2. <!--resultsinasetDriverClassName(String)call-->

  3. <propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>

  4. <propertyname="url"value="jdbc:mysql://localhost:3306/mydb"/>

  5. <propertyname="username"value="root"/>

  6. <propertyname="password"value="masterkaoli"/>

  7. </bean>

<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>

Spring团队更倾向采用属性方式(使用<value/> 元素)来定义value值。当然我们也可以按照下面这种方式配置一个java.util.Properties 实例:

Xml代码

  1. <beanid="mappings"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

  2. <!--typedasajava.util.Properties-->

  3. <propertyname="properties">

  4. <value>

  5. jdbc.driver.className=com.mysql.jdbc.Driver

  6. jdbcjdbc.url=jdbc:mysql://localhost:3306/mydb

  7. </value>

  8. </property>

  9. </bean>

<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>

看到什么了吗?如果采用上面的配置,Spring容器将使用JavaBean PropertyEditor 把<value/> 元素中的文本转换为一个java.util.Properties 实例。由于这种做法的简单,因此Spring团队在很多地方也会采用内嵌的<value/> 元素来代替value 属性。

2.idref 元素

idref 元素用来将容器内其它bean的id 传给<constructor-arg/> 或 <property/> 元素,同时提供错误验证功能。

Xml代码

  1. <beanid="theTargetBean"class="..."/>

  2. <beanid="theClientBean"class="...">

  3. <propertyname="targetName">

  4. <idrefbean="theTargetBean"/>

  5. </property>

  6. </bean>

<bean id="theTargetBean" class="..."/><bean id="theClientBean" class="..."> <property name="targetName"> <idref bean="theTargetBean" /> </property></bean>

上述bean定义片段完全地等同于(在运行时)以下的片段:

Xml代码

  1. <beanid="theTargetBean"class="..."/>

  2. <beanid="client"class="...">

  3. <propertyname="targetName"value="theTargetBean"/>

  4. </bean>

<bean id="theTargetBean" class="..." /><bean id="client" class="..."> <property name="targetName" value="theTargetBean" /></bean>

第一种形式比第二种更可取的主要原因是,使用idref标记允许容器在部署时 验证所被引用的bean是否存在。而第二种方式中,传给client bean的targetName属性值并没有被验证。任何的输入错误仅在client bean实际实例化时才会被发现(可能伴随着致命的错误)。如果client bean 是prototype类型的bean,则此输入错误(及由此导致的异常)可能在容器部署很久以后才会被发现。

此外,如果被引用的bean在同一XML文件内,且bean名字就是bean id,那么可以使用local属性,此属性允许XML解析器在解析XML文件时对引用的bean进行验证。

Xml代码

  1. <propertyname="targetName">

  2. <!--abeanwithanidof'theTargetBean'mustexist;otherwiseanXMLexceptionwillbethrown-->

  3. <idreflocal="theTargetBean"/>

  4. </property>

<property name="targetName"> <!-- a bean with an id of 'theTargetBean' must exist; otherwise an XML exception will be thrown --> <idref local="theTargetBean"/></property>

上面的例子中,与在ProxyFactoryBean bean定义中使用<idref/>元素指定AOP interceptor的相同之处在于:如果使用<idref/>元素指定拦截器名字,可以避免因一时疏忽导致的拦截器ID拼写错误。

3.引用其它的bean(协作者)

在<constructor-arg/> 或<property/> 元素内部还可以使用ref 元素。该元素用来将bean中指定属性的值设置为对容器中的另外一个bean的引用。如前所述,该引用bean将被作为依赖注入,而且在注入之前会被初始化(如果是singleton bean则已被容器初始化)。尽管都是对另外一个对象的引用,但是通过id/name指向另外一个对象却有三种不同的形式,不同的形式将决定如何处理作用域及验证。

第一种形式也是最常见的形式是通过使用<ref/> 标记指定bean 属性的目标bean,通过该标签可以引用同一容器或父容器内的任何bean(无论是否在同一XML文件中)。XML 'bean '元素的值既可以是指定bean的id 值也可以是其name 值。

Xml代码

<refbean="someBean"/>

<ref bean="someBean"/>

第二种形式是使用ref的local 属性指定目标bean,它可以利用XML解析器来验证所引用的bean是否存在同一文件中。local 属性值必须是目标bean的id属性值。如果在同一配置文件中没有找到引用的bean,XML解析器将抛出一个例外。如果目标bean是在同一文件内,使用local方式就是最好的选择(为了尽早地发现错误)。

Xml代码

<reflocal="someBean"/>

<ref local="someBean"/>

第三种方式是通过使用ref的parent 属性来引用当前容器的父容器中的bean。parent 属性值既可以是目标bean的id 值,也可以是name 属性值。而且目标bean必须在当前容器的父容器中。使用parent属性的主要用途是为了用某个与父容器中的bean同名的代理来包装父容器中的一个bean(例如,子上下文中的一个bean定义覆盖了他的父bean)。

Xml代码

  1. <!--intheparentcontext-->

  2. t;beanid="accountService"class="com.foo.SimpleAccountService"><!--insertdependenciesasrequiredashere--></bean>

<!-- in the parent context --> <bean id="accountService" class="com.foo.SimpleAccountService"> <!-- insert dependencies as required as here --> </bean>

Xml代码

  1. <!--inthechild(descendant)context-->

  2. <beanid="accountService"<--noticethatthenameofthisbeanisthesameasthenameofthe'parent'bean

  3. class="org.springframework.aop.framework.ProxyFactoryBean">

  4. <propertyname="target">

  5. <refparent="accountService"/><--noticehowwerefertotheparentbean

  6. </property>

  7. <!--insertotherconfigurationanddependenciesasrequiredashere-->

  8. </bean>

<!-- in the child (descendant) context --><bean id="accountService" <-- notice that the name of this bean is the same as the name of 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 as here --></bean>

4. 内部bean(使用内部bean,但该bean不能被其他bean使用)

所谓的内部bean(inner bean)是指在一个bean的<property/> 或 <constructor-arg/> 元素中使用<bean/> 元素定义的bean。内部bean定义不需要有id或name属性,即使指定id 或 name属性值也将会被容器忽略。

Xml代码

  1. <beanid="outer"class="...">

  2. <!--insteadofusingareferencetoatargetbean,simplydefinethetargetbeaninline-->

  3. <propertyname="target">

  4. <beanclass="com.example.Person"><!--thisistheinnerbean-->

  5. <propertyname="name"value="FionaApple"/>

  6. <propertyname="age"value="25"/>

  7. </bean>

  8. </property>

  9. </bean>

<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>

注意:内部bean中的scope标记及id或name属性将被忽略。内部bean总是匿名的且它们总是prototype模式的。同时将内部bean注入到包含该内部bean之外的bean是不可能的。

5.集合类型

通过<list/> 、<set/> 、<map/> 及<props/> 元素可以定义和设置与Java Collection 类型对应List 、Set 、Map 及Properties 的值。

javaBean代码:

Java代码

  1. packageexample;

  2. publicclassExampleBean{

  3. privateSet<String>sets=newHashSet<String>();

  4. privateList<String>lists=newArrayList<String>();

  5. privatePropertiesproperties=newProperties();

  6. privateMap<String,String>maps=newHashMap<String,String>();

  7. publicMap<String,String>getMaps(){

  8. returnmaps;

  9. }

  10. publicvoidsetMaps(Map<String,String>maps){

  11. this.maps=maps;

  12. }

  13. publicPropertiesgetProperties(){

  14. returnproperties;

  15. }

  16. publicvoidsetProperties(Propertiesproperties){

  17. this.properties=properties;

  18. }

  19. publicSet<String>getSets(){

  20. returnsets;

  21. }

  22. publicvoidsetSets(Set<String>sets){

  23. this.sets=sets;

  24. }

  25. publicList<String>getLists(){

  26. returnlists;

  27. }

  28. publicvoidsetLists(List<String>lists){

  29. this.lists=lists;

  30. }

  31. }

package example;public class ExampleBean {private Set<String> sets = new HashSet<String>();private List<String> lists = new ArrayList<String>();private Properties properties = new Properties();private Map<String, String> maps = new HashMap<String, String>();public Map<String, String> getMaps() { return maps; }public void setMaps(Map<String, String> maps) { this.maps = maps; }public Properties getProperties() { return properties; }public void setProperties(Properties properties) {this.properties = properties; } public Set<String> getSets() {return sets; } public void setSets(Set<String> sets) {this.sets = sets; } public List<String> getLists() { return lists; }public void setLists(List<String> lists) { this.lists = lists; }}

xml配置文件如下:

Xml代码

  1. <beanid="personService"class="example.ExampleBean">

  2. <propertyname="sets">

  3. <set>

  4. <value>第一个</value>

  5. <value>第二个</value>

  6. <value>第三个</value>

  7. </set>

  8. </property>

  9. <propertyname="lists">

  10. <list>

  11. <value>第一个list元素</value>

  12. <value>第二个list元素</value>

  13. <value>第三个list元素</value>

  14. </list>

  15. </property>

  16. <propertyname="properties">

  17. <props>

  18. <propkey="key1">value1</prop>

  19. <propkey="key2">value2</prop>

  20. <propkey="key3">value3</prop>

  21. </props>

  22. </property>

  23. <propertyname="maps">

  24. <map>

  25. <entrykey="key-1"value="value-1"/>

  26. <entrykey="key-2"value="value-2"/>

  27. <entrykey="key-3"value="value-3"/>

  28. </map>

  29. </property>

  30. </bean>

<bean id="personService" class="example.ExampleBean"> <property name="sets"> <set> <value>第一个</value> <value>第二个</value> <value>第三个</value> </set> </property> <property name="lists"> <list> <value>第一个list元素</value> <value>第二个list元素</value> <value>第三个list元素</value> </list> </property> <property name="properties"> <props> <prop key="key1">value1</prop> <prop key="key2">value2</prop> <prop key="key3">value3</prop> </props> </property> <property name="maps"> <map> <entry key="key-1" value="value-1"/> <entry key="key-2" value="value-2"/> <entry key="key-3" value="value-3"/> </map> </property> </bean>

Xml代码

  1. <beanid="moreComplexObject"class="example.ComplexObject">

  2. <!--resultsinasetAdminEmails(java.util.Properties)call-->

  3. <propertyname="adminEmails">

  4. <props>

  5. <propkey="administrator">administrator@example.org</prop>

  6. <propkey="support">support@example.org</prop>

  7. <propkey="development">development@example.org</prop>

  8. </props>

  9. </property>

  10. <!--resultsinasetSomeList(java.util.List)call-->

  11. <propertyname="someList">

  12. <list>

  13. <value>alistelementfollowedbyareference</value>

  14. <refbean="myDataSource"/>

  15. </list>

  16. </property>

  17. <!--resultsinasetSomeMap(java.util.Map)call-->

  18. <propertyname="someMap">

  19. <map>

  20. <entry>

  21. <key>

  22. <value>anentry</value>

  23. </key>

  24. <value>justsomestring</value>

  25. </entry>

  26. <entry>

  27. <key>

  28. <value>aref</value>

  29. </key>

  30. <refbean="myDataSource"/>

  31. </entry>

  32. </map>

  33. </property>

  34. <!--resultsinasetSomeSet(java.util.Set)call-->

  35. <propertyname="someSet">

  36. <set>

  37. <value>justsomestring</value>

  38. <refbean="myDataSource"/>

  39. </set>

  40. </property>

  41. </bean>

<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> <value>an entry</value> </key> <value>just some string</value> </entry> <entry> <key> <value>a ref</value> </key> <ref bean="myDataSource" /> </entry> </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>

注意:map的key或value值,或set的value值还可以是以下元素:

bean | ref | idref | list | set | map | props | value | null

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值