A preliminary solution to the working principle of spring framework

Keywords: Programming Spring xml Struts Hibernate

1: Basic concepts of spring

1) Struts 2 is web framework, hibernate is orm framework

2) spring is a container framework, which creates beans and maintains the relationship between beans

3) Spring can manage the web layer, persistence layer, business layer, dao layer, spring can configure the components of each layer, and maintain the relationship of each layer

2: Core principles of spring

1.IOC control reversal

Concept: control is transferred from the object itself to the container, which creates the object instance according to the configuration file and implements the dependency of each object.

Core: bean factory

IoC (Inverse of Control) literally means inversion of control, which includes two contents:

One is control, the other is reversal

So what is the reverse of control?

For software, that is to say, the selection control right of a specific interface implementation class is removed from the calling class and transferred to the third party for decision.

However, because IoC is not straightforward enough, there has been extensive discussion in the industry. Finally, Martin Fowler, a leading figure in the software industry, proposed the concept of DI (Dependency Injection) to replace IoC.

That is to say, the dependency of the calling class on an interface implementation class is injected by a third party (container or collaboration class) to remove the dependency of the calling class on an interface implementation class. The term "dependency injection" is obviously more straightforward and easy to understand than "inversion of control".

2.AOP aspect oriented programming

a. Static agent

Write agent classes according to each concrete class

Write a proxy class according to an interface

b. Dynamic agent

Write an InvocationHandler for an aspect, and then use the Proxy class in the JDK reflection package to dynamically generate the corresponding Proxy class for various interfaces

3: A simple introduction to Spring

1. Write a class: UserService

package com.cloud.service; 
public class UserService { 
    private String name; 
    public String getName() { 
        return name; 
    public void setName(String name) { 
        this.name = name; 
    public void sayHello(){ 

2. Write core configuration file: applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> 
    <!-- Configure in container bean object --> 
    <!-- The following sentence is equivalent to: UserService userService = new UserService() --> 
    <bean id="userService" class="com.cloud.service.UserService"> 
        <!-- Equivalent to: userService.setName("SpringName"); --> 
        <property name="name"> 

3. Write test class: Test

package com.cloud.test; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.cloud.service.UserService; 
public class Test { 
    public static void main(String[] args) { 
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 
        UserService userService = (UserService) ac.getBean("userService"); 

4: spring principle summary
1) . with spring, there is no new object. We give the task of creating the object to the spring framework.

Through new xmlbeanfactory(“ applicationContext.xml ”)Wait until the container is started. When the container is started, Spring automatically instantiates the Bean and completes the assembly of dependencies according to the description information of the configuration file. From the container, you can return the ready Bean instance, which can be used directly later.
Why does Spring have this "magic" power? With a simple configuration file, it can magically instantiate and assemble the beans used by the program? This "magic" power is due to the Java language's own class reflection capabilities.
2) spring is actually a container framework, which can configure various beans (action / service / domain / Dao) and maintain the relationship between beans. When we need to use a bean, we can get the bean (ID) and use it.

Posted by werty37 on Tue, 09 Jun 2020 23:13:17 -0700