1.对象属性设置
String、list、set、map、properties
applicationContext.xml
<?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="hello" class="com.spring.test.Hello">
<property name="username">
<value>wayne</value>
</property>
<property name="list">
<list>
<value>one</value>
<value>two</value>
</list>
</property>
<property name="set">
<set>
<value>one</value>
<value>two</value>
</set>
</property>
<property name="map">
<map>
<entry>
<key>
<value>title</value>
</key>
<value>依赖注入</value>
</entry>
<entry>
<key>
<value>id</value>
</key>
<value>1</value>
</entry>
</map>
</property>
<property name="props">
<props>
<prop key="gmail">wxiong6@gmail.com</prop>
<prop key="qq">wxiong6@qq.com</prop>
</props>
</property>
<constructor-arg value="1" index="0"></constructor-arg>
<constructor-arg value="hello" index="1"></constructor-arg>
</bean>
</beans>
2.java对象
package com.spring.test;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Hello {
private String username;
private List<?> list;
private Set set;
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
private Map map;
private Properties props;
public List<?> getList() {
return list;
}
public void setList(List<?> list) {
this.list = list;
}
public Hello(int index, String name) {
System.out.println("index"+index+";name:"+name+";");
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void doHello(){
System.out.println("执行doHello()");
}
}
3.对象调用
对这几种类型的调用方法
package com.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class HelloTest {
@Test
public void testdoHello(){
String [] str = {"applicationContext.xml","applicationContext-list.xml"};
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(str);
Hello hello = (Hello) applicationContext.getBean("hello");
hello.doHello();
System.out.println(hello.getUsername());
//list 遍历
System.out.println("list...");
List<?> list = hello.getList();
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
//set 遍历
System.out.println("set....");
Set<?> set = hello.getSet();
Iterator<?> it = set.iterator();
while (it.hasNext()) {
String str1 = (String) it.next();
System.out.println(str1);
}
//map
System.out.println("map......");
Map<?, ?> map = hello.getMap();
Set<?> keys = map.keySet();
Iterator<?> mit = keys.iterator();
while (mit.hasNext()) {
String key = (String) mit.next();
String value = (String) map.get(key);
System.out.println("key:"+key+",value:"+value);
}
System.out.println("properties...");
Properties props = hello.getProps();
System.out.println(props.getProperty("qq"));
System.out.println(props.getProperty("gmail"));
}
}