spring中props,list,set,map元素在…

转自:http://my.oschina.net/u/1012289/blog/129681

在spring容器中配置bean,常用到的元素除了和外,还有、、、 ,在hibernate等框架的配置文件中我们经常可以见到这些元素,下面是他们的具体用法。

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

    1 import java.util.Properties;  
     
    3 public class Person {  
       private Properties basicInfo;  
     
       public void setBasicInfo(Properties basicInfo) {  
           this.basicInfo = basicInfo;  
        }  
    9 }  



配置方式:

    1  
            
                
                    
                   1.75  
                    
                   120  
                
            
    10      




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



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

    1  
           25  
           2.26  
           140  
    5  
    6  
            
                
                    
    10                 1.75  
    11                  
    12                 120  
    13              
    14          
    15          
    16              
    17                 xiaohong  
    18                  
    19                 zhangsan  
    20              
    21          
    22  



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

Java代码   收藏代码

    1  
                
                    
                    
                    
              
 
    7  



4、spring配置里map的value是list配法

     
                 
                    
                       
                       
                      
                 
                 
                    
                       
                    
                 
    
 




4、

     
   
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
         
             
                classpath*:config.properties  
             
         
     
      
     
     
      
     
       



5、查找jndi的方式

     
             
                java:comp/env/jms/name  
             
     

     
     
     
       
      
       
   
    class="org.springframework.jndi.JndiTemplate">   
       
       
       
    weblogic.jndi.WLInitialContextFactory   
       
       
    t3://192.166.68.44:7001   
       
       
    weblogic.jndi.factories   
       
       
       
       
      
       
   
    class="org.springframework.jndi.JndiObjectFactoryBean">   
       
       
       
   
    class="org.springframework.jndi.JndiObjectFactoryBean">   
       
       
       
      
       
   
    class="org.springframework.jms.core.JmsTemplate">   
       
       
     

 


6、Spring 在配置中使用*.properties
     
   
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:util="http://www.springframework.org/schema/util"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans     
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
                http://www.springframework.org/schema/context    
                http://www.springframework.org/schema/context/spring-context-3.0.xsd  
                http://www.springframework.org/schema/util   
                http://www.springframework.org/schema/util/spring-util-3.0.xsd">  
         
         
         
          
         
        访法一  
         
            方法二  
         
     


    实现一:  
    package com.web.spring.other;  
      
    import org.springframework.beans.factory.annotation.Value;  
    import org.springframework.context.annotation.Bean;  
    import org.springframework.context.annotation.Configuration;  
    import org.springframework.context.annotation.ImportResource;  
      
    @Configuration  
    @ImportResource("classpath*:spring/spring-properties.xml")  
    public class AppConfig {  
        private @Value ("${jdbc.driverClassName}") String driverClassName;  
        @Bean (initMethod = "init")  
        public JDBCBean jdbc(){  
            JDBCBean jdbc=new JDBCBean();  
            jdbc.setDriverClassName(driverClassName);  
            return jdbc;  
        }  
      
    }  
      
    实现二:  
    package com.web.spring.other;  
      
    import org.springframework.beans.factory.annotation.Value;  
    import org.springframework.context.annotation.Bean;  
    import org.springframework.context.annotation.Configuration;  
      
    @Configuration  
    public class AppConfig {  
        private @Value ("#{jdbcProperties.driverClassName}") String driverClassName;  
        //private @Value("#{jdbcProperties['jdbc.driverClassName']}") String driverClassName;  
        @Bean (initMethod = "init")  
        public JDBCBean jdbc(){  
            JDBCBean jdbc=new JDBCBean();  
            jdbc.setDriverClassName(driverClassName);  
            return jdbc;  
        }  
      
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值