(C) the Spring Framework: spring IOC using

First, create a bean of three ways

Creating objects 1, using no-argument constructor way

After using tags bean spring profile, together with the id and class attributes, and other attributes, and no label.

<!--使用无参构造的方式创建对象-->
<bean id="userInfo" class="com.wedu.spring01.entity.UserInfo"/>

Note: Creating object id must be unique.

2, create an object using a static factory method (static method of a class object is created and stored in the spring container)

<!--利用静态工厂方法来创建对象-->
<bean id="cal" class="java.util.Calendar" factory-method="getInstance"/>

factory-method attribute: Specify a static method name.

3, using the factory method to create an object instance (use to create an object of a class and stores spring containers)

<!--使用实例工厂的方法创建对象-->
<bean id="date" class="java.util.Date"/>
<bean id="time" factory-bean="date" factory-method="clone"/>

 factory-bean attribute: Specifies the bean id.

Two, bean range of action

using the bean scope scope attribute set, scope of the following values:

  • singleton: Singleton (default), throughout the application, only create an instance of the bean.
  • prototype: Multi embodiment, the or each injection time acquired by the Spring application context, will create a new instance of the bean.
  • request: the request scope is acting in web application, in a Web application, a bean instance created for each request.
  • session: acting on the web application session scope, in the Web application, create a bean instance for each session.
  • global-session: role in cluster environments session scope (global session scope), when not a clustered environment, it is session
<!--使用scope属性指定bean的作用范围-->
<bean id="scope" class="com.wedu.spring01.entity.UserInfo" scope="singleton"/>

Three, bean object's life cycle

1, the initialization container

<!--指定初始化方法-->
<bean id="msg" class="com.wedu.spring01.entity.UserInfo" init-method="init" />

2, the destruction of the container

<!--指定销毁方法-->
<bean id="msg" class="com.wedu.spring01.entity.UserInfo" destroy-method="destroy" />
	

Note: destroy method is called when the container is closed sping, only scoped singleton, destruction methods to work.

Published 134 original articles · won praise 10 · views 7359

Guess you like

Origin blog.csdn.net/yu1755128147/article/details/103541408