Interesting things
有2天没有写diary report了,因为这几天都在火车上颠簸。上个星期五晚上9点的火车从广州到武汉,星期日在学校考试,然后晚上9点的火车又匆忙的赶回广州。今天是8点30赶到的广州,很累。今天老大给我布置了一个新的Task,实现FastDFS的集群和负载均衡。看来今天是要加班的,FastDFS对我来说是一个未知的领域,Spring Security 和 Filter、Interceptor的demo还没有写出来。(目测又要delay)我的时间计划表是每天6点30起床,晚上加班到10点(正常是6点),笨鸟先飞。
What did you do today
- 可插拔性,在软件系统的设计中,可插拔是一个重要特性。它意味着给系统添加新功能的时候(或者将原来功能的实现替换成新的实现而保持接口不变),不改变系统已有的功能。
- CMD (Common Module Definition):公共模块定义,对于模块的依赖,CMD是延迟执行,推崇依赖就近。
今天安装VMware Workstation,这个玩意大学上嵌入式的时候用过(设置ip和掩码:ifconfig eth0 192.168.1.2 netmask 255.255.255.0就是这样用,设置的ip是一次性,重启虚拟机后就失效, 设置网关:route add default gw 192.168.1.1),可是在我自己的笔记本我一直用Virtural Box。用起来Vmware还真不习惯,首先遇到一个问题就是虚拟机ping不同主机(真尼玛蛋疼),所以记录一下。
- 进入虚拟机设置,把网络连接设置为桥接模式。
- 点“高级”按钮,记录我们的MAC地址00:0C:29:67:76:2F
- 如果/etc/sysconfig/network-scripts目录没有ifcfg-eth0文件,那么我们就创建ifcfg-eth0文件,cd /etc/sysconfig/network-scripts, touch ifcfg-eth0
vi ifcfg-eth0 ,对这个文件进行编辑。具体的编辑内容可以参考图片。
- vi /etc/resolv.conf 进入resolv.conf文件设置dns。
vim /etc/rc.d/rc.local,配置ifup eth0,让开始时重启eth0。
重启网络服务,service network restart
- 我们满怀期待的输入ping 192.168.12.61(这个是主机地址),然后结果啪啪啪打脸,根本就ping不通嘛。
- vi /etc/resolv.conf 进入resolv.conf文件设置dns。
- 冥思苦想一下,发现是本地防火墙开启的问题,那么我们关掉防火墙再试一试。果然是防火墙的问题,草他妈。
- 我们本地的防火墙肯定不能关闭,那么有没有一种策略,能实现虚拟机ping通主机同时又保证本地防火墙保持开启状态呢?肯定是有的,打开Windows防火墙,找到高级设置。
- 我们在入站规则中找到“文件和打印机共享(回显请求 - ICMPv4-In)”,然后启动该规则即可解决问题。
- 但是我们又发现一个问题,我们ping不通www.baidu.com,这个问题就很操蛋了。
- 我们只需要把nameserver 设置为 114.114.114.114就能解决虚拟机ping不通外网的问题。
- 进入虚拟机设置,把网络连接设置为桥接模式。
关于这个问题,我们接着上一篇博客讲,我上一篇博客的观点出自于使用@Controller注解为什么要配置,这篇博客作者的观点是”要使用spring mvc中的@Controller注解,就必须要配置,否则org.springframework.web.servlet.DispatcherServlet无法找到控制器并把请求分发到控制器。” 当我刚开始学习SpringMVC的时候,就看过这篇文章,这篇文章某些观点的确解决了我的一些开发问题,但是我上面列举的作者观点就错的有点离谱。 下面我就用代码去证明这个观点是错的!
- 我们在spring-servlet注释这一行代码。
- 定义一个MvcAnnotationDrivenTestController
- 我们在spring-servlet注释这一行代码。
@RequestMapping("/mvc")
@Controller
public class MvcAnnotationDrivenTestController {
@RequestMapping(value = "/testMvcAnnotationDriven", method = RequestMethod.GET)
public ModelAndView testMvcAnnotationDriven() {
return new ModelAndView("mvc_annotation_driven_test");
}
}
- 在/WEB-INF/jsp/下面,定义mvc_annotation_driven_test.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>mvc_annotation_driven_test</title>
</head>
<body>
hello, this is mvc_annotation_driven_test
</body>
</html>
- 我们启动tomcat,输入http://localhost:8080/mvc/testMvcAnnotationDriven,我们就证明了”使用spring mvc中的@Controller注解,就必须要配置” 这个观点是错的了。
- 接下来我们在定义一个TestController,我们可以提供一个问题:“如果不在spring-servlet文件中配置,我们能成功的去访问http://localhost:8080/test/interceptor1这个链接吗?” 答案是不能访问!!!
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/interceptor1")
public String testInterceptor1(HttpSession session) {
session.setAttribute("test1", "interceptor1");
session.invalidate();
return "testInterceptor1";
}
@GetMapping("/interceptor2")
public String testInterceptor2(HttpSession session) {
session.setAttribute("test2", "interceptor2");
session.invalidate();
return "testInterceptor2";
}
}
* 会抛出"[20:02:37:859] [WARN] - org.springframework.web.servlet.DispatcherServlet.noHandlerFound(DispatcherServlet.java:1176) - No mapping found for HTTP request with URI [/test/interceptor1] in DispatcherServlet with name 'filter_interceptor_mvc'"这个错误,造成404。
* 我们可以去官方的一些文档找到答案。其实是毫无意义的。它声明了对注解驱动(@RequestMapping, @Controller)的mvc控制器的显式支持,尽管对这些的支持是默认行为。
<mvc:annotation-driven /> is actually rather pointless. It declares explicit support for annotation-driven MVC controllers (i.e.@RequestMapping, @Controller, etc), even though support for those is the default behaviour.
- 翻译后的意思就是:我的建议总是声明,但是不要打扰,除非你想通过Jackson来支持Json。
My advice is to always declare <context:annotation-config>, but don't bother with <mvc:annotation-driven /> unless you want JSON support via Jackson.
我想上面的解释能解开你们心中的疑惑。美滋滋!
接下来的篇幅会讲Listener、Filter、Interceptor的demo(终于可以还债了)。
- 创建TestInterceptor1、TestInterceptor2。
@Slf4j
public class TestInterceptor1 extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("TestInteceptor1 preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
log.info("TestInterceptor1 postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
log.info("TestInterceptor1 afterCompletion");
}
}
@Slf4j
public class TestInterceptor2 extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("TestInterceptor2 preHandle");
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
log.info("TestInterceptor2 postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
log.info("TestInterceptor2 afterCompletion");
}
}
- 创建TestFilter1、TestFilter2。
@Slf4j
public class TestFilter1 extends OncePerRequestFilter {
private String username;
private String password;
public TestFilter1() {}
public TestFilter1(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
log.info("TestFilter1 doFilterInternal started, username = {} , password = {} ", username, password);
filterChain.doFilter(httpServletRequest, httpServletResponse);
log.info("TestFilter1 doFilterInternal end, username = {} , password = {}", username, password);
}
}
@Slf4j
public class TestFilter2 extends OncePerRequestFilter {
private String username;
private String password;
public TestFilter2() {}
public TestFilter2(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
log.info("TestFilter2 doFilterInternal started");
filterChain.doFilter(httpServletRequest, httpServletResponse);
log.info("TestFilter2 doFilterInternal end");
}
}
- 创建ContextListener、RequestListener、RequestAttributeListener、SessionListener。
@Slf4j
public class ContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
log.info("context initialized");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
log.info("context destroyed");
}
}
@Slf4j
public class RequesListener implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
log.info("request destroyed");
}
@Override
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
log.info("request initialized");
}
}
@Slf4j
public class RequestAttributeListener implements ServletRequestAttributeListener {
@Override
public void attributeAdded(ServletRequestAttributeEvent servletRequestAttributeEvent) {
log.info("attribute added");
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent servletRequestAttributeEvent) {
log.info("attribute removed");
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent servletRequestAttributeEvent) {
log.info("attribute replaced");
}
}
@Slf4j
public class SessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
log.info("session created");
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
log.info("session destroyed");
}
}
- 在web.xml配置Filter、Listener
<?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">
<display-name>filter_interceptor</display-name>
<welcome-file-list>
<welcome-file>cmazxiaoma.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-config.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextCleanupListener</listener-class>
</listener>
<listener>
<listener-class>com.cmazxiaoma.demo.listener.ContextListener</listener-class>
</listener>
<listener>
<listener-class>com.cmazxiaoma.demo.listener.RequesListener</listener-class>
</listener>
<listener>
<listener-class>com.cmazxiaoma.demo.listener.RequestAttributeListener</listener-class>
</listener>
<listener>
<listener-class>com.cmazxiaoma.demo.listener.SessionListener</listener-class>
</listener>
<servlet>
<servlet-name>filter_interceptor_mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<init-param>
<param-name>detectAllHandlerExceptionResolvers</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>filter_interceptor_mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>testFilter1</filter-name>
<filter-class>com.cmazxiaoma.demo.filter.TestFilter1</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>username</param-name>
<param-value>testFilter1</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>123</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>testFilter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>testFilter2</filter-name>
<filter-class>com.cmazxiaoma.demo.filter.TestFilter2</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>username</param-name>
<param-value>testFilter2</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>456</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>testFilter2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
- 在servlet-servlet中配置Interceptor。
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/test/interceptor1"/>
<bean class="com.cmazxiaoma.demo.interceptor.TestInterceptor1" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/test/interceptor2"/>
<bean class="com.cmazxiaoma.demo.interceptor.TestInterceptor2" />
</mvc:interceptor>
</mvc:interceptors>
- 我们输入http://localhost:8080/test/interceptor1,观看控制台打出的log和返回的视图。
[21:19:03:666] [INFO] - com.cmazxiaoma.demo.listener.RequesListener.requestInitialized(RequesListener.java:18) - request initialized
[21:19:03:666] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:03:666] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:03:667] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:03:667] [INFO] - com.cmazxiaoma.demo.filter.TestFilter1.doFilterInternal(TestFilter1.java:44) - TestFilter1 doFilterInternal started, username = testFilter1 , password = 123
[21:19:03:667] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:03:667] [INFO] - com.cmazxiaoma.demo.filter.TestFilter2.doFilterInternal(TestFilter2.java:42) - TestFilter2 doFilterInternal started
[21:19:03:680] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:03:680] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:03:680] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:03:681] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:03:681] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:03:681] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:03:685] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:03:686] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:03:686] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:03:691] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:03:691] [INFO] - com.cmazxiaoma.demo.interceptor.TestInterceptor1.preHandle(TestInterceptor1.java:15) - TestInteceptor1 preHandle
[21:19:03:705] [INFO] - com.cmazxiaoma.demo.listener.SessionListener.sessionDestroyed(SessionListener.java:18) - session destroyed
[21:19:03:718] [INFO] - com.cmazxiaoma.demo.interceptor.TestInterceptor1.postHandle(TestInterceptor1.java:22) - TestInterceptor1 postHandle
[21:19:03:718] [INFO] - com.cmazxiaoma.demo.interceptor.TestInterceptor1.afterCompletion(TestInterceptor1.java:27) - TestInterceptor1 afterCompletion
[21:19:03:719] [INFO] - com.cmazxiaoma.demo.filter.TestFilter2.doFilterInternal(TestFilter2.java:44) - TestFilter2 doFilterInternal end
[21:19:03:719] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeRemoved(RequestAttributeListener.java:18) - attribute removed
[21:19:03:719] [INFO] - com.cmazxiaoma.demo.filter.TestFilter1.doFilterInternal(TestFilter1.java:46) - TestFilter1 doFilterInternal end, username = testFilter1 , password = 123
[21:19:03:719] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeRemoved(RequestAttributeListener.java:18) - attribute removed
[21:19:03:719] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeRemoved(RequestAttributeListener.java:18) - attribute removed
[21:19:03:719] [INFO] - com.cmazxiaoma.demo.listener.RequesListener.requestDestroyed(RequesListener.java:13) - request destroyed
- 我们输入http://localhost:8080/test/interceptor2,控制台打出的log和返回的视图。
[21:19:46:054] [INFO] - com.cmazxiaoma.demo.listener.RequesListener.requestInitialized(RequesListener.java:18) - request initialized
[21:19:46:055] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:46:056] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:46:056] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:46:056] [INFO] - com.cmazxiaoma.demo.filter.TestFilter1.doFilterInternal(TestFilter1.java:44) - TestFilter1 doFilterInternal started, username = testFilter1 , password = 123
[21:19:46:056] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:46:057] [INFO] - com.cmazxiaoma.demo.filter.TestFilter2.doFilterInternal(TestFilter2.java:42) - TestFilter2 doFilterInternal started
[21:19:46:057] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:46:057] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:46:058] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:46:058] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:46:059] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:46:059] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:46:059] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:46:060] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:46:060] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:46:061] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeAdded(RequestAttributeListener.java:13) - attribute added
[21:19:46:061] [INFO] - com.cmazxiaoma.demo.interceptor.TestInterceptor2.preHandle(TestInterceptor2.java:15) - TestInterceptor2 preHandle
[21:19:46:061] [INFO] - com.cmazxiaoma.demo.filter.TestFilter2.doFilterInternal(TestFilter2.java:44) - TestFilter2 doFilterInternal end
[21:19:46:062] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeRemoved(RequestAttributeListener.java:18) - attribute removed
[21:19:46:062] [INFO] - com.cmazxiaoma.demo.filter.TestFilter1.doFilterInternal(TestFilter1.java:46) - TestFilter1 doFilterInternal end, username = testFilter1 , password = 123
[21:19:46:062] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeRemoved(RequestAttributeListener.java:18) - attribute removed
[21:19:46:062] [INFO] - com.cmazxiaoma.demo.listener.RequestAttributeListener.attributeRemoved(RequestAttributeListener.java:18) - attribute removed
[21:19:46:063] [INFO] - com.cmazxiaoma.demo.listener.RequesListener.requestDestroyed(RequesListener.java:13) - request destroyed
通过对比log和返回的视图,我们发现了Interceptor1执行了preHandler()、postHandler()、afterCompletion()这三个方法,而Interceptor2只执行了preHandler()这个方法。通过比较它们的源码,我们发现了Interceptor1的preHandler()返回的是true,而Interceptor2的preHandler()返回的是false。而第一个url我们拦截了请求,返回了视图,第二个url我们拦截了请求,返回没有视图。
preHandle方法是进行处理拦截用的,该方法将在Controller处理之前调用,Interceptor拦截器是链式的,并且可以中断,当返回false的时候整个请求结束。
当preHandle返回true时,postHandle()才会执行,在Controller的方法调用之后执行,但是它会在DispatcherServlet进行视图的渲染之前执行。
当preHandle返回ture才会执行,该方法将在整个请求完成之后,也就是DispatcherServlet渲染视完毕才执行,主要作用是情理资源。
从启动日志来看,Listener > Filter > Interceptor > Servlet,自定义Listener和Filter很简单,也不需要讲。
What to do tomorrow
- 明天去看FastDFS的集群和负载均衡,Spring Security的demo又要delay了。
Summary
笨鸟先飞