Spring集成Web

Spring 集成web

环境集成

web层调用service层,service层调层

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>

<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.1</version>
</dependency>

Spring 集成web环境

https://blog.csdn.net/weixin_45273750/article/details/119221662?spm=1001.2014.3001.5502

UserDaoImpl.java

public class UserDaoImpl implements UserDao {
    @Override
    public void save() {
        System.out.println("save running...");
    }
}

UserServiceImpl.java

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void save() {
        userDao.save();
    }
}

UserServlet.java

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
}

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_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>com.zyz.web.UserServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/userService</url-pattern>
    </servlet-mapping>
</web-app>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation=
               "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd"
        >

<!--    配置Dao-->
    <bean id="userDao" class="com.zyz.dao.impl.UserDaoImpl"></bean>
<!--配置service-->
    <bean id="userService" class="com.zyz.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
       </bean>

<!--    配置主键扫描-->
    <context:component-scan base-package="com.zyz"/>

<!--    <import resource=""/>-->
</beans>

在这里插入图片描述

监听器

为了避免上面的多次加载配置文件,多次创建对象;

ContextLoaderListener.java

public class ContextLoaderListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        //将这个Spring应用上下文对象存储在ServletContext域中
        ServletContext servletContext = servletContextEvent.getServletContext();
        servletContext.setAttribute("app",app);//取名,存域
        System.out.println("spring容器创建完毕.. ");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

web.xml添加

<!--    配置监听器-->
    <listener>
        <listener-class>com.zyz.listener.ContextLoaderListener</listener-class>
    </listener>

UserServlet.java

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        ApplicationContext app = (ApplicationContext)servletContext.getAttribute("app");
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
}

这样可以在服务器启动用户访问前就把Spring容器创建好,用户只需要从容器中拿,提高用户体验性能

代码优化

web,xml中添加全局参数配置

<!--    全局初始化参数-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>applicationContext.xml</param-value>
    </context-param>

报红不是错

监听类的初始化方法

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {

    //将这个Spring应用上下文对象存储在ServletContext域中
    ServletContext servletContext = servletContextEvent.getServletContext();

    //读取web.xml中的全局参数
    String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");

    ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
    servletContext.setAttribute("app",app);//取名,存域
    System.out.println("spring容器创建完毕.. ");
}

WebApplicationContextUtil.java

public class WebApplicationContextUtil {

    public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
        return (ApplicationContext)servletContext.getAttribute("app");
    }
}

UserServer.java调用

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext servletContext = this.getServletContext();
    ApplicationContext app = WebApplicationContextUtil.getWebApplicationContext(servletContext);
    UserService userService = app.getBean(UserService.class);
    userService.save();
}

以上的内容只是帮助理解Spring中监听器的原理

Spring获取上下文的工具

在这里插入图片描述

导入坐标
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.2.8.RELEASE</version>
</dependency>

配置web.xml

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

先前用的自己写的所以applicationContext.xml爆红,这次是用Spring内部提供的

 @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
//        ApplicationContext app = WebApplicationContextUtil.getWebApplicationContext(servletContext);

        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值