Spring学习笔记目录
- 步骤
- 代码示例
- 导入需要的坐标
- 运行图片
- ApplicationContext应用上下文对象
- 概念
- 代码示例
- 运行结果
- 改进
- 运行结果
- Spring提供获取应用上下文的工具
- 概念
- 步骤
- 运行结果
①导入需要的坐标
代码示例项目结构:
org.springframework spring-context 5.0.5.RELEASE javax.servlet javax.servlet-api 3.0.1 provided javax.servlet.jsp javax.servlet.jsp-api 2.2.1
新建接口UserDao.java:
package com.stu.dao;
public interface UserDao {
public void save();
}
新建实现类UserDaoImpl .java:
package com.stu.dao.impl;
import com.stu.dao.UserDao;
public class UserDaoImpl implements UserDao {
@Override
public void save() {
System.out.println("save runing");
}
}
新建接口类UserService.java:
package com.stu.service;
public interface UserService {
public void save();
}
新建实现类UserServiceImpl.java:
package com.stu.service.impl;
import com.stu.dao.UserDao;
import com.stu.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save(){
userDao.save();
}
}
新建配置文件ApplicationContext.xml:
新建类UserServlet.java:
package com.web;
import com.stu.service.UserService;
import com.stu.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = applicationContext.getBean(UserService.class);
userService.save();
}
}
在web.xmnl中新增以下代码:
UserServlet com.web.UserServlet UserServlet /userServlet
配置Tomcat
运行tomcat后,在网址区域输入http://localhost:8080/userServlet在控制台打有以下结果就说明成功。
代码示例在Web项目中,可以使用ServletContextListener监听Web应用的启动,在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。——黑马程序员
新建类ContextLoaderListener.java:
package com.stu.listener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ContextLoaderListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//将Spring的应用上下文对象存储到Servlet域中
ServletContext servletContext = servletContextEvent.getServletContext();//获得ServletContext
servletContext.setAttribute("applicationContext",applicationContext);//通过ServletContext设置属性
System.out.println("spring容器创建完毕...");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
类UserServlet.java新增以下代码:
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// req.getServletContext();
ServletContext servletContext = this.getServletContext();
ApplicationContext applicationContext = (ApplicationContext) servletContext.getAttribute("applicationContext");
web.xml中新增以下代码:
com.stu.listener.ContextLoaderListener
运行结果
在浏览器的网址中输入http://localhost:8080/userServlet
这两处在代码中是已经写定了,耦合程度高,接下来进行解耦操作。
项目结构:
web.xml新增以下配置:
contextConfigLocation
applicationContext.xml
报红没关系,不管它。
ContextLoaderListener.java的contextInitialized()方法内的代码更改为以下:
//将Spring的应用上下文对象存储到Servlet域中
ServletContext servletContext = servletContextEvent.getServletContext();//获得ServletContext
//读取web.xml中的全局参数
String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(contextConfigLocation);
servletContext.setAttribute("applicationContext",applicationContext);//通过ServletContext设置属性
System.out.println("spring容器创建完毕...");
新建类WebApplicationContextUtils:
package com.stu.listener;
import org.springframework.context.ApplicationContext;
import javax.servlet.ServletContext;
public class WebApplicationContextUtils {
public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
return (ApplicationContext) servletContext.getAttribute("applicationContext");
}
}
UserServlet.java新增以下代码:
// ApplicationContext applicationContext = (ApplicationContext) servletContext.getAttribute("applicationContext");
ServletContext servletContext = this.getServletContext();
这样写就不需要总是记住文件中指定的applicationContext的名,该方法会直接返回。
在浏览器的网址中输入http://localhost:8080/userServlet
在控制台中打印出以下结果说明配置成功。
步骤上面的分析不用手动实现,Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获得应用上下文对象。——黑马程序员
①在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标)
②使用WebApplicationContextUtils获得应用上下文对象ApplicationContext
在pom.xml新增以下坐标:
org.springframework spring-web 5.0.5.RELEASE
web.xml新增以下配置:
classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener
UserServlet的doGet方法替换为以下:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// req.getServletContext();
ServletContext servletContext = this.getServletContext();
// ApplicationContext applicationContext = (ApplicationContext) servletContext.getAttribute("applicationContext");
// ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserService userService = applicationContext.getBean(UserService.class);
userService.save();
}
运行结果
运行tomcat后,在网址区域输入http://localhost:8080/userServlet在控制台打有以下结果就说明成功。
Tomcat正常运行,且控制台有打印对应结果。



