Spring Notes 4--Construction method to realize bean, discuss the scope of bean object

If you haven’t watched the previous article, I suggest you take a look at
Spring Note 3-Continue HelloSpring, Context, IOC Container, Bean
. In the previous section, we left a question about how bean objects in Spring are instantiated.
We know that there are many ways to create an object, such as: no parameter construction , parameter construction , reflection (essentially through the newInstance () method to obtain the constructor of the class), the clone method of the Object class (not calling the constructor, The premise is that there is an object for you to use, so it’s not possible), deserialization ...
These are just guesses, let’s actually use the code to conduct an example test to verify it.

1. No parameters or parameters to instance objects?

Create a new template, take the name yourself, create a package under blue java, my package is called (com.ysj.study), create a class HelloDemo1 under the package, as follows
Insert picture description here
(if you are an IDEA user, press Left alt+insert key can quickly write parameterized structure and getter and setter methods with one click, IDEAnb)

package com.ysj.study;

@SuppressWarnings("ALL")
public class HelloDemo1 {
    
    

    private String name;

    public HelloDemo1(){
    
    
        System.out.println("HelloDemo1的 无 参构造");
    }

    public HelloDemo1(String name) {
    
    
        System.out.println("HelloDemo1的 有 参构造");
        this.name = name;
    }

    public String getName() {
    
    
        return name;
    }

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

Still create a beans.xml file under the resources folder to import the basic configuration information code
Insert picture description here

<?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>

Then write the id creation code of the bean below. The
Insert picture description here
old method, write the test class code (without @Test annotation for the time being), create myTest
IDEA in the java folder under the test folder below, and then just enter CPX and there will be a prompt.
Insert picture description here
Insert picture description here

Output result:
Insert picture description here

Through the above output, it is not difficult to see that the route taken by the bean's default instantiation object is classNo-parameter construction

Two, parameterized construction to instantiate objects

Sometimes we need to use the parameterized structure of the class to instantiate the class, because this can instantiate some data for this object (it is true that the setter method can also be initialized all, just to learn how to use the bean through the parameterized structure)

<constructor-arg name="有参构成参数名" value="所需要赋的值"/>

Insert picture description here
Let's output the result again.
Insert picture description here
This is when there is only one parameter in the parameter construction, then if there are multiple parameters, the usage is still the same as above, just write a few more constructor-arg tags.

At the same time, we found that if we remove the setter method, the bean can still complete property injection.

So the parameterized construction of the bean does not depend on the setter method of the property! ! !
There is also a way to construct bean-id through BeanFactory. I forgot to write it in the blog last time. If you are interested, you can check it out by yourself. It is quite important.


3. Is the object of the Bean-id obtained each time the same?

Many people will wonder, every time we or the id object is transferred to the java code is the same object. For
example, look at the following test code
Insert picture description here
. Are helloDemo1 and helloDemo2 and helloDemo3 the same object? We say, judge whether the objects are the same , You can check the HashCode of the object address. If the HashCode is the same, it is the same object, otherwise, it is a different object.
Let's output the HashCode separately to see if it is the same.
Insert picture description here
Actually, it is the same object.
The advantage of the same object is that it can save memory and improve development efficiency. It is what we call the singleton mode
but it has disadvantages. Sometimes we don't want this object to be unique in memory, because we may need several copies in our transaction requirements. Different objects can be realized, what should I do?

Fourth, the life cycle and mode changes of the object in the IOC

The official provides us with a pattern diagram of objects in the IOC container,
Insert picture description here
which are singleton (singleton mode), prototype (prototype mode), request (request mode), session (session mode), application (application mode), websocket ( Communication mode)
In fact, it is not very important what it is called. And we are learning is spring5, is started, we are currently only being in contact with the first two (singleton and prototype mode) can be, back when we learned springmvc, javaweb and beyond will be used
then we Specifically talk about singleton mode and prototype mode

Singleton (singleton mode): Constraint that the object created each time is globally unique, and there is only one
prototype (prototype mode) in memory : Constraint that the object created every time is different, and there are multiple and independent copies in memory

Singleton mode: The
Insert picture description here
prototype mode
Insert picture description here
IOC container defaults to the singleton mode for the creation of each class object, which also explains why the HashCode of all objects obtained outside of java are the same, then if a certain class IOC container What about the instance mode inside

scope="模式名称"

Insert picture description here
Let's output the result again, and look at the HashCode
Insert picture description here
output result of each object to prove everything! ! !
That's it for this section, knowledge slowly absorbs O(∩_∩)O haha~

Guess you like

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