1. Environment construction
Development IDE: IDEA 2020
Build tool: Maven 3.6.0 (install your own Maven in advance)
2. Create a project
Create a normal Maven project
3. Import dependencies
Import dependencies in pom.xml
It is enough to directly import Spring-webmvc, which contains all Spring basic Jar packages
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
The following are the basic packages required by the Spring project
4. Create related entity classes
Create related entity classes to use
public class People {
private String name;
public People(){
}
public People(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
'}';
}
}
5. Create related xml configuration files
Because the core of Spring is Spring's IOC mechanism, so using the basic Spring, we only need to learn how to use the Bean container to implement IOC and
create the Beans.xml configuration file
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--bean就是java对象 , 由Spring创建和管理-->
<!--在bean配置文件中关联我们的实体类-->
<bean id="people" class="person.jay.pojo.People">
<!--将实体类内的属性注入-->
<property name="name" value="Jay"/>
</bean>
</beans>
6. Junit test
Create a Junit test class for testing
public class PeopleTest {
@Test
public void people(){
//读取beans.xml配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//在执行getBean的时候, people已经通过无参构造创建好了,使用的时候只是将他从bean中取出来
People people = (People) context.getBean("people");
System.out.println(people.toString());
}
}