Spring is used to change the scope of the annotation @Scope and life cycle related annotations @PreDestroy@PostConstruct

@Scope annotation for changing scope

  他们的作用就和在bean标签中使用scope属性实现的功能是一样的
  @Scope
      作用:用于指定bean的作用范围
      属性:
          value:指定范围的取值。常用取值:singleton prototype(默认单例

default singleton

insert image description here

multiple cases

insert image description here
insert image description here

Related to the life cycle (understanding) @PreDestroy@PostConstruct

  @PreDestroy
  作用:用于指定销毁方法
  @PostConstruct
  作用:用于指定初始化方法
  他们的作用就和在bean标签中使用init-method和destroy-method的作用一样

Add two methods to the business layer implementation class.
insert image description here
If you want to see the execution method of destroy, you should pay attention.
You can’t write it like this. You can only use the parent’s method without close.
insert image description here
It should be changed to

ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

Note that it is not responsible to destroy spring with multiple instances and multiple instances here.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_42727032/article/details/104603942