🍓个人主页:个人主页
🍒系列专栏:SSM框架
目录
1.为集合类型属性赋值
①为List集合类型属性赋值
②为Map集合类型属性赋值
2.p命名空间
3.引入外部属性文件
private List students;
public List getStudents() {
return students;
}
public void setStudents(List students) {
this.students = students;
} 方法1:配置bean: 若为Set集合类型属性赋值,只需要将其中的list标签改为set标签即可
吃饭 睡觉 打豆豆 吃饭 睡觉 打豆豆 吃饭 睡觉 打豆豆
测试:
@org.junit.Testpublic void testHelloWorld(){ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");Clazz clazzTwo = ac.getBean("clazzTwo", Clazz.class);System.out.println(clazzTwo); 
方法2:配置一个集合类型的bean,需要使用util 的约束-
效果一样的:

创建教师类Teacher:
public class Teacher {private Integer tid;private String tname;public Teacher() {}public Teacher(Integer tid, String tname) {this.tid = tid;this.tname = tname;}public Integer getTid() {return tid;}public void setTid(Integer tid) {this.tid = tid;}public String getTname() {return tname;}public void setTname(String tname) {this.tname = tname;}@Overridepublic String toString() {return "Teacher{" +"tid=" + tid +", tname='" + tname + '\'' +'}';}
}
在Student类中添加以下代码: private Map teacherMap;
public Map getTeacherMap() {
return teacherMap;
}
public void setTeacherMap(Map teacherMap) {
this.teacherMap = teacherMap;
} 方法1:配置bean(引用集合类型的bean): 吃饭 睡觉 打豆豆
测试:
public void testHelloWorld(){ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");Student student = ac.getBean("student", Student.class);System.out.println(student);}

方法2:配置bean(util:map):
吃饭 睡觉 打豆豆
引入p命名空间后,可以通过以下方式为bean的各个属性赋值

①加入依赖
mysql
mysql-connector-java
8.0.16
com.alibaba
druid
1.0.31
②创建外部属性文件
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm? serverTimezone=UTC
jdbc.username=root
jdbc.password=root ③引入属性文件 ④配置bean
⑤测试 public void test(){
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring-datasource.xml");
DruidDataSource bean = ioc.getBean(DruidDataSource.class);
try {
System.out.println(bean.getConnection());
} catch (SQLException throwables) {
throwables.printStackTrace();
}}
