Use SMM framework for developing enterprise applications ----- Spring Introduction namely Spring Ioc

Introduction to the Spring Framework

  Spring is an open source framework Spring is a lightweight rise in 2003 Java development framework, part of the philosophy set forth by the Rod Johnson in his book Expert One-On-One J2EE Development and Design and prototypes derived. It is designed to address the complexity of enterprise application development created. Spring uses basic JavaBean to do things previously only possible done by EJB. However, Spring's use is not limited to server-side development. Any Java application can benefit from the simplicity, testability and loose coupling from the perspective of Spring.  Briefly, Spring is a lightweight inversion of control (IoC) and the facing section (AOP) of the container frame.

 

  Spring basic framework mainly includes six modules: DAO, ORM, AOP, JEE, WEB, CORE

    

  

  DAO the Spring : the Spring provides operational support for the JDBC: JdbcTemplate template tools.

  ORM the Spring : the Spring can be integrated with the ORM framework. E.g. Spring integrate Hibernate framework, wherein HibernateDaoSupport Spring provides tools, Hibernate simplify operation.

  WEB the Spring : the Spring provides support for Struts, Springmvc, support for WEB development. At the same time Spring itself provides a MVC-based solutions.

  AOP the Spring : the Spring provides aspect-oriented programming, it can provide transaction management to a certain level, such as adding things in the Service control layer.

  JEE the Spring : the development of the J2EE specification support, such as EJB.

  Core the Spring : IOC container to provide the object creation and manipulation of objects dependent relationship.

Spring advantage

  1. facilitate decoupling, simplified (the high cohesion and low coupling) Development 

    Spring is a large plant (container), you can create all the objects and dependencies maintenance management to Spring 

    spring bean plant for generating

  2. AOP programming support 

    Spring provides aspect-oriented programming, you can easily implement a program permission to intercept, monitoring and other functions run

  3. Support declarative transactions 

    Only you need to complete the configuration management of the affairs, without the need for manual programming

  4. facilitate the testing procedure 

    Spring for Junit4 supported by Spring annotation convenient test program

  The easy integration of various excellent framework 

    Spring does not exclude a variety of excellent open source framework, which provides a variety of excellent internal framework: direct support (such as Struts, Hibernate, MyBatis, Quartz, etc.)

  6. reduce the difficulty of using the JavaEE API 

    Spring for some API JavaEE development is very difficult to use (JDBC, JavaMail, remote calls, etc.), provides packaging that greatly reduces the difficulty of these API application

Why use Spring

  Spring fundamental mission is to simplify the java development;

  In order to reduce the complexity of java development, Spring has adopted the following four key strategies:

    1. lightweight and minimally invasive POJO-based programming;

    2. The loose coupling and dependency injection oriented programming interface;

    3. Based section and declarative programming practices;

    4. By reducing the cut and style code templates;

Inversion of Control

  Inversion of Control, also referred dependency injection, one design concept of the object oriented programming, to reduce the coupling between the program code;

  IoC is not a technology, but an idea, an important rule of object-oriented programming that guides how we design a loosely coupled, better program. Traditional applications are created by our initiative within the class dependent objects, resulting in a high coupling between classes, it is difficult tests; Once you have IoC container to create and find dependent objects of control to the container by container implanting a combination of objects, between objects and the objects are loosely coupled, which would also facilitate the testing, which will help multiplexing function, more importantly, makes the whole architecture of the program has become very flexible.

  Function that has a dependency injection container, the container can create objects, IOC container is responsible for instantiating, location, configuration application objects and dependencies between these objects. Typically a new instance, the control by the programmer control, and "control reversal" means new working examples but do not help the programmer to do Spring container. In Spring BeanFactory is the actual representation of IOC container.

  In fact, IoC programming bring the biggest change is not from the code, but ideologically, "master-slave transposition" of change. The application was originally boss, to get what resources are to take the initiative, but in IoC / DI thought, the application becomes passive, and passive waiting IoC container to create and inject the resources it needs.

  IoC good indication of one of the objects oriented design rules - Hollywood rule: "Do not come to us, we are looking for you"; that is, to help find the appropriate objects by the IoC container object and dependency injection, rather than take the initiative to find the object.

Dependency Injection DI

   DI-Dependency Injection, namely "dependency injection" : dependent relationship between components at runtime is determined by the vessel, said the image, that dynamic will be injected by the container into a dependency component . Dependency injection is not intended to bring more functionality to software systems, but to enhance component reuse frequencies, and for the system to build a flexible, scalable platform. Via dependency injection mechanism, we need only a simple configuration, without any code can specify the target resources needed to complete their business logic without having to be concerned about the specific resources come from, who to achieve.

   DI is the key to understanding: "Who dependence, why rely on, who is who inject, inject something" that we have to analyze in depth:

     ● Who depends on whom: of course, is application dependent on the IoC container ;

     ● Why rely on: the application needs IoC container to provide external resources required objects ;

     ● Who injection: It is clear that the object IoC container inject an application object, the application-dependent ;

    ● injected what: that is injected into the external resources needed for an object (including objects, resources, constant data) .

  IoC and DI by what relationship do? In fact, they are the same concept describing a different point of view , due to the inversion of control concepts vague (probably only be understood as a container control object this level, it is hard to think of going to protect object-relational), so in 2004 the master at Martin Fowler has given a new name: "Dependency injection", relatively speaking IoC, "dependency injection" clearly describes the "object dependency is injected IoC container configuration dependent objects" .

Spring IOC examples

  Step a: dependence guide

    <dependency>

     <groupId>org.springframework</groupId>

      <artifactId>spring-beans</artifactId>

      <version>4.2.0.RELEASE</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-context</artifactId>

      <version>4.2.0.RELEASE</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-core</artifactId>

      <version>4.2.0.RELEASE</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-expression</artifactId>

      <version>4.2.0.RELEASE</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-jdbc</artifactId>

      <version>4.2.0.RELEASE</version>

    </dependency>

  Step Two: Write entity class

    

 

 

  Step three: Create the Spring core profile --applicationContext.xml

    

 

 

  Step Four: Test

    

 

Guess you like

Origin www.cnblogs.com/haohanwuyin/p/11740354.html