SpringMVC 温习笔记(一)服务器启动和组件解析
spring 组件扫描解析
首先小猿先引入一张图片,该图片可以全面的诠释自定义组件的全部运行过程:
整个过程中最为核心的思想就是tomcat服务器启动的时候可以直接通过监听器去加载spring配置文件applicationContext.xml文件,以这种方式,我们可以借助tomcat服务器加载监听器的过程,可将service层,dao层的各类bean纳入到spring容器中来进行管理,这样就使得整个启动过程变得非常完美,总之自定义监听类的本质作用是将tomcat容器初始化和spring容器初始化紧密的链接在了一起。
下面小猿将整个小案例的代码附在下面。
项目依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</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>
dao 层
接口
public interface UserDao {
public void save();
}
实现类
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("save running .......");
}
}
service层
接口
public interface UserService {
public void save();
}
实现类
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save() {
userDao.save();
}
}
自定义监听类和工具类
监听类
public class ContextLoaderListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
String contextConfigLocation = servletContext
.getInitParameter("contextConfigLocation");
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext(contextConfigLocation);
servletContext.setAttribute("applicationContext",applicationContext);
System.out.println("容器已经创建完毕。。。。。。");
}
public void contextDestroyed(ServletContextEvent sce) {
}
}
工具类
public class WebApplicationContextUtils {
public static ApplicationContext getAttribute(ServletContext servletContext){
return (ApplicationContext) servletContext.getAttribute("applicationContext");
}
}
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">
<!--加载外部的properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置Dao-->
<bean id="userDao" class="com.feitian.component.dao.impl.UserDaoImpl"></bean>
<!--配置service-->
<bean id="userService" class="com.feitian.component.servicce.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
</beans>
当然此处配置的数据库暂时没有用。
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_4_0.xsd" version="4.0">
<!--将applicationContext.xml放入ServletContext,方便自定义监听类初始化该文件。-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--监听器启动时要加载定义监听类-->
<listener>
<listener-class>com.feitian.component.web.listener.ContextLoaderListener</listener-class>
</listener>
<!--将UserServlet加载到web容器中-->
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.feitian.component.web.servlet.UserServlet</servlet-class>
</servlet>
<!--映射UserServlet为userServlet-->
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/userServlet</url-pattern>
</servlet-mapping>
</web-app>
web层
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
ApplicationContext applicationContext =
WebApplicationContextUtils.getAttribute(servletContext);
UserService userService = applicationContext.getBean(UserService.class);
userService.save();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doGet(req,resp);
}
}
上述案例基本上完整的演绎了整个tomcat服务器启动并加载spring容器的过程,从下节小猿正式开始复习springMVC。
还没有评论,来说两句吧...