Day 1 of learning Spring

Day 1 of learning Spring

Frame:
Mybatis is used to solve the problem of interacting with the database.
Spring MVC is used to replace us in writing these complex Web logic.
Spring is a container framework.
EJB: Enterprise Java Bean

Core focus: IOC and AOP
Container: manage all components;
Components: those classes you write.
DI: dependency injection.
AOP: Aspect Oriented Programming.

Enter the official website:
https://spring.io/


To learn Spring well is to learn IOC and AOP well
Spring module;
1. Test module: Spring's unit test module
2. Core Container: Core Container (IOC)
There are beans, core, context and expression
3. AOP+Aspects: aspect oriented programming module
4. Data access: Spring data access module
jdbc, ORM (object relation mapping), TX (transaction)
5. Web: Spring develops web application modules
web-socket
Native web
web-mvc

IOC

Inversion Of Control
Control reversal
Control: control the acquisition method of resources; (passive)
There are two ways to obtain resources: active and passive.
Active: in the form of new, what resources do you want
Passive: the acquisition of resources is not cxy self created, but is created and set by a container.
Container: manage all components (functional classes.)
So control reversal means that the form of active new has become passive reception.

DI:

Dependency Injection
Dependency injection.
The container can know which component needs another class when it runs. The container uses reflection to copy the prepared objects in the container.
Register components in the container.

Now you are ready to test the functionality of Spring's IOC container.


1. Guide Package
Four jar packages of the core container
beans,context,core,expression


After importing these packages, you also need a log package.
Because when Spring runs, there is a log package. If there is no log package, an error will be reported.


2. Write configuration.
All components managed by Spring's IOC container in the configuration file.


3. Create the Person class.

4. Start writing some configurations in ioc.xml.
Register a Person object in ioc.xml, and Spring will automatically create the Person object

The largest label is beans.
A bean tag can register a component

<bean class="com.rtl.bean.Person"></bean>


Class: write the full class name of the component to be registered.

<bean id="person01" class="com.rtl.bean.Person"></bean>


id: the unique identifier of this object.

Assign a value to the property of the Person object using the property tag,
Name specifies the attribute name
Value: assign a value to this property name.
5. After the configuration file is written, start writing the test class
ApplicationContext: this class represents an IOC container.
ClassPathXmlApplicationContext: This is a subclass. Represents the container obtained in the form of an xml file.

package com.rtl.test;

import com.rtl.bean.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IOCTest {
    @Test
    public void test01(){
        ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");
        Person p1 = (Person)ioc.getBean("person01");
        System.out.println(p1);
    }

}

Test results:




View the location of ioc.xml.




Everything in src will enter the bin directory, and the bin directory is the beginning of the classpath.
There is an ioc.xml directly under src, so there will also be an ioc.xml directly under the bin directory
If ioc.xml is under the com package of src, there will also be ioc.xml under the com package under the corresponding bin

Explain details:
1. Inheritance tree of ApplicationContext.


1. ClassPathXmlApplicationContext represents that the configuration file of IOC container is under the classpath.
2. FileSystemXmlApplicationContext represents that the configuration file of the IOC container is under the disk path.
We got this component from the container by id.
Therefore, the creation of the Person object is done by the container.
Q: when was the Person object created?
Test:
Print a sentence for the nonparametric structure of Person:

public Person() {
        System.out.println("Person Object created");
    }


From this test, we found that:
Q: when was the Person object created?
Answer:
When the container is created, it is already created.

When this sentence is executed, it is already created.
 ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");


This test shows that:
First create all the objects in the container.
Third test:
Two bean tags are written in the ioc.xml configuration file.


Conclusion:
He will create all the objects in the container.

Fourth test:
In the test class, take the object with id=person01 twice. Are the two objects the same?


Conclusion: it is the same object.
The same component is single instance in the ioc container. The container is ready to be created when it starts.

Test 5:

There is no person03 component in the container.

Conclusion:
If this component is not in the container. An exception occurs when getting a component.

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'person03' is defined

Test 6:
There is only one bean tag of Person in ioc.xml

Previously obtained objects: they are obtained by the name of the id

Person p1 = (Person)ioc.getBean("person01");

Now?

Person p1 = ioc.getBean(Person.class);

However, it should be noted that:
In this form, ioc.xml can only have one bean tag of Person, otherwise an error will be reported:

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.rtl.bean.Person] is defined: expected single matching bean but found 2: person01,person02

Assign a value to the attribute in another way.

previous:

 <bean id="person01" class="com.rtl.bean.Person">
        <property name="lastName" value="Zhang San"></property>
        <property name="age" value="18"></property>
        <property name="email" value="zhangsan@qq.com"></property>
        <property name="gender" value="male"></property>
    </bean>


Assign values through the property property property.
Now?
1. Add a parameterized construct to the Person class.

2. Now ioc.xml

<bean id="person02" class="com.rtl.bean.Person">
        <constructor-arg name="lastName" value="Zhang San"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
        <constructor-arg name="email" value="zhangsan@qq.com"></constructor-arg>
        <constructor-arg name="gender" value="male"></constructor-arg>
    </bean>


Test class:

 public void test01(){
        ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");
        Person p2 = ioc.getBean("person02",Person.class);
        System.out.println(p2);
    }

be careful:
The attribute types in Person we just talked about are all basic data types.
So you can use the property tag to assign values.

If there are many types of attribute types, how to write bean tags in ioc.xml?

Keywords: Spring

Added by semsem on Sun, 31 Oct 2021 19:43:09 +0200