Spring笔记(4)

Spring笔记(4)

集成Web环境

1.步骤

  1. 导入Spring-web坐标

    <!--    spring-web-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>5.0.5.RELEASE</version>
        </dependency>
    
  2. 将ApplicationContext.xml的文件名称存入web.xml的全局参数中

    <!--  全局初始化参数-->
    <!--applicationContext文件名-->
    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
  3. 配置监听器

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

  5. 使用WebApplicationContextUtils获取ApplicationContext(获取的为子类WebApp..)

    //获取app对象(使用工具包中的方法)
    ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    

2.pom.xml

<!--    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>
<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对象在监听器模块内部实现存储
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中)

<!--  全局初始化参数-->
  <!--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>

内部封装好

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时应该加入范围

<scope>provided</scope>

applicationContext.xml中:

添加扫描注解

<context:component-scan base-package="dao"/>
<context:component-scan base-package="service"/>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • C语言实现学生成绩管理系统(EasyX图形界面)

    C语言实现学生成绩管理系统(EasyX图形界面)我的小站——半生瓜のblog代码文件下载链接——链接学生成绩管理系统学生成绩管理系统效果图流程&注意要点代码实现学生成绩管理系统效果图流程&注意要点核心部分——EasyX显示图形界面,结构体数组和文件操作负责对数据进行各种操作。只要一进去程序就对存储数据的文件进行读取,如果有数据直接读到学生结构体数组里面,得到当前结构体数组中的数据数量(几个人),并将所有数据打印到屏幕上。管理员用户输入密码验证——读取文件——验证用户输入是否正确。显示所有数据——如果通过文件.

  • QTabWidget的详细使用「建议收藏」

    QTabWidget的详细使用「建议收藏」QTabWidget介绍QTabWidget主要是用来分页显示的,每一页一个界面,众多界面公用一块区域,节省了界面大小,很方便的为用户显示更多的信息1,创建四个QWidgetself.tab1=QWidget()self.tab2=QWidget()self.tab3=QWidget()self.ta…

  • 【python】Windows中编译安装libsamplerate和scikits.samplerate

    【python】Windows中编译安装libsamplerate和scikits.sampleratelibrosa缘由librosa是一个音频和音乐处理的Python包,我用它来做音频的特征提取。但是在使用时,发现librosa.load将音乐文件转化为时间序列的过程中,速度实在难以忍受,cpu跑的非常高,程序好像假死的状态。查阅官方文档发现,默认情况下,librosa会使用scipy.signal进行音频信号的重采样,这在实际使用时是很慢的。如果要获得很高的性能,官方建议安装libsampl

    2022年10月17日
  • java.lang.NumberFormatException: For input string: “0.001”

    java.lang.NumberFormatException: For input string: “0.001”

  • linux mysql mysql.sock(mysqldump命令详解)

    什么是socat?socat是一个实用的命令行工具。常用方法端口转发#端口转发#socat-d-d-lf/var/log/socat.logTCP4-LISTEN:6666,bind=127.0.0.1,reuseaddr,forkTCP:127.0.0.1:3306#mysql-uroot-h127.0.0.1-P6666-p文件写入#不使用group则默认当前用户soc…

  • matlab求解时滞微分方程_matlab延迟环节传递函数

    matlab求解时滞微分方程_matlab延迟环节传递函数具有常时滞的DDERef:时滞微分方程—示例

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号