Spring note 1--spring origin and its core technology

Source: Internet
Author: User

the role of spring

When we use a technology, we need to think about why we should use this technology. And why should we use spring? From the three frameworks above the surface SSH, struts is responsible for the separation of the MVC responsibilities and provides functions such as control forwarding, data validation, and type conversion for the Web layer. Hibernate is responsible for abstracting the two-dimensional data tables into entity classes, allowing us to move from data-oriented to entity-oriented development, or object-oriented development. And what is spring doing? Spring seems to have done nothing, but it seems to be missing it. What exactly does spring do? We first look at the origins of spring.

When it comes to spring technology, you have to mention JavaBean technology, and JavaBean component technology is an important specification for Java technology. When Java was just released, a large number of programmers were attracted by the powerful, unique features of the applet, which were capable of producing dazzling animations with rich client features, but were left to javabean this component technology. JavaBean is able to define a software component model that enables Java objects to be reused and to build complex systems. However, with the development of technology, the submission of EJB component technology, because of the huge and complex EJB technology, so many Java programmers feel very uncomfortable, so Rod Johnson in his book expert One-on-one EE Development and The spring framework is presented in design, and the ultimate goal of spring is to simplify Java development .

Oh, the original spring is to simplify the development of Java, Ah, that spring has adopted what principles to achieve it?

Spring Four principles

1. Lightweight and minimally invasive programming based on Pojo.

2, through the dependency injection and interface-oriented implementation of loose coupling.

3. Declarative programming based on facets and conventions.

4. Reduce boilerplate code by cutting slices and templates.

Core technologies of Spring

What is the core technology of spring when it comes to the principles of spring? How do these technologies achieve the goal of spring: simplifying Java development?

The First core technology: Dependency Injection (Dependency injection, referred to as DI), is a technology that separates behavior from dependencies, simply, it allows the developer to define a method function that relies on various external interactions, You do not need to encode how to obtain instances of these external interactions. The time to create an object in spring is given to the container for processing, without requiring the class to create the object internally. Dependency injection allows you to save loosely coupled software that works together. Therefore, in the spring container, you do not need to instantiate a specific object within the class, simply associate the corresponding interface, and then inject the object of the implementation class of the interface through the spring container.

Here's a concrete example: Knight for Knights, Braveknight for heroic Knights, Knights for Adventure (Embarkonquest)

Normal code:

 Packagecom.springinaction.knights; Public classBraveknightImplementsKnight {PrivateSlaydragonquest Quest;//instantiate a specific object    Publicbraveknight () { This. Quest =Newslaydragonquest (); }    Public voidEmbarkonquest ()throwsquestexception {quest.embark (); }  Public Static voidMain (string[] args) {Braveknight Knight=Newbraveknight ();   Knight.embarkonquest (); }}

Spring Code:

 Package com.springinaction.knights;  Public class Implements Knight {  private Quest Quest;    // Quest for Interface    Public braveknight (Quest Quest) {    this. Quest = Quest;         }     Public void throws questexception {    quest.embark ();  }}

Braveknight simply relies on the quest interface and does not rely on specific implementation classes to achieve loosely coupled purposes.

Configuration file:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd ">  <BeanID= "Knight"class= "Com.springinaction.knights.BraveKnight" >    <Constructor-argref= "Quest" /> <!--Constructor Injection -        </Bean>  <BeanID= "Quest"class= "Com.springinaction.knights.SlayDragonQuest"/> </Beans>

To run the Main method:

 Packagecom.springinaction.knights;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classKnightmain { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Knights.xml"); Knight Knight= (Knight) context.getbean ("Knight");   Knight.embarkonquest (); }}}

The Second core technology: plane-oriented programming (Aspect oriented programming, referred to as AOP). AOP is a reusable component that classifies functionality throughout the application. These tasks, which are distributed throughout, include functional components such as logs, things management, and security, which are distributed over a number of common functional modules, but if implemented in other components, can lead to problems such as code duplication and code confusion. AOP is a combination of some common functions, and we can think of these functions as a facet that covers many components, so it's called aspect-oriented programming.

: Common transaction Management, log modules, security modes are distributed across modules, and they are components of a slice.

Here is an example to illustrate the AOP, bard Minstrel people need to record the story of the warrior, so define a plane, before the Warriors to praise the Warriors, after the Warriors completed the expedition to celebrate the warrior.

Code:

 Packagecom.springinaction.knights;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classKnightmain { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Knights.xml"); Knight Knight= (Knight) context.getbean ("Knight");   Knight.embarkonquest (); }}}

Normal code:

 Packagecom.springinaction.knights; Public classBraveknightImplementsKnight {PrivateQuest Quest; PrivateMinstrel Minstrel;  Publicdamselrescuingknight (Quest Quest, Minstrel Minstrel) {Quest= This. Quest; Minstrel= This. Minstrel; }     Public voidEmbarkonquest ()throwsquestexception {minstrel.singbeforequest (); //here the Braveknight class must rely on minstrel, too coupledQuest.embark ();    Minstrel.singafterquest (); }}
Spring can implement AOP in a simple configuration, with the following configuration files:
<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spri Ng-aop-3.0.xsd ">  <BeanID= "Knight"class= "Com.springinaction.knights.BraveKnight" >    <Constructor-argref= "Quest" />        </Bean>  <BeanID= "Quest"class= "Com.springinaction.knights.SlayDragonQuest" />   <BeanID= "Minstrel"class= "Com.springinaction.knights.Minstrel" />     <Aop:config>    <Aop:aspectref= "Minstrel">      <Aop:pointcutID= "Embark"expression= "Execution (* *.embarkonquest (..))" /> <!--define a facet as embark, execute Embarkonquest method, <aop:before pointcut-ref = "Embark" method= "singbeforeq Uest "/> <!--statement Pre-notification -      <Aop:afterPointcut-ref= "Embark"Method= "Singafterquest"/>     <!--declaring a post-notification -    </Aop:aspect>  </Aop:config>  </Beans>

Operation Result:

Third technique: templates . Spring provides a number of common templates that eliminate style code, allowing you to focus more on your own responsibilities.

Instance--JDBC code:

Code after using the spring template:

Summary

In a word, Spring provides technology such as dependency injection, aspect-oriented programming, templates, and interface-oriented programming to enable us to use the Pojo class to achieve EJB-like results, simplifying our Java code and paying more attention to our core tasks.

Spring note 1--spring origin and its core technology

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.