JavaShuo
栏目
标签
【转】spring中props,list,set,map元素在配置文件中的用法
时间 2019-12-08
标签
spring
props
list
set
map
元素
配置文件
用法
栏目
Spring
繁體版
原文
原文链接
在spring容器中配置bean,经常使用到的元素除了<value>和<ref>外,还有<props>、<list>、<set>、<map>,在hibernate等框架的配置文件中咱们常常能够见到这些元素,下面是他们的具体用法。
1.<props>元素
<props>建立了一个注入的java.util.Properties元素。例如每一个人都有身高、体重等基本信息
Java代码
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
}
配置方式:
Java代码
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.例如每一个人都有一些朋友
Java代码
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的朋友有小红、姚明和张三
Java代码
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元素。
Java代码
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>
四、spring配置里map的value是list配法
Java代码
<map>
<entry key=
"a"
>
<list>
<ref bean=
"b"
/>
<ref bean=
"c"
/>
</list>
</entry>
<entry key=
"d"
>
<list>
<ref bean=
"e"
/>
</list>
</entry>
</map>
四、<!-- 外部配置文件 -->
Java代码
<!-- 加载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"
/>
五、查找jndi的方式
Java代码
<bean id=
"jmsConnectionFactory"
class
=
"org.springframework.jndi.JndiObjectFactoryBean"
>
<property name=
"jndiName"
>
<value>java:comp/env/jms/name</value>
</property>
</bean>
Java代码
<!-- 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>
六、Spring 在配置中使用*.properties
Java代码
<?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>
Java代码
实现一:
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;
}
}
相关文章
1.
Spring中注入List Set Map Properties的配置文件
2.
java中LIST、SET、MAP
3.
Spring配置Map,List
4.
Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
5.
c++11模板:容器(map,set,list,vector)中元素类型转换
6.
Java中List、Set、Map区别
7.
List,Set,Map用法以及区别(转)
8.
Spring中常用类型的bean配置(Map,List,Set,基本类型)
9.
C++中set map的用法
10.
将List集合中的元素反转
更多相关文章...
•
在Spring中使用Redis
-
Redis教程
•
Spring中Bean的作用域
-
Spring教程
•
C# 中 foreach 遍历的用法
•
SpringBoot中properties文件不能自动提示解决方法
相关标签/搜索
List、Set、Map
配置文件
list&set
map+set
list&map
中元
中转
中文
文中
SSH配置文件
Spring
Spring教程
SQLite教程
MySQL教程
注册中心
文件系统
spring cloud
0
分享到微博
分享到微信
分享到QQ
每日一句
每一个你不满意的现在,都有一个你没有努力的曾经。
最新文章
1.
说说Python中的垃圾回收机制?
2.
蚂蚁金服面试分享,阿里的offer真的不难,3位朋友全部offer
3.
Spring Boot (三十一)——自定义欢迎页及favicon
4.
Spring Boot核心架构
5.
IDEA创建maven web工程
6.
在IDEA中利用maven创建java项目和web项目
7.
myeclipse新导入项目基本配置
8.
zkdash的安装和配置
9.
什么情况下会导致Python内存溢出?要如何处理?
10.
CentoOS7下vim输入中文
本站公众号
欢迎关注本站公众号,获取更多信息
相关文章
1.
Spring中注入List Set Map Properties的配置文件
2.
java中LIST、SET、MAP
3.
Spring配置Map,List
4.
Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
5.
c++11模板:容器(map,set,list,vector)中元素类型转换
6.
Java中List、Set、Map区别
7.
List,Set,Map用法以及区别(转)
8.
Spring中常用类型的bean配置(Map,List,Set,基本类型)
9.
C++中set map的用法
10.
将List集合中的元素反转
>>更多相关文章<<