scope=“singleton” 单例的bean 是在spring容器启动时创建
scope=“prototype” 原型bean 在获取bean时才会创建
对象创建完成后做赋值的工作
初始化 – 对象赋值操作完成后 资源加载
spring容器关闭时 资源释放 只会帮我们销毁单例的bean 原型bean不归他管
bean的作用域 – 默认singleton 单例 全局只会创建一个当前类的对象
scope=“prototype” 原型bean 在获取bean时才会创建
文件一旦启动,就会去创建对象
// 文件一旦启动,就会去创建对象
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
在单例模式下,两次getBean容器智慧创建一个对象
// 只会创建一个对象
Student student1 = (Student)context.getBean("student");
Student student2 = (Student)context.getBean("student");
System.out.println(student1==student2);
控制台输出
Student()无参构造被执行! setUserName() setPassword() interface afterPropertiesSet() true在ProtoType模式下,每一次getBean都会创建一次对象
// 文件一旦启动,就会去创建对象
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student1 = (Student)context.getBean("student");
Student student2 = (Student)context.getBean("student");
System.out.println(student1==student2);
控制台输出
Student()无参构造被执行! setUserName() setPassword() interface afterPropertiesSet() Student()无参构造被执行! setUserName() setPassword() interface afterPropertiesSet()



