Spring Framework -----> (1) In-depth analysis of the Spring concept and IoC core technology of inversion of control

One, what is the Spring framework

1. Spring overview:

It is to ease the management between project modules, classes and classes, help developers create objects, and manage the relationships between objects.

2. Spring core technology:
(1) Inversion of control (IoC)

IoC : It can realize the decoupling between modules and classes, that is, there is no need to create the objects to be used by yourself. Instead, it is managed by the Spring container, which automatically "injects", and injection means assignment.

(2) Aspect-oriented programming (AOP)

AOP : Maximize the reuse of system-level services, and programmers no longer need to manually "mix" system-level services into the main business logic. Instead, the Spring container completes the "weaving".

3. Advantages:
  • 1. Lightweight
  • 2. Decoupling for interface programming
  • 3. AOP programming support

Two, IoC control inversion

1. IoC (Inversion of Control): Inversion of Control is a theory, concept, and idea.

(1) Inversion of control : the creation of objects, attribute assignment, and dependency management are all implemented by containers outside the code, that is, the creation of objects is completed by other external resources.

(2) Control : Object creation, attribute assignment of objects, and relationship management between objects.

(3) Reversal : Transfer the original developer management and object creation authority to a container outside of the code. The container manages the objects instead of the developer. Create objects and assign values ​​to attributes.

(4) Forward rotation : The developer uses the new construction method to create the object in the code, and the developer actively manages the object.

public static void main(String args[ ]){
    
    
     Student student = new Student( ); // 在代码中, 创建对象。
}

(5) Container : is a server software, a framework (spring)

2. What are the ways to create objects in java:
  • 1. Construction method, new object
  • 2. Reflection
  • 3. Serialization
  • 4. Clone
  • 5. ioc: container creation object
  • 6. Dynamic Agent

Here we have also learned the idea of ​​ioc before, such as servlet:
1. Create a class to inherit HttpServelt
2. Register servlet in web.xml and use
< servlet-name> myservlet < /servlet-name>
< servelt-class>com.hcz.controller.MyServlet< /servlet-class >
3. There is no Servlet object created here, that is, there is no MyServlet myservlet = new MyServlet( )
4. This Servlet object is created by the Tomcat server. Tomcat is also called container
5. Tomcat as a container: Servlet objects, Listener, and Filter objects are stored inside.

3. Why use IoC?

The purpose is to reduce changes to the code and achieve different functions. Realize decoupling.

4. Implementation of IoC
  • DI (Dependency Injection) dependency injection:

If you need to call another object for assistance, you don’t need to create a callee in the code, just provide the name of the object to be used in the program. As for how the object is created in the container, assignment, and search are all implemented inside the container. Passed by the container to the program.

  • The Spring framework uses dependency injection to implement IoC:

1. Spring is a container that manages objects and assigns values ​​to properties. The bottom layer is reflection to create objects.
2. The Spring container is a super factory responsible for creating and managing all Java objects. These Java objects are called Bean
3. Spring container management Regarding the dependencies between Beans in the container, Spring uses "dependency injection" to manage the dependencies between Beans

Three, create the first Spring program

(1) Create a maven project first

Insert picture description here

(2) Introduce maven dependency pom.xml
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.2.3.RELEASE</version>
</dependency>
(3) Define interfaces and entity classes
public interface SomeService {
    
    
    public void doSome();
}
public class SomeServiceImpl implements SomeService {
    
    
    public SomeServiceImpl(){
    
    
        System.out.println("无参构造方法");
    }
    @Override
    public void doSome() {
    
    
        System.out.println("执行了SomeService的doSome方法");
    }
}
(4) Create a Spring configuration file
Now create an xml file in the src/main/resources/ directory. The file name can be arbitrary, but Spring recommends the name applicationContext.xml.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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.xsd">

    <bean id="someService" class="com.hcz.service.impl.SomeServiceImpl"/>
    <bean id="someService1" class="com.hcz.service.impl.SomeServiceImpl"/>

    <!--创建一个非定义类的对象-->
    <bean id="myDate" class="java.util.Date"/>
</beans>

Analysis :
1. <bean>: used to create an instance object, an instance object corresponds to a bean tag
2. id attribute: the custom name of the object, it is a unique value, spring finds the instance object through this id value
3. class Attribute: the fully qualified name of the class (note: it cannot be an interface, because Spring is a reflection mechanism to create objects, you must use a class)

The underlying implementation principle of Spring :
SomeService someService = new SomeServiceImpl();
Spring puts the created objects into the map. The spring framework has a map to store the objects, springMap.put (id value, object);
for example:
springMap.put("someService",new SomeSerivceImp());

(5) Define the test class
/**
 * 1、不使用spring容器创建对象
 */
@Test
public void testSomeServiceOne(){
    
    
    SomeService service = new SomeServiceImpl();
    service.doSome();
}

/**
 * 2、使用spring容器创建对象
 */
@Test
public void testSomeServiceTwo(){
    
    
    
    //1.指定spring的配置文件
    String config = "bean.xml";
    //2.创建spring容器的【所有对象】,因为调用了它们各自的无参构造方法
    ApplicationContext context = new ClassPathXmlApplicationContext(config);
    //3.从容器中获取某个对象
    SomeService someService = (SomeService) context.getBean("someService");
    someService.doSome();
}

Analysis :
1. ApplicationContext: Represents the entry point of the IOC container. When the container object is initialized, all the objects in it will be assembled at one time. If you want to use these objects in the code in the future, you only need to get them directly from the memory. The execution efficiency is higher. But it takes up memory.
This class has two implementation classes for reading configuration files:

  • ClassPathXmlApplicationContext: means to read data from the classpath>
  • FileSystemXmlApplicationContext: means to read data from the current file system

2. When was the instance object in the
container created? The object in the container is already created when the spring container is created.

3. Schematic diagram of using Spring container to create objects
Insert picture description here

4. The spring container not only defines the method to obtain an object, but also defines the number of instance objects in the spring container and their respective object names

//3.从容器中获取容器中定义的对象的数量
int num = context.getBeanDefinitionCount();

//4.获取容器中每个定义的对象的名称
String[] names = context.getBeanDefinitionNames();

5. The spring container can also obtain non-customized classes, such as Date

Defined in the configuration file

<bean id="myDate" class="java.util.Date"/>

Define the test method

@Test
public void testSomeServiceFour(){
    
    
    //1.指定spring的配置文件
    String config = "bean.xml";
    //2.创建spring容器中的所有对象
    ApplicationContext context = new ClassPathXmlApplicationContext(config);
    //3.从容器中获取某个对象
    Date myDate = (Date) context.getBean("myDate");
    System.out.println(myDate);
}

If there are any shortcomings, please correct me!

Guess you like

Origin blog.csdn.net/hcz666/article/details/113190638