1.Maven项目中导入jar包依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.5</version>
</dependency>
</dependencies>
2.编写实体类
3.编写spring文件,命名为beans.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="com.spring.pojo.Hello">
<property name="name" value="Spring"/>
</bean>
<!-- more bean definitions go here -->
</beans>
4.编写测试类
@Test
public void springTest(){
//获取spring的上下文对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
//在容器中获取需要的对象
Hello hello = (Hello)applicationContext.getBean("hello");
hello.show();
}
5.结果输出