Spring framework bean scope, the life cycle

Spring beand scope

When set to singleton, a class corresponding to only one instance, when the application again to return to the same instance

can see two bean instance hashcode value is the same, to be described in this application is the same one instance
when the bean's scope to prototype, to run again, you can see, is to apply two different bean instances

Currently only a prototype scopes and learning singleton scope, after learning the rest of the wait

Spring bean lifecycle
1, defined xml configuration file of the process
2, the initialization of the bean container ioc, instantiate
3, a
4, ioc container destruction destroy all bean instances created by the bean container stop

Initialization and destruction are three ways
1, implement the interface, the InitializingBean, The DisposableBean
2, defined init-method and destroy-method in each of the bean
default default-init 3 and the default-destroy method defined in the xml configuration file

/*
 * public void defautinit() { System.out.println("Bean defaut Init." +
 * this.hashCode()); }
 * 
 * public void defautdestroy() { System.out.println("Bean defaut destroy." +
 * this.hashCode()); }
 */


/*
 * @Override public void destroy() throws Exception {
 * System.out.println("Bean destory."+this.hashCode()); }
 * 
 * @Override public void afterPropertiesSet() throws Exception {
 * System.out.println("Bean init."+this.hashCode()); }
 */

/*
 * public void start() { System.out.println("Bean start."+this.hashCode()); }
 * 
 * public void stop() { System.out.println("Bean stop."+this.hashCode()); }
 */

Initialization and destruction methods interface defines priority higher than bean method defined,
xml default method has the lowest priority, not even achieve bean, no error will, if realized the other two, will be covered
if set in a bean the init and destroy methods are needed to achieve a certain, otherwise it will fail to start

Mu class network https://www.imooc.com/video/3751

Guess you like

Origin www.cnblogs.com/wjune-0405/p/12148683.html