Spring journey (I) Why use Spring

Source: Internet
Author: User

Spring framework

Spring has Spring, Spring, jump and Spring eye. Spring is an open-source framework that is created by RodJohnson to solve the complexity of enterprise application development. Although Spring is launched for enterprise-level applications, Spring can be used for all java system development, including desktop applications and enterprise-level Web applications. Spring has never appeared out of thin air. Any framework is also because the struts and hibernate mentioned above have their own reasons, and what problems have prompted people to develop new frameworks. Why is the Spring framework used?

Starting from the abstract factory

Previously, in order to solve the problem of database replacement at the Dao layer, we had to write the factory by ourselves so that the factory could be created to hide the specific creation details during client calling, in this way, we can change the database. The abstract factory can also be used in the Service layer. To adapt to changes in user requirements, we apply the abstract factory at the sevice layer so that web calls can only call interfaces. The specific creation can be placed in the factory, in this way, we can replace different ones. class file to replace different business implementations. Next we will simply explain it through a program.

Dao Layer

The UserDao interface code is as follows.

Public interface UserDao {public void addUser (String username, String password);} implement UserDaoMySqlImpl at the Dao layer. javapublic class UserDao4MySqlImpl implements UserDao {@ Override public void addUser (String username, String password) {System. out. println ("UserDao4MySqlImpl. addUser ()");}}


Implement UserDaoOracleImpl. java at the Dao Layer

public class UserDao4OracleImpl implements UserDao {   @Override  public void addUser(String username, String password) {     System.out.println("UserDao4OracleImpl.addUser()");   } }


In this way, we create the Dao layer interface and provide different implementations for different database drivers.

Business logic layer manager (no factory is introduced here)

UserManager. java

public interface UserManager {   public void addUser(Stringusername,String password);}


Implement UserManagerImpl. java at the Manager layer

Public class UserManagerImpl implements UserManager {@ Override public void addUser (String username, String password) {// Service (object) location, where our application is responsible for service (object) location. // take the initiative // UserDao userDao = new UserDao4MySqlImpl (); // you can change it if you want to change the implementation. userDao userDao = new UserDao4OracleImpl (); userDao. addUser (username, password );}}


The abstract factory + configuration file is not used here. Let's look at our client call.

Public class client {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub // Service (object) location, our applications are responsible for service (object) positioning. // because you have generated an active search. therefore, it must be depended on and written to death. userManager userManager = new UserManagerImpl (); userManager. addUser ("Zhang San", "123 ");}}


If we want to change the database, we need to open the UserManagerImpl class to change different implementations. Similarly, we need different UserManager implementations for client calls if we want to change different businesses. Even if we add the abstract factory + configuration file, we also need to call the service layer on the interface and call the Dao layer to make the program more complex. Every project has to be written, this is also true for multiple projects. When the business logic is complex, we need several factories to create different products. When using the factory, we also need to ensure that one instance is created each time, all of this, even if the factory is manually written by our programmers, is repetitive. We manage these classes. Why not let machines manage them?


So Spring came into being. Spring encapsulates more duplicates and complexities. It not only encapsulates factories, but also encapsulates thread security issues during creation. Spring can manage connections, when using hibernate, we need to manage sessions, one thread and one session, and the session connection is our lazy validity period. spring provides a Filter, So we no longer need to manage session creation and destruction, spring helps us manage it.


Next, let's take a look at some of the terms that Spring will often mention.


In short, Spring is a lightweight container framework for IOC and AOP.

Lightweight: Spring is lightweight in terms of size and overhead.

Non-Intrusive: Objects in Spring applications do not depend on specific Spring classes.

Control reversal: Spring promotes loose coupling through a technology called IOC. When IOC is applied to an object, other objects dependent on it are passed in passively, instead of creating or searching for dependent objects.

Aspect Programming: Spring provides rich support for aspect programming, allowing separation of application business logic and services.

Container: Spring contains and manages the configuration and lifecycle of application objects. In this sense, Spring is a container, you can configure how each of your beans is created-a configurable prototype. Your bean can create a separate instance or generate a new instance every time you need it, and how they are associated.

Framework: Spring can combine simple component configurations into complex applications. In Spring, application objects are declared and combined, typically in an XML file, spring provides many basic functions (such as transaction management and persistent framework integration), leaving the development of application logic to us.

I 'd like to know about these terms. later articles will apply examples to describe Spring functions in detail.


In the next article, we will use Spring to implement the above example, so as to compare the strength of Spring's IOC.

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.