复杂类型的属性注入

1、有如下几种复杂类型:

1)数组(2)List 集合(3)Map 集合(4)Properties 类

2、具体实现

  1)编写一个普通类

  User.java

package com.siwuxie095.property;

  

import java.util.Arrays;

import java.util.List;

import java.util.Map;

import java.util.Properties;

  

public class User {

 

private String[] arr;

private List list;

private Map map;

private Properties properties;

 

publicvoid setArr(String[] arr) {

this.arr = arr;

}

 

publicvoid setList(List list) {

this.list = list;

}

 

publicvoid setMap(Map map) {

this.map = map;

}

 

publicvoid setProperties(Properties properties) {

this.properties = properties;

}

 

publicvoid print() {

System.out.println(Arrays.toString(arr)+"\n"+list+"\n"+map+"\n"+properties);

}

 

}

  

  

  

2)在配置文件中注入属性

  

applicationContext.xml:

  

 

<bean id="person2" class="cn.bdqn.ioc.Person">

<property name="arrs">

<list>

<value>啊1value>

<value>啊2value>

<value>啊3value>

list>

property>

 

<property name="list">

<list>

<value>小奥value>

<value>小金value>

<value>小普value>

list>

property>

 

<property name="map">

<map>

<entry key="aa" value="lucy">entry>

<entry key="bb" value="mary">entry>

<entry key="cc" value="tom">entry>

map>

property>

 

<property name="properties">

<props>

<prop key="driverclass">com.mysql.jdbc.Driverprop>

<prop key="username">rootprop>

props>

property>

bean>

  

  

  

3)编写一个测试类

  

TestProperty.java:

  

package com.siwuxie095.property;

  

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

  

public class Userlist {

public static void main(String[] args) {

ApplicationContext path=new ClassPathXmlApplicationContext("applicationContext.xml");

User user=  (User) path.getBean("user");

user.add();

}

 

}

你可能感兴趣的:(笔记)