Spring学习

Spring学习,第1张

Spring学习

目录

环境搭建Spring集成web环境

ApplicationContext应用上下文获取方式 Spring提供获取应用上下文的工具

环境搭建

项目结构

1.需要有基本的Dao层

package com.itheima.dao.impl;
 
import com.itheima.dao.UserDao;
 
public class UserDaoImpl implements UserDao {
    public void save() {
        System.out.println("save running....");
    }
}
package com.itheima.dao;
 
public interface UserDao {
    public void save();
}

2、需要有持久层service

package com.itheima.service;
 
public interface UserService {
    public void save();
}
package com.itheima.service.impl;
 
import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
 
public class UserServiceImpl implements UserService {
 
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
 
    public void save() {
        userDao.save();
    }
}

3、建立web表现层

package com.itheima.web;
 
import ...
 
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();
    }
}

4、配置applicationContext.xml



 
    
    
 
    
    
        
        
        
        
    
 
    
    
 
    
    
        
    

需要tomcatweb层配置

配置web.xml



  
    UserServlet
    com.itheima.web.UserServlet
  
  
    UserServlet
    /userServlet
  


Spring集成web环境 ApplicationContext应用上下文获取方式

应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件)方式获取的,但是每次从容器中获取Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件),这样的弊端是配置文件加载多次,应用上下文对象创建多次。

首先创建一个含有ContextLoaderListener类的listener包
ContextLoaderListener类内容如下:

package com.itheima.listener;

import ...

public class ContextLoaderListener implements ServletContextListener {
    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容器创建完毕...");
    }
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }
}

在web.xml中配置监听器

  
    com.itheima.listener.ContextLoaderListener
  

之后就可以在UserService中通过这两句ServletContext servletContext = this.getServletContext(); ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");获取容器

package com.itheima.web;

import ...

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        ServletContext servletContext = this.getServletContext();
        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
}
Spring提供获取应用上下文的工具


导入坐标

    
      org.springframework
      spring-web
      5.0.5.RELEASE
    

web.xml

  
    contextConfigLocation
    classpath:applicationContext.xml
  


  
    org.springframework.web.context.ContextLoaderListener
    

  

通过工具获取应用上下文

        ServletContext servletContext = this.getServletContext();
        //ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        //ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = app.getBean(UserService.class);

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5708650.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存