What is Bean?
Bean is the initialization, assembly, and management object of Spring containers. It is mainly the <bean> element in an XML file that needs to be configured during Spring IoC development.
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans
Xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.0.xsd>
<Bean id = "User" class = "com. spring. user. User" init-method = "init" destroy-method = "destroy">
<Property name = "username">
<Value> zhulixx </value>
</Property>
<Property name = "age">
<Value> 50 </value>
</Property>
</Bean>
</Beans>
Bean element table:
Attribute Name Description
Unique id of the id Bean
Class Name, such as com. spring. user. User.
Name alias
Whether singleton is singleton mode. The default value is true.
Autowire attribute Assembly Method
Scope Bean scope, singleton, prototype. Each HTTP request generates a new session.
Init-method The Name Of The method called by default Initialization
Destroy-method: name of the method automatically destroyed by default
Abstract: An Instance cannot be created if it is abstract.
The parent class id or alias of the parent class, that is, the parent class id or name attribute.
Instance
Assign the default value to the class. A value can be assigned only when setUsername is required in the called class. The following <property name = "username"> assigns the default value to the username variable in the User class.
[Html]
<Bean id = "User" class = "com. spring. user. User" init-method = "init" destroy-method = "destroy">
<Property name = "username">
<Value> zhulixx </value>
</Property>
<Property name = "age">
<Value> 50 </value>
</Property>
</Bean>
[Java]
// Set value in the User class
Public void setUsername (String username ){
This. username = username;
}
Public void setAge (int age ){
This. age = age;
}
The initialization method and the method for destruction after the end. Mainly the init-method and destroy-method Methods
[Html]
<Bean id = "User" class = "com. spring. user. User" init-method = "init" destroy-method = "destroy">
<Property name = "username">
<Value> zhulixx </value>
</Property>
<Property name = "age">
<Value> 50 </value>
</Property>
</Bean>
[Java]
// The java code contains specific methods for initialization and destruction.
Public void init (){
System. out. printf ("init a class ");
}
Public void destroy (){
System. out. print ("destory a class ");
}
By default, a method that assigns an object a value to a variable in the object
[Html]
<Property name = "impObj" ref = "imp">