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