Spring Framework: Spring container Specific explanation

Source: Internet
Author: User

Spring container

The spring container can help you manage all your bean objects. Jargon is called IOC control inversion. In a traditional program. The generation of objects is completed by the developer. In control reversal, the object's generation is all given to the frame. This advantage is to reduce the dependence of the program.


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

    • instantiated.

      Spring instantiates a bean through Newkeyword, and JavaBean has a default constructor, so there is no need to provide a construction 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 is able to use the bean normally.
    • Destroyed. Suppose the bean implements the Disposablebean interface. The 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 can only be obtained with Applicationcontext.getbean ("Apple").


Scope.

You can also specify the scope of the bean in spring, which is the lifetime of the bean. Just set the scope property on the bean label. 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 corresponding Bean, Global-session each Portlet context session with the corresponding bean.

The default life cycle is singleton.


Initialize and destroy. The first method, the bean tag in the XML, adds the Init-method and Destroy-method properties to specify the initialization and destruction methods.

Another method is to initialize and destroy implements Initializingbean or Disposablebean interfaces in the Bean's Java class. The third method. Add 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 password, need to be placed outside the XML file for easy change.

Spring solves a problem in two ways. One is a property placeholder and one is a property override. In addition the AutoConfig provided by Ali has a better configuration management mechanism. will be described in detail later.


About 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.

Loads the properties file and overwrites the existing XML configuration.

Take a look at the following examples:

<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 need to be directly manipulated, but instead saves the data to the scope=session in the Bean container.

Spring Framework: Spring container Specific explanation

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.