在spring中,创建对象不是new出来的。
spring中创建对象的步骤:
1.先创建bean类,一定要有get,set方法,和构造方法
例如
public class person {
String name;
int age;
String sex;
int id;
Car car;
2.创建一个spring bean configeration file(是一个xml文件)
3.在xml文件中写配置,对bean对象做初始化
配置的时候要指明id和class
4.创建ioc对象
ApplicationContext ioc=new ClassPathXmlApplicationContext("ioc.xml");
5.利用ioc对象,根据id获取bean对象
public void test() {
Object bean1=ioc.getBean("person01");
System.out.println(bean1);
}