Spring Framework: Spring container Details

Source: Internet
Author: User

Spring container

The spring container helps you manage all bean objects, which are called IOC control reversals. In traditional programs, the generation of objects is done by the developer. In control reversal, the generation of the objects is all given to the framework to complete. The benefit of this is to reduce the dependency of the program.


The Bean's life cycle in spring is as follows:

    • instantiated. Spring instantiates a bean with the new keyword, and JavaBean has a default constructor, so you don't need to provide a constructor parameter.
    • Fill in the properties. Spring fills in the corresponding properties by invoking the Setxxx method in the bean based on the configuration in the XML file.
    • Event notification. Spring checks to see if the bean implements the Beannameaware, Beanfactoryaware, Applicationcontextaware, Beanpostprocessor, Initializingbean interfaces , if any, call these interfaces in turn.
    • Use. The application can use the bean normally.
    • Destroyed. If the bean implements the Disposablebean interface, its destroy method is called.


The syntax for defining a bean is as follows.

<beans xmlns= "..." >  <bean id= "Apple" class= "Com.whitejava.Apple"/></beans>


This bean is available only through Applicationcontext.getbean ("Apple").


Scope. You can also specify the scope of the bean in spring, which is the lifetime of the bean, as long as the scope property of the bean label is set. The life cycle provided by spring is: Singleton the entire application has only one, prototype each invocation produces a new one, requests each HTTP request corresponding to a bean, session each HTTP session corresponds to a bean, Global-session each Portlet context session corresponds to a bean. The default life cycle is singleton.


Initialize and destroy. The first method, the bean tag in XML, adds the Init-method and Destroy-method properties to specify the initialization and destruction methods. The second method initializes and destroys the implements Initializingbean or Disposablebean interface in the Bean's Java class. The third method adds the Default-init-method and Default-destroy-method properties to the beans tag in the XML.


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


External configuration

Sometimes some key attributes, such as database passwords, need to be placed outside the XML file for subsequent modifications. Spring solves this problem in two ways: a property placeholder and a property rewrite. In addition the AutoConfig provided by Ali has a better configuration management mechanism, which will be described in detail later.


For placeholders, load the properties file first:

<context:property-placeholder location= "classpath:/db.properties"/> or <context:property-placeholder location= "File:///etc/db.properties"/>


Variables in the Reference property:

<bean id= "DataSource" class= "Org.springframework.jdbc.datbase.DriverManagerDataSource" >  <property Name= "Driverclassname" value= "${jdbc.driverclassname}"/>  <property name= "url" value= "${jdbc.url}/> </bean>


Overwrite the variables in the properties file:

<util:properties id= "defconf>  <prop key=" Jdbc.url ">jdbc:mysql://localhost/test</prop>  <prop key= "Jdbc.driverclassname" >com.mysql.jdbc.driver</prop></util:properties><context: Property-placeholder location= "classpath:/db.properties" properties-ref= "defconf" system-properties-mode= " Override "/> In addition to fallback, never.


Overwrite the XML configuration. Load the properties file and overwrite the existing XML configuration. Take a look at the following example:

<bean id= "DataSource" class= "xxx" >  <property name= "url" >jdbc:oracle://xxx</property>  <property name= "username" >xxx</property></bean><context:property-overrider location= " Classpath:/db.properties "/>


Contents of the Db.properties file:

Datasource.url=jdbc:mysql://xxxdatasource.username=yyy


Encrypt the configuration value. Load the encrypted properties file:

<bean id= "Environmentconfig" class= "Org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig" >  < Property name= "Algorithm" value= "Pbewithmd5anddes"/>  <property name= "Passwordenvname" value= "DB_ Encryption_pwd "/></bean><bean id=" Stringencrypter "class=" Org.jasypt.encryption.pbe.Environment ">  <property name= "Config-ref" ref= "Environmentconfig"/></bean><bean id= " Org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer ">  <constructor-arg ref=" Stringencrypter "/></bean>


Session

The session in spring generally does not require direct manipulation, but instead saves the data to the scope=session in the Bean container.

Spring Framework: Spring container Details

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.