依赖注入的几种数据类型的注入方式
很简单 没啥说的 看代码就是了
package com.wow.StudentInfo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.wow.StudentInfo;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class StudentInfo {
private String name;
private String [] names;
private List<String> IDs;
private Map<String, String> teacherName;
private Set<String> hobbies;
private String wife;
private Properties pro;
private Address add;
public void setName(String name) {
this.name = name;
System.out.println("name="+name);
}
public void setNames(String[] names) {
this.names = names;
for (int i = 0; i < names.length; i++) {
System.out.println("names="+names[i]);
}
}
public void setIDs(List<String> iDs) {
this.IDs = iDs;
System.out.println("IDs="+IDs);
}
public void setTeacherName(Map<String, String> teacherName) {
this.teacherName = teacherName;
System.out.println("teacherName="+teacherName);
}
public void setHobbies(Set<String> hobbies) {
this.hobbies = hobbies;
}
public void setAdd(Address add) {
this.add = add;
System.out.println("add="+add.getAddress());
}
public void setWife(String wife) {
this.wife = wife;
System.out.println("wife="+wife);
}
public void setPro(Properties pro) {
this.pro = pro;
System.out.println("pro="+pro);
}
public void printInfo(){
System.out.println("Info Running");
}
}
重点来了 beans.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="add" class="com.wow.StudentInfo.Address">
<property name="address" value="暴风城AH"></property>
</bean>
<bean id="stu" class="com.wow.StudentInfo.StudentInfo">
<property name="name" value="老佛爷小地狱咆哮"></property>
<property name="add" ref="add"></property>
<property name="names">
<array>
<value>吃书编年史1</value>
<value>吃书编年史2</value>
<value>吃书编年史3</value>
<value>吃书编年史4</value>
<value>吃书编年史5</value>
</array>
</property>
<property name="IDs">
<list>
<value>国王的护卫剑1</value>
<value>国王的护卫剑2</value>
<value>国王的护卫剑3</value>
<value>国王的护卫剑4</value>
<value>国王的护卫剑5</value>
</list>
</property>
<property name="teacherName">
<map>
<entry key="洛萨1" value="左手剑"></entry>
<entry>
<key>
<value>洛萨2</value>
</key>
<value>右手剑</value>
</entry>
</map>
</property>
<property name="hobbies">
<set>
<value>wow1</value>
<value>wow2</value>
<value>wow3</value>
<value>wow4</value>
<value>wow5</value>
</set>
</property>
<property name="wife"><null></null></property>//设置空值
<property name="pro">
<props>
<prop key="技能">嘲讽</prop>
<prop key="cd">1s</prop>
<prop key="伤害">-999</prop>
</props>
</property>
</bean>
</beans>
package com.wow.StudentInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StuTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
StudentInfo stu = (StudentInfo)context.getBean("stu");
}
}
测试结果
name=老佛爷小地狱咆哮
add=暴风城AH
names=吃书编年史1
names=吃书编年史2
names=吃书编年史3
names=吃书编年史4
names=吃书编年史5
IDs=[国王的护卫剑1, 国王的护卫剑2, 国王的护卫剑3, 国王的护卫剑4, 国王的护卫剑5]
teacherName={洛萨1=左手剑, 洛萨2=右手剑}
wife=null
pro={伤害=-999, 技能=嘲讽, cd=1s}