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

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

1.<props>元素
<props>创建了一个注入的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 <bean id="person" class="Person">
2 <property name="basicInfo">
3 <props>
4 <!-- 身高 -->
5 <prop key="stature">1.75</prop>
6 <!-- 体重 -->
7 <prop key="avoirdupois">120</prop>
8 </props>
9 </property>
10 </bean>



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


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

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


4、spring配置里map的value是list配法
<map>
<entry key="a">
<list>
<ref bean="b"/>
<ref bean="c"/>
</list>
</entry>
<entry key="d">
<list>
<ref bean="e"/>
</list>
</entry>
</map>



4、<!-- 外部配置文件 -->
<!-- 加载jdbc属性文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:config.properties</value>
</list>
</property>
</bean>

<!-- 外部配置文件 -->
<context:property-placeholder location="classpath*:application.properties" />

<!--定义全局连接命名变量 -->
<util:properties id="posmqProperties" location="classpath:/jms/pos-mq-jndi.properties" />


5、查找jndi的方式
<bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jms/name</value>
</property>
</bean>


<!--  MQ 连接工厂 -->
<jee:jndi-lookup id="posMqConnectionFactory" jndi-name="jms/posmq" environment-ref="posmqProperties" />
<!--定义全局连接命名变量 -->
<util:properties id="posmqProperties" location="classpath:/jms/pos-mq-jndi.properties" />

<!-- Jndi -->
<bean id="jndiTemplate"
class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">
weblogic.jndi.WLInitialContextFactory
</prop>
<prop key="java.naming.provider.url">
t3://192.166.68.44:7001
</prop>
<prop key="java.naming.factory.url.pkgs">
weblogic.jndi.factories
</prop>
</props>
</property>
</bean>

<!-- jms sender -->
<bean id="jmsConnectionFactory"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName" value="ConnectionFactory" />
</bean>
<bean id="jmsQueue"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate"></property>
<property name="jndiName" value="Queue"></property>
</bean>

<!-- jms template -->
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory"></property>
<property name="defaultDestination" ref="jmsQueue"></property>
</bean>




6、Spring 在配置中使用*.properties
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<context:annotation-config/>
<!-- picks up and registers AppConfig as a bean definition -->
<context:component-scan base-package="com.web.spring.other" />

<bean class="com.web.spring.other.AppConfig"/>
访法一
<context:property-placeholder location="classpath:jdbc.properties" />
方法二
<util:properties id="jdbcProperties" location="classpath:jdbc.properties"/>
</beans>


实现一:
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、付费专栏及课程。

余额充值