Framework technology----Spring

Source: Internet
Author: User

Spring is a lightweight solution for enterprise application development, an open source IOC and AOP framework that can greatly improve the development efficiency of enterprise applications by using spring. It includes the following advantages: 1, low-intrusive design, code pollution is very low; 2, the spring container reduces the complexity of business object substitution, improves the decoupling between components;3, Spring's AOP support allows for some common tasks such as: Security, transactions, Log and so on centralized processing, thus provides the better reuse; 4. Spring's ORM and DAO provide good integration with third-party persistence frameworks and simplify the underlying database access; 5, spring's high level of exploitation, does not force the application to rely entirely on spring, Developers are free to select part or all of the spring framework.

When using the spring framework, you must use the Spring Core Container (sping container). All of spring's modules are built on top of the core container, and its main function is to convert the <bean> elements in the configuration file into runtime objects, and then inject them into the corresponding object according to the description in the configuration file, the process is 1, the configuration file is read into the container; 2. The description of the object in the configuration file is converted to a Java object through the reflection mechanism of Java, and the Java objects are injected into their matching objects according to the definitions in the configuration file 3.

Below, we use a simple spring configuration file to briefly describe his scope.

<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:p= "http://www.springframework.org/schema/p"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
<!--define a bean component that needs to be managed by the spring container, there are 2 parameters that are required--
<!--The bean objects generated by the spring container have different scopes and have the following 5 types:

Scope= "Singleton", the default configuration, is valid in the case of container management.

Scope= "Prototype" is also called a prototype, its life cycle is: from the container to create, until garbage collection is valid. Change
In other words, the spring container is only responsible for creating, not responsible for the management of its life cycle.

Scope= "Request", also known as the scope of requests, is within the range of the same request.

Scope= "Session", also known as session scope, is within the scope of the same session.

Scope= "Global Session" is also called the global sessions scope, it applies to the portal site, and the session is synchronized between the servers
-

<bean id= "user" class= "Com.lovo.beans.UserBean" scope= "singleton" >
<!--spring containers allow us to initialize certain properties when creating an object by using Set ()
<property name= "UserName" value= "Zhang San" ></property>
<property name= "Password" value= "123456" ></property>-


<!--the spring container allows us to initialize certain properties by constructor () when creating an object--
<constructor-arg index= "0" >
<value> Zhang San </value>
</constructor-arg>

<constructor-arg index= "1" >
<value>123456</value>
</constructor-arg>

</bean>

<!--Byname,bytype are injected using setting-
<!--ByName is automatically assembled, the spring container is found based on the name of the property that needs to be assembled in the caller.
See if there is an instance of the corresponding bean component, and if it exists, it is automatically assembled and fails if it does not exist.
But no exception is thrown, and a null pointer exception is thrown only when the program is running--

<!--when Bytype is automatically assembled, the spring container is located according to the type of property that the caller needs to assemble.
See if there is a corresponding instance of the same type of bean component, if the type is an interface, then look for the implementation class of the interface,
If there is an automatic assembly, if it does not exist on the assembly is unsuccessful, but does not throw any exceptions, the code throws when the runtime
Null pointer. When there are more than 1 instances of the qualifying instance, it does not know which one is assembled and throws: Unsatisfieddependencyexception--

<!--constructor structure assembly, the container is found based on the property type in the constructor, if the type is an interface,
Then go to the implementation class of the interface, if it exists on the automatic assembly, if it does not exist on the assembly is not successful, but will not throw any exception, run, the code will throw
Null pointer. When there are more than 1 instances of the condition, it does not know which one is assembled and throws:unsatisfieddependencyexception-->

<!--manual assembly > Automatic assembly, when both exist, manual assembly, automatic assembly Although simple, direct, rough, but the structure is not clear, maintenance is poor, we recommend the use of manual assembly--
<bean id= "Userserviceimpl" class= "Com.lovo.service.impl.UserServiceImpl" autowire= "ByName" >
<!--manual assembly, need to provide setting (); -
<property name= "Userdaoimpl" ref= "Userdaoimpl" ></property>

<!--manual assembly, construction injection, no need to define index=0 when the parameter is only 1 bits
<constructor-arg>
<ref bean= "Userdaoimpl"/>
</constructor-arg>

</bean>

<bean id= "Userdaoimpl" class= "Com.lovo.dao.impl.UserDaoImpl" ></bean>

<import resource= "Beans.xml" ></import>

Framework technology----Spring

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.