在spring容器中配置bean,常用到的元素除了<value>和<ref>外,还有<props>、<list>、<set>、<map>,在hibernate等框架的配置文件中我们经常可以见到这些元素,下面是他们的具体用法。
1.<props>元素 <props>创建了一个注入的java.util.Properties元素。例如每个人都有身高、体重等基本信息 配置方式: 2.<list>元素 <list>元素对应于java.util.ArrayList.例如每个人都有一些朋友 配置该person的朋友有小红、姚明和张三 3.<set>元素 <set>元素和<list>元素的用法一样,不同的是他注入的是java.util.Set元素。 4.<map>元素 <map>元素用来注入java.util.Map元素。 4、spring配置里map的value是list配法 4、<!-- 外部配置文件 --> 5、查找jndi的方式 6、Spring 在配置中使用*.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>
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
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>
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>
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" />
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>
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 ("${jdbc.driverClassName}") String driverClassName;
- (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 ("#{jdbcProperties.driverClassName}") String driverClassName;
- //private @Value("#{jdbcProperties['jdbc.driverClassName']}") String driverClassName;
- (initMethod = "init")
- public JDBCBean jdbc(){
- JDBCBean jdbc=new JDBCBean();
- jdbc.setDriverClassName(driverClassName);
- return jdbc;
- }
- }