Summary of the Spring

Spring is a lightweight Java Development framework that emerged in 2003, based On some of the ideas and prototypes expounded by Rod Johnson in his book Expert One-to-One J2EE Development and Design. Spring was created to simplify the complexity of Java EE enterprise application development. Spring has become the de facto Java EE development standard, and learning about the Spring framework has become a required course for every Java developer.

The Spring framework promotes lightweight development based on POJOs (Plain Old Java Objects).

Spring official website Spring. IO, the latest version of Spring is 5.3.3 documentation

Docs. Spring. IO/spring – fram…

Introduction to Spring core components

Resource

The Spring framework internally uses the Resource interface as an abstraction and access interface for all resources. A ClassPathResource is a specific type of implementation of the Resource interface, representing resources in the CLASspath. The Resource interface can be implemented according to the different types of resources, and more implementations of the Resource interface provided by Spring will be described later in this article.

BeanDefinitionReader

Most functions in Spring are done through configuration, and reading configuration files is an important function in Spring. XmlBeanDefinitionReader is an implementation class for reading XML configuration files. XmlBeanDefinitionReader reads and parses the Spring specified configuration file, maps the parsed bean to the corresponding BeanDefinition, and loads it into the corresponding BeanFactory.

BeanFactory

BeanFactory is the root interface for accessing the Spring Bean container. It provides methods to access beans managed within the container. For example, we can use the getBean(String name) method to return the bean instance. The BeanFactory just an interface, we eventually need a the interface implementation class for the actual bean-managed, DefaultListableBeanFactory is more general the BeanFactory implementation class.

DefaultListableBeanFactory indirect class implements the BeanFactory interface also implements the BeanDefinitionRegistry interface. The BeanDefinitionRegistry interface defines the bean’s registration logic. Typically, a concrete BeanFactory implementation class will implement this interface to manage the bean’s registration.

Environment set up

To develop A Java application based on Spring, the general steps are as follows:

Declare the bean information in the Spring configuration file. Create an XML configuration file named ApplicationContext.xml.


      
<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="user" class="com.geekymv.spring.domain.User">
        <property name="id" value="1" />
        <property name="name" value="tom" />
    </bean>

</beans>
Copy the code

2. Load corresponding configuration files through various resources abstracted from Spring;

Declare a Spring Bean factory (BeanFactory) that manages the beans we define in the configuration file and the dependencies between the beans.

4, define the configuration information for the reader BeanDefinitionReader, used to read before, as defined by the bean configuration files, and the information that will read the assembly to the statement of the factory;

5. Correlate readers with factories and resource objects;

6. After all the objects managed by the Bean factory are assembled, the client can directly invoke various Bean objects.

public class SpringApplication {

    public static void main(String[] args) {

        // Load the bean configuration file
        Resource resource = new ClassPathResource("applicationContext.xml");
        // Declare the Spring Bean factory
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

        / / define BeanDefinitionReader, and specify the BeanFactory
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);

        // Perform configuration file parsing, bean information assembly, etc
        reader.loadBeanDefinitions(resource);

        // Get the bean instance managed by the BeanFactory
        User user = (User) beanFactory.getBean("user");
        System.out.println(user.getId() + ","+ user.getName()); }}Copy the code

Complete code github.com/geekymv/spr…

With a few lines of code above, you can implement a simple Spring application. The Spring framework does a lot of the tedious work behind the wheel, such as loading configuration files and resolving registered beans.

At this point, Spring source analysis preparations have been completed, and the next step is specific Spring source analysis.

More exciting content please follow the public geekymv, like please share with more friends oh”