spring 整合 servlet

目的:记录spring整合 servlet过程demo。(企业实际开发中可能很少用到),融会贯通。

前言:在学习spring 过程(核心 ioc,aop,插一句 学了spring 才对这个有深刻概念, 在net时候都是直接使用,不得不说 java 还是深刻点)过程中,我们基本上都是在test中测试如图

环境:IDEA 

但是,开发中是 spring容器 是共享的,其他地方直接调用,这里就涉及到spring和其他的整合,此文servlet 为测试点。

1:新建servlet 过程参考,https://www.cnblogs.com/y112102/p/11338610.html

2:导入jar包。(4个核心,一个依赖)后期开发推荐使用 Maven,后面在测试,示logging找不到  , 日志也要导入

   2.1: spring-web 也要记得

在复习一遍:

1:    基础:4+1 , beans、core、context、expression , commons-logging 
2:    AOP:aop联盟、spring aop 、aspect规范、spring aspect
3:    db:jdbc、tx(事务)
4:    测试:test
5:    web开发:spring web

 3:web.xml 配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!-- 确定配置文件位置:
        classpath: src 或者classes 目录下-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- 配置spring 监听器,加载xml配置文件
         目的:tomcat启动的时候会自动根据配置文件创建 spring对象-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>HelloServlet</servlet-class>
    </servlet>

    <!-- 浏览器访问 hello 找到HelloServlet 请求  -->
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

</web-app>

  

 3.2 参考3.1配置文件 新建 HelloServlet 不要过多解释,如图:

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
/**
 * Create by  on 2019-09-10
 *
 * @author lsw
 */
public class HelloServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response){

        try {

            // 从application作用域(ServletContext)获得spring容器
            //方式1: 手动从作用域获取 getAttribute返回的Object类型 需要强转
            ApplicationContext applicationContext =
                    (ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

            //方式2:通过工具获取
            ApplicationContext apppApplicationContext2 =
                    WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());


            //从容器中取Bean的实例
            var c =  applicationContext.getBean("AddressId", Address.class);
            System.out.println(  c.toString() );

            response.getWriter().println("<h1>Hello Servlet!</h1>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

最后启动运行结果:

控制台:

总结:

servlet :web层,这里将来是spring mvc ,有很多选择 比如 struts(现在也很少用到了)

mybatis:dao层,知识点杂

spring:service层

java 入门 坚持下,你会发现很简单的,特别是net开发者,接触java 后才去理解net的开发方式 又会有另一番心得。

所有的都是围绕次服务 ,扩展的。

猜你喜欢

转载自www.cnblogs.com/y112102/p/11498564.html