本文纯属自我学习文章请勿过于计较
首先springframework中文文档为:Spring Framework 中文文档 - Spring Framework 5.1.3.RELEASE Reference | Docs4dev
第一步:
创建一个maven项目向里面填入插件信息
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
这是spring的mvc插件依赖(是一个多插件依赖)
1个依赖对应多个多个插件对应了以下这几个插件
第二步:创建一个实体类
第三步:
配置ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="Index of /schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="Index of /schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 构造一个实体类对象
也就相当于 pet pet=new pet()
意思是以后都不用自己去new了 创建对象全部由spring容器去管理 -->
<bean id="pet" class="pojo.pet">
<property name="petId" value="1"/>
<property name="petName" value="1"/>
<property name="petBreed" value="1"/>
<property name="petSex" value="1"/>
<property name="description" value="1"/>
</bean>
<!-- more bean definitions go here -->
</beans>
第四步:书写测试代码
第五步:如果我要获取一个service(逻辑层实体对象呢)
1.首先创建dao层和service层(已知service依赖于dao)
总目录:
Dao层:
services层:
然后ApplicationContext.xml配置为:
然后我们就可以通过spring容器去初始化这个service对象
@Test
public void go(){
//获取spring容器
ApplicationContext ApplicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
petServices petService= (petServices) ApplicationContext.getBean("petServiceImp");
System.out.println(petService.getPetDao().go());
}