1. 搭建Web运⾏环境
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.18</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
web环境我们就搭建好了,这时就出现了一个问题?
2. 为什么要整合MVC框架
- MVC框架提供了控制器(Controller)调⽤Service
DAO —》 Service - 请求响应的处理
- 接受请求参数 request.getParameter("")
- 控制程序的运⾏流程
- 视图解析 (JSP JSON Freemarker Thyemeleaf )
3. Spring可以整合那些MVC框架
- struts1
- webwork
- jsf
- struts2
- springMVC
Spring整合MVC框架的核⼼思路
1. 准备⼯⼚
1. Web开发过程中如何创建⼯⼚
ApplicationContext ctx = new
ClassPathXmlApplicationContext("/applicationContext.xml");
WebXmlApplicationContext()
2. 如何保证⼯⼚唯⼀同时被共⽤
被共⽤:Web request|session|ServletContext(application)
⼯⼚存储在ServletContext这个作⽤域中
ServletContext.setAttribute("xxxx",ctx);
唯⼀:ServletContext对象 创建的同时 ---》 ApplicationContext ctx = new
ClassPathXmlApplicationContext("/applicationContext.xml");
ServletContextListener ---> ApplicationContext ctx = new
ClassPathXmlApplicationContext("/applicationContext.xml");
ServletContextListener 在ServletContext对象创建的同时,被调⽤(只会
被调⽤⼀次) ,把⼯⼚创建的代码,写在ServletContextListener中,也会保证只调⽤
⼀次,最终⼯⼚就保证了唯⼀性
3. 总结
ServletContextListener(唯⼀)
ApplicationContext ctx = new
ClassPathXmlApplicationContext("/applicationContext.xml");
ServletContext.setAttribute("xxx",ctx) (共⽤)
4. Spring封装了⼀个ContextLoaderListener
1. 创建⼯⼚
2. 把⼯⼚存在ServletContext中
5. ContextLoaderListener使⽤⽅式
web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
2.代码整合
依赖注⼊:把Sevice对象注⼊个控制器对象。