分享

【转】spring中props,list,set,map元素的用法

 KhanLau 2015-09-16
     在spring容器中配置bean,常用到的元素除了<value>和<ref>外,还有<props>、<list>、<set>、<map>,在hibernate等框架的配置文件中我们经常可以见到这些元素,下面是他们的具体用法。

1.<props>元素
<props>创建了一个注入的java.util.Properties元素。例如每个人都有身高、体重等基本信息

Java代码  收藏代码
  1. 1 import java.util.Properties;  
  2. 2   
  3. 3 public class Person {  
  4. 4     private Properties basicInfo;  
  5. 5   
  6. 6     public void setBasicInfo(Properties basicInfo) {  
  7. 7         this.basicInfo = basicInfo;  
  8. 8      }  
  9. 9 }  


配置方式:
Java代码  收藏代码
  1. 1 <bean id="person" class="Person">  
  2. 2         <property name="basicInfo">  
  3. 3             <props>  
  4. 4                 <!-- 身高 -->  
  5. 5                 <prop key="stature">1.75</prop>  
  6. 6                 <!-- 体重 -->  
  7. 7                 <prop key="avoirdupois">120</prop>  
  8. 8             </props>  
  9. 9         </property>  
  10. 10     </bean>  



2.<list>元素
<list>元素对应于java.util.ArrayList.例如每个人都有一些朋友
Java代码  收藏代码
  1. 1 package org.hag.flex.model;  
  2. 2   
  3. 3 import java.util.List;  
  4. 4 import java.util.Properties;  
  5. 5   
  6. 6 public class Person {  
  7. 7     private Properties basicInfo;  
  8. 8     private List friends;  
  9. 9   
  10. 10     public void setBasicInfo(Properties basicInfo) {  
  11. 11         this.basicInfo = basicInfo;  
  12. 12      }  
  13. 13   
  14. 14     public void setFriends(List friends) {  
  15. 15         this.friends = friends;  
  16. 16      }  
  17. 17 }  
  18. 18  


配置该person的朋友有小红、姚明和张三

Java代码  收藏代码
  1. 1 <bean id="yaoming" class="Person">  
  2. 2         <prop key="age">25</prop>  
  3. 3         <prop key="stature">2.26</prop>  
  4. 4         <prop key="avoirdupois">140</prop>  
  5. 5 </bean>  
  6. 6 <bean id="person" class="Person">  
  7. 7         <property name="basicInfo">  
  8. 8             <props>  
  9. 9                 <!-- 身高 -->  
  10. 10                 <prop key="stature">1.75</prop>  
  11. 11                 <!-- 体重 -->  
  12. 12                 <prop key="avoirdupois">120</prop>  
  13. 13             </props>  
  14. 14         </property>  
  15. 15         <property name="firends">  
  16. 16             <list>  
  17. 17                 <value>xiaohong</value>  
  18. 18                 <ref local="yaoming"/>  
  19. 19                 <value>zhangsan</value>  
  20. 20             </list>  
  21. 21         </property>  
  22. 22 </bean>  


3.<set>元素
<set>元素和<list>元素的用法一样,不同的是他注入的是java.util.Set元素。
4.<map>元素
<map>元素用来注入java.util.Map元素。

Java代码  收藏代码
  1. 1 <property name="score">  
  2. 2             <map>  
  3. 3                 <entry key="math" value="150"></entry>  
  4. 4                 <entry key="english" value="140"></entry>  
  5. 5                 <entry key="chinese" value="60"></entry>  
  6. 6             </map>  
  7. 7 </property>  


4、spring配置里map的value是list配法
Java代码  收藏代码
  1. <map>  
  2.             <entry key="a">  
  3.                <list>  
  4.                   <ref bean="b"/>  
  5.                   <ref bean="c"/>  
  6.                </list>   
  7.             </entry>  
  8.             <entry key="d">  
  9.                <list>  
  10.                   <ref bean="e"/>  
  11.                </list>  
  12.             </entry>  
  13.  </map>  



4、<!-- 外部配置文件 -->
Java代码  收藏代码
  1. <!-- 加载jdbc属性文件 -->  
  2. <bean id="propertyConfigurer"  
  3.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  4.     <property name="locations">  
  5.         <list>  
  6.             <value>classpath*:config.properties</value>  
  7.         </list>  
  8.     </property>  
  9. </bean>  
  10.   
  11. <!-- 外部配置文件 -->  
  12. <context:property-placeholder location="classpath*:application.properties" />  
  13.   
  14. <!--定义全局连接命名变量 -->  
  15. <util:properties id="posmqProperties" location="classpath:/jms/pos-mq-jndi.properties" />   


5、查找jndi的方式
Java代码  收藏代码
  1. <bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">  
  2.         <property name="jndiName">  
  3.             <value>java:comp/env/jms/name</value>  
  4.         </property>  
  5. </bean>  


Java代码  收藏代码
  1. <!--  MQ 连接工厂 -->  
  2. <jee:jndi-lookup id="posMqConnectionFactory" jndi-name="jms/posmq" environment-ref="posmqProperties" />  
  3. <!--定义全局连接命名变量 -->  
  4. <util:properties id="posmqProperties" location="classpath:/jms/pos-mq-jndi.properties" />   
  5.   
  6. <!-- Jndi -->   
  7. <bean id="jndiTemplate"   
  8. class="org.springframework.jndi.JndiTemplate">   
  9. <property name="environment">   
  10. <props>   
  11. <prop key="java.naming.factory.initial">   
  12. weblogic.jndi.WLInitialContextFactory   
  13. </prop>   
  14. <prop key="java.naming.provider.url">   
  15. t3://192.166.68.44:7001   
  16. </prop>   
  17. <prop key="java.naming.factory.url.pkgs">   
  18. weblogic.jndi.factories   
  19. </prop>   
  20. </props>   
  21. </property>   
  22. </bean>   
  23.   
  24. <!-- jms sender -->   
  25. <bean id="jmsConnectionFactory"   
  26. class="org.springframework.jndi.JndiObjectFactoryBean">   
  27. <property name="jndiTemplate" ref="jndiTemplate" />   
  28. <property name="jndiName" value="ConnectionFactory" />   
  29. </bean>   
  30. <bean id="jmsQueue"   
  31. class="org.springframework.jndi.JndiObjectFactoryBean">   
  32. <property name="jndiTemplate" ref="jndiTemplate"></property>   
  33. <property name="jndiName" value="Queue"></property>   
  34. </bean>   
  35.   
  36. <!-- jms template -->   
  37. <bean id="jmsTemplate"   
  38. class="org.springframework.jms.core.JmsTemplate">   
  39. <property name="connectionFactory" ref="jmsConnectionFactory"></property>   
  40. <property name="defaultDestination" ref="jmsQueue"></property>   
  41. </bean>  
 


6、Spring 在配置中使用*.properties
Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www./schema/beans"  
  3.     xmlns:xsi="http://www./2001/XMLSchema-instance"   
  4.     xmlns:context="http://www./schema/context"  
  5.     xmlns:util="http://www./schema/util"  
  6.     xsi:schemaLocation="http://www./schema/beans     
  7.             http://www./schema/beans/spring-beans-3.0.xsd    
  8.             http://www./schema/context    
  9.             http://www./schema/context/spring-context-3.0.xsd  
  10.             http://www./schema/util   
  11.             http://www./schema/util/spring-util-3.0.xsd">  
  12.     <context:annotation-config/>  
  13.     <!-- picks up and registers AppConfig as a bean definition -->  
  14.     <context:component-scan base-package="com.web.spring.other" />  
  15.       
  16.     <bean class="com.web.spring.other.AppConfig"/>  
  17.     访法一  
  18.     <context:property-placeholder location="classpath:jdbc.properties" />  
  19.         方法二  
  20.     <util:properties id="jdbcProperties" location="classpath:jdbc.properties"/>  
  21. </beans>  


Java代码  收藏代码
  1. 实现一:  
  2. package com.web.spring.other;  
  3.   
  4. import org.springframework.beans.factory.annotation.Value;  
  5. import org.springframework.context.annotation.Bean;  
  6. import org.springframework.context.annotation.Configuration;  
  7. import org.springframework.context.annotation.ImportResource;  
  8.   
  9. @Configuration  
  10. @ImportResource("classpath*:spring/spring-properties.xml")  
  11. public class AppConfig {  
  12.     private @Value("${jdbc.driverClassName}") String driverClassName;  
  13.     @Bean(initMethod = "init")  
  14.     public JDBCBean jdbc(){  
  15.         JDBCBean jdbc=new JDBCBean();  
  16.         jdbc.setDriverClassName(driverClassName);  
  17.         return jdbc;  
  18.     }  
  19.   
  20. }  
  21.   
  22. 实现二:  
  23. package com.web.spring.other;  
  24.   
  25. import org.springframework.beans.factory.annotation.Value;  
  26. import org.springframework.context.annotation.Bean;  
  27. import org.springframework.context.annotation.Configuration;  
  28.   
  29. @Configuration  
  30. public class AppConfig {  
  31.     private @Value("#{jdbcProperties.driverClassName}") String driverClassName;  
  32.     //private @Value("#{jdbcProperties['jdbc.driverClassName']}") String driverClassName;  
  33.     @Bean(initMethod = "init")  
  34.     public JDBCBean jdbc(){  
  35.         JDBCBean jdbc=new JDBCBean();  
  36.         jdbc.setDriverClassName(driverClassName);  
  37.         return jdbc;  
  38.     }  
  39.   
  40. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约