Home  >  Article  >  Java  >  spring framework learning (1)

spring framework learning (1)

黄舟
黄舟Original
2016-12-29 13:07:521582browse

1. What is the spring framework?
spring is a J2EE application framework. It is a lightweight IoC and AOP container framework. It is mainly a lightweight container that manages the life cycle of javaBeans and can be used alone. It can also be used in combination with the Struts framework, ibatis framework, etc.


2, Architecture Overview

spring framework learning (1)

1) IoC (Inversion of Control) inversion of control, inversion of object creation responsibility, in spring BeanFactory is the core interface of the IoC container, responsible for instantiating, locating, configuring objects in the application and establishing dependencies between these objects. XmlBeanFactory implements the BeanFactory interface and forms application objects and dependencies between objects by obtaining xml configuration file data.
There are three injection methods in spring, one is set injection, one is interface injection, and the other is constructor injection.

2) AOP aspect-oriented programming
aop is vertical programming. As shown in the figure below, both business 1 and business 2 require a common operation. Instead of adding the same code to each business, It is better to write the code again and let the two businesses use this code together.

There are two ways to implement aspect-oriented changes in spring, one is dynamic proxy, and the other is CGLIB. Dynamic proxy must provide an interface, and CGLIB implementation has inheritance.

3. Why use the spring framework
Before using the spring framework, we had to use dao layer objects in our service layer and had to create a new object in the service layer. As follows:

//dao层对象
public class UserDao{
   publicvoid insert(User user){}
}
 
//service层对象
public classUserService{
   publicvoid insert(User user){
       UserDaouserdao = new UserDao();
       userdao.insert(user);
   }
}

Existing problems: dependencies between layers.
After using the framework:

//dao层对象
public class UserDao{
    publicvoid insert(User user){}
}
 
//service层对象
public classUserService{
   privateUserDao userdao;
 
   publicUserDao getUserdao() {
      returnuserdao;
   }
   publicvoid setUserdao(UserDao userdao) {
      this.userdao= userdao;
   }
 
   publicvoid insert(User user){
      userdao.insert(user);
   }
 
}

To use the dao layer object in the service layer, it needs to be configured in the xml configuration file. As for how the object is created and how the relationship is combined, it is left to the spring framework to implement.

4, Framework advantages

Lightweight container framework is not intrusive
Using IoC containers makes it easier to combine direct relationships between objects, interface-oriented programming, and reduce coupling
Aop can It is easier to expand functions and follow the ocp development principles
The created object is singleton by default, and there is no need to use the singleton mode for processing


5, Disadvantages: Business functions rely on spring-specific Functions, dependencies and spring environment.

The above is the content of spring framework learning (1). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn