前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring笔记(4)

Spring笔记(4)

作者头像
全栈程序员站长
发布2021-12-23 18:34:43
1790
发布2021-12-23 18:34:43
举报

集成Web环境

1.步骤

2.pom.xml

代码语言:javascript
复制
<!--    servlet-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>
<!--    servlet-jsp-->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.2.1</version>
      <scope>provided</scope>
    </dependency>
<!--    spring-context-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
<!--    jdbc-druid-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.9</version>
    </dependency>
<!--    jdbc-c3p0-->
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>
<!--    spring-web-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
代码语言:javascript
复制
<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.2</version>
</plugin>

3.servlet具体实现

3.1工具包实现:(封装好的)

  • ​ servlet中的app通过工具包中的方法获得()
  • ​ 在servletContext域中获得app对象
  • ​ 域中app对象在监听器模块内部实现存储
代码语言:javascript
复制
public class ApplicationContextLoaderUtil {
    public static ApplicationContext getApplicationContext(ServletContext servletContext){
        //在域中获取applicationContext对象
        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        //测试
        System.out.println("通过工具类获取app");
        return app;
    }
}

3.2监听器实现(封装好的)

手动配置(web.xml中)

代码语言:javascript
复制
<!--  全局初始化参数-->
  <!--applicationContext文件名-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

<!--  配置监听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

内部封装好

代码语言:javascript
复制
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //在servletContext中获取ApplicationContext对象
    //获取servletContext
    ServletContext servletContext = request.getServletContext();
    //获取app对象(使用工具包中的方法)
    ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    //获取userService
    UserService userService = app.getBean(UserService.class);
    userService.save();

4.注意

配置pom.xml时:

servlet-api和servlet-jsp-api时应该加入范围

代码语言:javascript
复制
<scope>provided</scope>

applicationContext.xml中:

添加扫描注解

代码语言:javascript
复制
<context:component-scan base-package="dao"/>
<context:component-scan base-package="service"/>

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/115198.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021年11月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 集成Web环境
    • 1.步骤
      • 2.pom.xml
        • 3.servlet具体实现
          • 3.1工具包实现:(封装好的)
          • 3.2监听器实现(封装好的)
        • 4.注意
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档