首页 > 代码库 > spring 注入java.util.Properties 属性两种xml中的配置练习
spring 注入java.util.Properties 属性两种xml中的配置练习
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="propertiesInjectBean1" class="com.doctor.spring.context.inject.PropertiesInjectBean1"> <property name="properties"> <value> name=doctor sex=man address=alien </value> </property> </bean> <bean id="propertiesInjectBean2" class="com.doctor.spring.context.inject.PropertiesInjectBean2"> <property name="properties"> <props> <prop key="name">doctor</prop> <prop key="address">alien</prop> </props> </property> </bean> </beans>
类如下:
package com.doctor.spring.context.inject; import java.util.Properties; public class PropertiesInjectBean1 { private Properties properties; public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } }
package com.doctor.spring.context.inject; import java.util.Properties; public class PropertiesInjectBean2 { private Properties properties; public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } }
启动容器:
package com.doctor.spring.context.inject; import org.springframework.context.support.ClassPathXmlApplicationContext; public class PropertiesInjectPractice { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/injectValueIntoBeanPropertiesPractice/propertiesInjectPracticeConfigurer.xml"); PropertiesInjectBean1 injectBean1 = context.getBean(PropertiesInjectBean1.class); System.out.println(injectBean1.getProperties()); PropertiesInjectBean2 injectBean2 = context.getBean(PropertiesInjectBean2.class); System.out.println(injectBean2.getProperties()); context.close(); } }输出:
{address=alien, name=doctor, sex=man}
{address=alien, name=doctor}
这两种都可以注入java.util.Properties 属性.
spring 注入java.util.Properties 属性两种xml中的配置练习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。