Spring framework: Spring container details

Source: Internet
Author: User

Spring framework: Spring container details
Spring container

Spring containers can help you manage all Bean objects. The terminology is IoC control inversion. In traditional programs, objects are generated by developers. In the control reversal, all object generation is handed over to the framework. This reduces program dependencies.


The Bean lifecycle in Spring is as follows:

Instantiation. Spring instantiates a Bean using the new keyword. JavaBean has default constructor, so no constructor is required. Enter attributes. Spring fills in the corresponding attributes by calling the setXXX method in Bean Based on the configurations in the xml file. Event Notification. Spring checks whether BeanNameAware, BeanFactoryAware, ApplicationContextAware, BeanPostProcessor, and InitializingBean interfaces are implemented in sequence. If so, they are called in sequence. . The application can use this Bean normally. Destroy. If Bean implements the DisposableBean interface, it will call its destroy method.


The syntax for defining a Bean is as follows.

   
  
 


You only need to use applicationContext. getBean ("apple") to obtain this Bean.


Scope. In Spring, you can also specify the Bean scope, that is, the Bean life, as long as you set the scope attribute of the bean tag. Spring provides the following lifecycles: singleton has only one application, and each prototype call generates a new one. Each HTTP request corresponds to one bean, each session HTTP session corresponds to one bean, and each global-session portlet context session corresponds to one bean. The default life cycle is singleton.


Initialization and destruction. First, add the init-method and destroy-method attributes to the bean tag in xml to specify the initialization and destruction methods. The second method is to initialize and destroy implements InitializingBean or the DisposableBean interface in the bean java class. Method 3: add the default-init-method and default-destroy-method attributes to the beans label in xml.


In addition, remote beans can be configured through RPC or JMS. The following chapters are described.


External Configuration

Sometimes some key attributes, such as the database password, need to be placed outside the xml file for later modification. Spring can solve this problem in two ways: attribute placeholders and attribute rewriting. In addition, the AutoConfig provided by Alibaba has a better configuration management mechanism, which will be detailed later.


For placeholders, first load the attribute file:

 Or
 


Reference the variables in the property:

   
    
  
   
    
Jdbc: mysql: // localhost/test  
    
     
Com. mysql. jdbc. Driver
    
   
   In addition to OVERRIDE, there are FALLBACK and NEVER.
  
 


Overwrite the xml configuration. Load the properties file and overwrite the existing xml configuration. See the following example:

   
  
   jdbc:oracle://xxx
    
  
   xxx
  
 
 


Content of the db. properties file:

dataSource.url=jdbc:mysql://xxxdataSource.username=yyy


Encryption configuration value. Load the encrypted properties file:

   
    
  
 
   
  
 
   
  
 


Session

Generally, sessions in Spring do not need to be operated directly, but save the data to scope = Session in the Bean container.

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.