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 属性.
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。