spring复杂类型属性注入

//创建实体类
package com.sq.service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 *@author sq
 *
 */
public class User {
	
	private String[] a;
	private List<String> b;
	private Map<String,String> c;
	private  Properties properties;
	public String[] getA() {
		return a;
	}
	public void setA(String[] a) {
		this.a = a;
	}
	public List<String> getB() {
		return b;
	}
	public void setB(List<String> b) {
		this.b = b;
	}
	public Map<String, String> getC() {
		return c;
	}
	public void setC(Map<String, String> c) {
		this.c = c;
	}
	public Properties getProperties() {
		return properties;
	}
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	@Override
	public String toString() {
		return "User [a=" + Arrays.toString(a) + ", b=" + b + ", c=" + c
				+ ", properties=" + 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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        
        
     <bean id="user" class="com.sq.service.User">
     		<!-- 数组 -->
     		<property name="a">
     			<array>
     				<value>1</value>
     				<value>2</value>
     				<value>3</value>
     			</array>
     		</property>
     		<!-- list集合 -->
     		<property name="b">
     			<list>
     				<value>1</value>
     				<value>1</value>
     				<value>1</value>
     				<value>1</value>
     			</list>
     		</property>
     		<!-- map集合 -->
     		<property name="c">
     			<map>
     				<entry key="1" value="1"></entry>	
     				<entry key="2" value="2"></entry>	
     				<entry key="3" value="3"></entry>	
     				<entry key="4" value="4"></entry>	
     			</map>
     		</property>
     		<!-- properties -->
     		<property name="properties">
     			<props>
     				<prop key="driverclass">com.mysql.jdbc.Driver</prop>
     				<prop key="username">root</prop>
     			</props>
     		</property>
     </bean>   
</beans>
测试
package com.sq.service;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *@author sq
 *
 */
public class Test1 {
	
	@Test
	public void test(){
		
		//1.加载spring配置文件,根据创建对象
		ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
		//2.得到配置对象
		User service = (User) context.getBean("user");
		System.out.println(service);
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值