Get into the habit of writing together! This is the 4th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the details of the event .
Three-tier architecture environment construction
In the previous explanation of the Spring core configuration file and data source configuration, mainly in the dao layer and the service layer, now we also integrate the web layer environment into it.
Current project structure
Build the web layer
1. Introduce servlet and jsp dependencies
<!-- 引入Servlet和JSP依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
复制代码
2. Create the UserServlet class
@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) application.getBean("userService");
userService.save();
}
}
复制代码
3. Configure Tomcat and start the test
Start Tomcat and access /UserServlet, the console prints as follows, and the basic environment of the three-tier architecture is successfully built.
Three-tier architecture basic project structure
Set the listener to get applicationContext.xml
Now there is a problem: every time you get a bean from the container, you need to ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml")
. When the configuration file is loaded multiple times, many ApplicationContext objects will be created, resulting in a waste of resources.
In the Web project, we use the characteristics of the listener to create a configuration file loading listener, load the Spring configuration file when the application starts, and create an ApplicationContext object; when it is to be used, it can be obtained directly from the domain. , to achieve the effect of one load and use everywhere.
Configure the location of the core configuration file in web.xml
<context-param>
<param-name>applicationContextLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
复制代码
Create a ContextLoaderListener class
public class ContextLoaderListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContextEvent sce;
ServletContext servletContext = servletContextEvent.getServletContext();
// 读取web.xml中配置的Spring核心配置文件的位置
String applicationContextLocation = servletContext.getInitParameter("applicationContextLocation");
// 创建ApplicationContext上下文对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(applicationContextLocation);
// 将ApplicationContext对象存到域中
servletContext.setAttribute("applicationContext",applicationContext);
System.out.println("创建ApplicationContext对象成功:" + applicationContext);
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
复制代码
Configure the ContextLoaderListener listener in web.xml
<listener>
<listener-class>com.wang.listener.ContextLoaderListener</listener-class>
</listener>
复制代码
Start Tomcat, check the console print, and find that the ApplicationContext object is created when Tomcat starts:
Modify UserServlet and use listener to get ApplicationContext
@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext application = (ApplicationContext) req.getServletContext().getAttribute("applicationContext");
UserService userService = (UserService) application.getBean("userService");
userService.save();
}
}
复制代码
Integrating ContextLoaderListener listeners with Spring
The above way of customizing the listener realizes that the Spring configuration file is loaded when the application starts, and the ApplicationContext object is created. However, this process is still relatively complicated. You can use the tools provided by Spring to obtain the application context to directly obtain the context object.
Introduce spring-web package
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
复制代码
Modify web.xml to configure Spring's ContextLoaderListener listener
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
复制代码
Get Application Context Object Using Spring Tools
@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
// ApplicationContext application = (ApplicationContext) req.getServletContext().getAttribute("applicationContext");
ApplicationContext application = WebApplicationContextUtils.getWebApplicationContext(req.getServletContext());
UserService userService = (UserService) application.getBean("userService");
userService.save();
}
}
复制代码