Java Study Notes 04: Spring XML configuration file Bean

Source: Internet
Author: User

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">

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.