Spring Note 3-Continue HelloSpring, Context, IOC container, Bean

If you haven't read the second chapter, it is recommended to turn around! ! ! ( _ )
Spring note 2-the first Spring project HelloSpring, solve the template java not highlighting. In the
previous chapter, we have completed the end of the Spring configuration. This time, we are really starting to get to know Spring

Before that, we open the official document to see the composition of Spring

Insert picture description here
This picture is still very important (doge official original picture)

1. Context understanding

We generally call Spring Context the Spring context, that is, the Spring support environment, responsible for some parameter requests, public object resolution, etc. for us. You can think of it as a big factory, making basic preparations for the production we need

2. IOC container (inversion of control understanding)

This is what the official said (don’t understand)

This chapter covers Spring’s Inversion of Control (IoC) container.

Just understand it like this. IOC is a container and a container for storing our instance objects. Because of IOC, our program begins to passively accept objects (why say that, when we created a class in the past, it was through the program itself. Come new new new). If you write it like this, it may be fine if it is written by a high-level programmer, but most people may still be in the initial stage and have poor development ability (Tucao I use it myself), and then write it myself Code, here should be new, there is a new, if one day a user thinks that you and so and so and so and so and so and so and so and so and so and so and so and so and so and so and so and so and so and so and so and so and so and so and so and so and so and so and so and so, that they want to change a business, then you look at your "wind blows" code. No way, the code coupling is very high unconsciously.
So you start to resent this damn customer and make you busy again!
Then, in order to get rid of this embarrassment and confusion, one way is to add some development models (factory model or whatever) in the development, but we are learning Spring, as the so-called IOC is in hand, I have decoupling .
IOC inversion of control is to give the customer the initiative to instantiate the code and modify the business, while the program passively accepts the change.

3、Bean

Bean, you understand it like this. Each bean is an object, but the object referred to by each bean is the same, that is to say, the HashCode of the object they refer to is the same, we will talk about this later

Some friends may learn annotation development at the beginning, but in order to better understand Spring and related knowledge, I will start with Spring configuration files first, and then explain annotations (two-point comparison, complementary learning)

4. Actual combat

We did not intend to use annotation development at the beginning, we first use the Spring configuration file for development
(1) Find the resources folder under java , and create the beans.xml file (the name doesn’t matter, just pick it up)
Insert picture description here
Insert picture description here
Insert picture description here

We need to import some default configuration information for 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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

The following random point can be (obsessive-compulsive disorder)
Insert picture description here
we create a package under blue java, called com.spring.study , and create a Hello class under this package , and write the following code

Insert picture description here

package com.spring.study;

// 这只是一个镇压这个类所有警告的注解
@SuppressWarnings("all")
public class Hello {
    
    

    public String name;

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }
}

If we add the following code to the beans.xml file, we will pass the bean tag to instantiate the classes we need

Insert picture description here

<bean id="myHello" class="com.spring.study.Hello">
    <property name="name" value="HelloYSJ"/>
</bean>

Let’s make a detailed explanation on this label,

1、bean: Each bean tag is equivalent to our new (instantiated) operation
2,id: Code We assign a unique identifier to the object created by each bean, which is equivalent to the name of the object when we create the object
3.class: The full path of the class that our bean wants to instantiate
4.property:, each property represents that we want to modify a property of the id object, note that this tag can only be embedded in the bean
5.name: The name of the class member attribute representing this id
6.value: Represents what value you want to assign to this fifth (namely) member, (note that if you want to refer to the id object of a bean, you must change it to ref instead of value. In other cases, use value)

Next, we create
a test class. Create a test class in the green java in the template. The name should not be called test or Test. You can take a myTest.
Insert picture description here
Since all our beans are configured by xml, we can only use xml files. Came and got it

1. The first line of code is equivalent to obtaining the IOC container through the context, and the context is the obtained IOC container ( ApplicationContext is officially recommended )
2. The second line of code is instantiated from the IOC container through the getBean method The object id
3. Output detection
Insert picture description here

By the way, post an inheritance of the ApplicationContext class, which is very deep.
Insert picture description here

Maybe someone will ask here, how is the name attribute assigned? ? ?

Insert picture description here
We see that the first and second places have a leaf-shaped mark. The
first green leaf represents: This class has been managed by Spring. Spring will help you instantiate the
second yellow leaf representative. This class uses this method. Inject and assign a property


The first leaf indicates that the bean tag has imported this class. How does the bean instantiate this class? Constructor, reflection? ? ? (Interested partners can test it by themselves. This is explained in the next section.) The
second leaf means that our name attribute is injected through this method. Is the method name fixed? , Can the injection be completed without this method? ? Let's do a little test

test

Test 1: Change the name of the setName method.
Insert picture description here
We found a problem, which means that the method name is fixed as setName

If a property is called xxxx, the name of the injected method must be called setXxx (the first letter of the property name is capitalized, if the first letter is originally capitalized, it will not change), I guess it mainly refers to the camel case nomenclature

Test 2: Remove the setName method directly,
Insert picture description here
you may guess it, and you will definitely get an error

There is only so much content about this section. If you are interested, you can test and test yourself to understand the principle.

Guess you like

Origin blog.csdn.net/YSJ367635984/article/details/113104647