Spring Road (14) - bean life cycle

What is the life cycle

The so-called life-cycle of an object (including ordinary objects, Spring containers, Spring common bean) from survival to the whole process of destruction.

What is the scope

The lifetime of the bean created by the Spring container, called the scope of the bean, bean that is the role of survival.

Common scopes four weeks:

  1. singletonScope, create a bean instance for a bean definition only, the entire life cycle can be used in this instance the Spring container. Note that this is also the default bean scope. We demonstrate in front of the bean are in this category.
  2. prototypeScope, which is similar to java objects used to create the new, each time removed from the container, creates a new bean.
  3. requestScope, every Web request will create a new bean instance, this scope can only be used in Web applications.
  4. sessionScope, create a new bean instance for different sessions, this scope can only be used in Web applications.

Simple to understand, singletonwhen a scope, no matter how many times removed from the container, is an object that is retrieved, the object has been alive, knowing the container is closed; prototypewhen the scope, each removed from the container, is out a new bean . The requestscope and sessionscope we need to understand the concept of web application and a request for a session.

Four kinds of concrete syntax scope

In the case of xml, using scope="xxx"the specified scope of the bean

<bean id="singer" class="org.maoge.Singer" scope="singleton"><bean>

In the case of annotations, use @Scope ( "xxx") specify the scope of the bean

@Component
@Scope("singleton")
public class Singer{
}

In JavaConfig, also specify the scope of the bean using @Scope ( "xxx")

Lazy Initialization and its role

Sometimes we have a lot of procedures bean, some bean start more slowly (such as the need to access the database, you need to access the network load resources, the need to access local files to load information).

These slow start of the bean, it is possible when the program has just started, or start is not used for some time. We have the bean delayed start to increase the speed of the program started.

I had encountered in the project, some bean is a bean netty related network module, loading speed is very slow and very wasteful system performance, and in fact there might not be used (some customers will use, and some customers It will not be used), if loading up on these bean, will undoubtedly slow down the system boot speed, delay initial configuration, the situation greatly improved.

Note By default, Spring bean container is created in the start-up phase, lazy initialization is the real bean to be created when needed .

When using the xml configuration bean, delay initialization syntax is as follows:

<bean id="singer" class="org.maoge.Singer" lazy-init="true"></bean>

When using annotation configuration bean, lazy initialization syntax is as follows:

@Component
@Lazy(true)
public class Singer{
}

In JavaConfig, is used @Lazy (true) arranged to delay the initialization bean

Specify the bean has the load order

Sometimes you need to specify the order to load the bean, such as a bean has associated database access logic and completed the initialization of the database interface, you need to call another bean bean access the database, then we need to ensure that the first load over a bean, and then load the first two bean.

At this point it can be so sad yo depends-on grammar, bean2 depends-on bean1 represent bean2 dependent on bean1, so in this case the container will be loaded bean1.

xml the specific syntax is:

<bean id="bean2" class="org.maoge.test.Bean2" depends-on="bean1"></bean>

The same can be use @DependsOnto indicate the same note.

bean creation and destruction callbacks

If a bean jdbc encapsulates data access logic, then we should inject url, user, password and the like in its properties, to initialize a database connection, a database connection and closes before the destruction bean.

At this point we need to use bean create a callback method after the success, the callback method before the destruction of the bean.

In xml, the specific syntax is as follows:

<bean id="datasource" class="org.maoge.test.db" init-method="initDatasource" destroy-method="destroyDataSource"></bean>

When using annotations, the following syntax:

public class DataSource{
  @PostConstruct//bean创建成功后回调该方法
  public void init throws Exception(){
  }
  @PreDestroy//bean销毁之前回调该方法
  public void destroy throws Exception(){
  }
}

to sum up

Lifecycle Management ordinary java object, is created by our own, and is responsible for the garbage collector to recover.

Spring bean object framework, the entire life cycle is managed, so if you want to set, manage their life cycle, you need to use Spring's rules by Spring.

发布了327 篇原创文章 · 获赞 238 · 访问量 52万+

Guess you like

Origin blog.csdn.net/woshisangsang/article/details/104081674