preface:
Hello, guys, I'm running snail rz. Of course, you can call me snail Jun. I'm a rookie who has studied Java for more than half a year. At the same time, I also have a great dream, that is, to become an excellent Java Architect one day.
This Spring basic learning series is used to record the whole process of learning the basic knowledge of Spring framework (this series is written with reference to the latest Spring 5 tutorial of crazy God in station B. because it was sorted out before, but it was not released at that time, there may be errors in some places. I hope you can correct them in time!)
After that, I will update this series at a faster rate day by day. If you haven't learned the spring 5 framework, you can learn from my blog; Of course, the little friends who have studied can also review the basics with me.
Finally, I hope I can make progress with you! Come on! Boys!
No more nonsense. Let's start today's learning content. Today we come to the third stop of Spring basic learning: the first hello Spring program!
3. The first hellosppring program
3.1 creating projects and generating profiles
3.1. 1 create Maven project
Create a new Module and select Create Maven project
3.1.2 generate pom.xml configuration file
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-study</artifactId> <groupId>com.kaung</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-02-hellospring</artifactId> </project>
3.2 writing entity classes and beans configuration files
3.2.1 write Hello entity class
package com.kuang.pojo; //Create a Hello class public class Hello { private String str; public String getStr() { return str; } //Injection using set method (dependency injection) public void setStr(String str) { this.str = str; } @Override public String toString() { return "Hello{" + "str='" + str + '\'' + '}'; } }
3.2.2 write beans.xml file
Create the beans.xml file in the resources folder and write the content
<?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"> <!--use Spring To create an object, in Spring These are called Bean Object instantiation object = new object() id = Instantiate object class = new Object of property This is equivalent to setting a value for an attribute in an object --> <bean id="hello" class="com.kuang.pojo.Hello"> <property name="str" value="Spring"/> </bean> </beans>
3.3 write test classes and test results
3.3.1 write MyTest test class
//Main method test public class MyTest { public static void main(String[] args) { //Get the context object of Spring ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); //Objects are managed in Spring. You need to use them and take them out directly Hello hello = (Hello) context.getBean("hello"); hello.toString(); System.out.println(hello.toString()); } }
3.3.2 test results
3.4 analysis of test results
3.4.1 who creates objects and attributes
- The Hello object is created by Spring
- The properties of the Hello object are set by the Spring container
3.4.2 control reversal
The above process is called control reversal
- Control: who controls the creation of objects? Traditional application objects are created and controlled by the program itself. After using Spring, objects are created by Spring
- Inversion: the program itself does not create an object, but becomes a passive receiving object
- Dependency injection: it uses the set method to inject
IOC is a programming idea, which changes from active programming to passive reception; It does not need to be changed in the program. To realize different operations, it only needs to be modified in the xml configuration file
The so-called IOC: objects are created, managed and assembled by Spring!
It's equivalent to inviting someone to dinner. The original procedure is: you write the menu and buy the dishes. When the guests come, fry the dishes and entertain the guests;
IOC: cooking is not done by yourself, but entrusted to a third party (restaurant)
3.4.3 knowledge expansion
Inheritance and implementation relationship from ClassPathXmlApplicationContext to ApplicationContext
Well, today's study on the first hellosppring program of basic Spring learning is over. Welcome to study and discuss actively. If you like, you can pay attention to Mr. snail. By the way, let's have a one click three links. See you next time. Bye!
Reference video link: https://www.bilibili.com/video/BV1WE411d7Dv([crazy God says Java] the latest tutorial of spring 5, the IDEA version is easy to understand)