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();
}