On Spring (I.)

Source: Internet
Author: User

First, Spring introduction

Spring is a lightweight framework that has a small number of code intrusions and is a combination of many excellent design patterns (factories, proxies, templates, policies).

Features: 1, convenient decoupling, simplified development through the IOC container provided by spring, we can control the dependencies between objects by spring and avoid excessive program coupling caused by hard coding.  With spring, users no longer have to write code for these very low-level requirements, such as single-instance pattern classes, attribute file parsing, and can focus more on upper-tier applications.  2. AOP programming support for aspect-oriented programming is facilitated by the AOP features provided by spring, and many features that are not easily implemented with traditional OOP can be easily coped with by AOP.  3. Declarative transaction support in spring, we can free ourselves from the tedious transaction management code, and flexibly manage the transaction through declarative means to improve the efficiency and quality of development. 4, convenient program testing can be used in a non-container-dependent programming method for almost all of the testing work, in spring, testing is no longer expensive operation, but can be done at hand.  For example, Spring supports JUNIT4, which can be easily tested with annotations for spring programs. 5, convenient integration of a variety of excellent framework spring does not exclude a variety of excellent open source framework, instead, spring can reduce the use of various frameworks difficult, spring provides a variety of excellent frameworks (such as Struts,hibernate, Hessian, Quartz) and other direct support. Second, start the Spring 1, environment construction

A. Import core jar packages with third-party dependent jar packages.

B, configuration files

Spring's configuration file has only one: applicationcontext.xml, place the location arbitrarily.

2. Initial knowledge

A, Core API

ApplicationContext factory class.

(1) Classpathxmlapplicationcontext (non-web environment)

(2) Webxmlapplicationcontext (under Web environment)

The factory class is available through the two interfaces above, and the factory class is a heavyweight resource, and an application should only create one. In a project that uses spring, all the user-written classes should be produced through the ApplicationContext factory class.

B, code example

Applicationcontext.xml configuration:

<id= "First"  class= "Xxxxx.xxxx.First"/>

Java code:

New Classpathxmlapplicationcontext ("/xxxx/applicationcontext.xml"= (first) Ctx.getbean ("first");

Explain:

The first line fetches the spring configuration file through Classpathxmlapplicationcontext to get the ApplicationContext factory class, which is the path to the spring configuration file.

The second line is produced by the ApplicationContext factory class to produce objects that have been configured in the configuration file (based on the id attribute).

Iii. IOC and Di (control inversion and dependency injection)

Control inversion (inversion of the English abbreviation for IOC) is an important object-oriented programming principle to reduce the coupling problem of computer programs, and is also the core of the lightweight spring framework. Control reversals are generally divided into two types, dependency injection (Dependency injection, short di), and dependent lookup (Dependency lookup). The application of dependency injection is quite extensive.

Simply put, the control of assigning a member variable to the XML file is transferred from the code. The benefit of this is decoupling and the principle is factory + reflective.

1. Injection of the JDK type member variable

Basic type +string in a, 8

<BeanID= "User"class= "xxxxx." User ">    < Propertyname= "Name">        <value>Tom</value>    </ Property>    < Propertyname= "Password">        <value>123456</>    </ Property>    < Propertyname= "Age">        <value>20</value>    </ Property></Bean>

<name= "password"  value= "123123"/>

Both of them can be written.

B, Array

< Propertyname= "emails">    <List>        <value>[Email protected]</value>        <value>[Email protected]</value>        <value>[Email protected]</value>    </List></ Property>

C, Set Set

<Set>    <value>123123</value>    <value>123123</value>    <value>123123</value> </Set>the label inside the//set tag, depending on the type of data stored, is not necessarily a value

D, List collection

<List>    <value>123123</value>    <Set>        <value>123123</value>    <refLocal=""/></List>//and set in the same vein, the internal label depends on the data type stored

E, Map Collection

<Map>    <entry>        <Key><value>Tom</value></Key>        <value>123123</value>    </entry></Map>//A entry node represents a key-value pair. 

F, properties type, equivalent to map<stirng,string>

< Props >    <  key= "Key1">value1</prop>    <  key= "Key2">value2</prop> </ Props >

2. User-defined type

First, create the corresponding class <bean id= "" class= ""/> In the spring configuration file. And then inject it.

// Java class, sample code  Public class userservice{    private  Userdao Userdao;    Get, set}
//spring configuration file<BeanID= "Userdao"class= "xxxx." Userdao "/><BeanID= "UserService"class= "xxxx." UserService ">    < Propertyname= "Userdao">        <refLocal= "Userdao">    </ Property></Bean>

User-defined types are injected with <ref local= "" > tags.

3. Construction injection (not commonly used)
<id= "User"  class= "xxxx." User>    <constructor-arg>         <  Value>xxxx</value>    </  Constructor-arg></beans>

Iv. Spring creates complex objects

  

1. Create complex objects with Factorybean

On Spring (I.)

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.