对于简单数据类型(byte,char,short,int,float,double,long )或者String的注入,一般只需写入标签
[html]
[html]
或者p模式
如果需要注入的是集合(list,set,map,pros),那该怎么做呢?
如果集合的泛型是简单数据类型,比如 List
[html]
...
...
如果集合的泛型是引用类型,比如List
[java]
......
......
下面是一个具体例子:
省份类:
[java]
public class Province {
private String name; // 省份名称
private long population; // 人口
private List
private List
// 标准setter 和 getter 以及 toString方法省略
}
public class Province {
private String name; // 省份名称
private long population; // 人口
private List
private List
// 标准setter 和 getter 以及 toString方法省略
}
[java]
public class Official {
private String name;
private String title;
private String age;
// 省略setter 和 getter 以及 toString方法
}
public class Official {
private String name;
private String title;
private String age;
// 省略setter 和 getter 以及 toString方法
}
bean配置文件:
[html]
< xml version="1.0" encoding="UTF-8" >
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">
< xml version="1.0" encoding="UTF-8" >
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">
运行它:
[java]
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"SpringConfig.xml"});
Province siChuan = (Province) context.getBean("sichuan");
System.out.println(siChuan);
}
}
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"SpringConfig.xml"});
Province siChuan = (Province) context.getBean("sichuan");
System.out.println(siChuan);
}
}
打印输出:
[plain