文章目录
一.从IOC容器中获取Bean
1.方法一:根据类型获取,此时容器中同类型的bean唯一
Student s1=ac.getBean(Student.class); //根据对象的类型获取对象 要求配置文件中该类型的bean唯一
2.方法二:同时指定bean的id和类型
Student s2 = ac.getBean("student", Student.class); //根据id获取bean
二.bean属性赋值的三种方式
Person类:
package com.bean;
public class Person {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
}
Teacher类:
package com.school;
public class Teacher {
private Integer tid;
private String 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;
}
public Teacher(Integer tid, String tname) {
super();
this.tid = tid;
this.tname = tname;
}
public Teacher() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Teacher [tid=" + tid + ", tname=" + tname + "]";
}
}
1通过bean的setXxx()方法赋值
name属性值与Xxx对应 而不是与字段名对应
<bean id="student" class="com.school.Student">
<pr