转自:http://my.oschina.net/u/1012289/blog/129681 在spring容器中配置bean,常用到的元素除了和外,还有、、、 ,在hibernate等框架的配置文件中我们经常可以见到这些元素,下面是他们的具体用法。 1.元素 创建了一个注入的java.util.Properties元素。例如每个人都有身高、体重等基本信息 1 import java.util.Properties; 2 3 public class Person { 4 private Properties basicInfo; 5 6 public void setBasicInfo(Properties basicInfo) { 7 this.basicInfo = basicInfo; 8 } 9 } 配置方式: 1 2 3 4 5 1.75 6 7 120 8 9 10 2.元素 元素对应于java.util.ArrayList.例如每个人都有一些朋友 1 package org.hag.flex.model; 2 3 import java.util.List; 4 import java.util.Properties; 5 6 public class Person { 7 private Properties basicInfo; 8 private List friends; 9 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 2 25 3 2.26 4 140 5 6 7 8 9 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 2 3 4 5 6 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; } }