[Spring] HelloSpring-Introduction and Quick Start

Introduction to Spring

What is Spring

  • Spring is J2EE layered lightweight development framework that provides full stack (full-stack) solution, it IOC (Inverse Of Control, inversion control ) and AOP (Aspect Oriented Programming, Aspect Oriented Programming ) kernel.
  • Provides many enterprise-level application technologies such as presentation layer SpringMVC, persistence layer SpringJDBCTemplate, business layer transaction management, etc. It can also integrate many famous third-party frameworks and class libraries in the open source world, gradually becoming the most used JavaEE enterprise application open source framework

Spring development history

  1. Rod Johnson (Spring father) questioned the bloated and inefficient JavaEE framework in 2002 and 2004, the publication of the "Expert one-on-one J2EE Development without EJB" with its new design employed instead , And became famous
  2. Traditional JavaEE has not fulfilled the promise of "Write Once and Run Anywhere", while Johnson's new design provides a simple and powerful solution for each layer. This one-stop service is "full stack".
  3. The classic version of Spring Spring 5.0 General Edition (GA) released in September 2017 has become the current optimal solution sought by today’s enterprises

In fact, which is better between Spring and EJB is a false proposition. EJB is a traditional and rigorous specification for JavaEE, and Spring is a simple and flexible framework for JavaEE. Rod Johnson's criticism is not negative, but seeks a more beautiful design on the basis of the academic school.

Advantages of Spring

  1. Convenient decoupling and simplifying development.
    Through the IoC container provided by Spring, we can hand over the dependencies between objects to Spring to avoid excessive program coupling caused by hard coding (for example, newDao layer objects in the Service layer are a typical coupling ). In this way, you can focus more on the upper-level applications.
  2. AOP programming support
    Through the AOP function provided by Spring, it is convenient to carry out aspect-oriented programming. In short, it is to solve the problem that OOP is not easy to solve.
  3. Support for declarative transactions
    In Spring, we can get rid of the monotonous and boring transaction management code (manual control of begin, commit, rollback), but flexible management through declarative methods.
  4. Convenient program testing
    In Spring, testing is no longer an expensive operation, but something you can do at your fingertips. Spring supports Junit and can easily test Spring programs through annotations.
  5. Facilitate the integration of various excellent frameworks
    Spring not only does not exclude various excellent open source frameworks, but even Spring can reduce the difficulty of using various frameworks. Spring supports Struts, Hibernate, Hessian, Quartz, etc.
  6. Reduce the difficulty of using Java EE API
    Spring provides a thin encapsulation layer for many difficult-to-use Java EE APIs (such as JDBC, JavaMail, remote calls, etc.). Through Spring’s simple encapsulation (you don’t need to write Util classes by hand!), The difficulty of using Java EE API is greatly reduced.
  7. Java source code is a classic learning example.
    Spring's source code design is exquisite, clear in structure, and original ingenuity. It reflects the master's flexible use of Java design patterns and advanced knowledge of Java technology. The Spring framework source code is undoubtedly the only way to learn Java.

 
 

Spring quick start

▍Development steps

Insert picture description here

① Import Maven coordinates of Spring related packages

② Write Dao interface and its implementation class

③ Create and configure the xml file to realize the mapping between id and the complete class name class

④ Use Spring's API to obtain examples
 
▍Code implementation

① Introduce dependency in pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
</dependencies>

② Standard structure of dao layer

// dao.UserDao
public interface UserDao {
    
    
    public void sayHi();
}
// dao.impl.UserDaoImpl
public class UserDaoImpl implements UserDao {
    
    

    public void sayHi() {
    
    
        System.out.println("Hello!!");
    }
    
}

③ Create a new applicationContext and configure under resource

<bean id="userDao" class="dao.impl.UserDaoImpl"></bean>

④ Use of service layer

public class UserDaoDemo {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = (UserDao)app.getBean("userDao");
        userDao.sayHi();
    }
}

 
 
 
 

 
 
 
 

 
 
 
 

More >_<

Guess you like

Origin blog.csdn.net/m0_46202073/article/details/113737393