有2天沒有寫diary report了,由於這幾天都在火車上顛簸。上個星期五晚上9點的火車從廣州到武漢,星期日在學校考試,而後晚上9點的火車又匆忙的趕回廣州。今天是8點30趕到的廣州,很累。今天老大給我佈置了一個新的Task,實現FastDFS的集羣和負載均衡。看來今天是要加班的,FastDFS對我來講是一個未知的領域,Spring Security 和 Filter、Interceptor的demo尚未寫出來。(目測又要delay)個人時間計劃表是天天6點30起牀,晚上加班到10點(正常是6點),笨鳥先飛。html
###What did you do todayjava
可插拔性,在軟件系統的設計中,可插拔是一個重要特性。它意味着給系統添加新功能的時候(或者將原來功能的實現替換成新的實現而保持接口不變),不改變系統已有的功能。web
CMD (Common Module Definition):公共模塊定義,對於模塊的依賴,CMD是延遲執行,推崇依賴就近。spring
今天安裝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不一樣主機(真尼瑪蛋疼),因此記錄一下。vim
進入虛擬機設置,把網絡鏈接設置爲橋接模式。 tomcat
點「高級」按鈕,記錄咱們的MAC地址00:0C:29:67:76:2F bash
若是/etc/sysconfig/network-scripts目錄沒有ifcfg-eth0文件,那麼咱們就建立ifcfg-eth0文件,cd /etc/sysconfig/network-scripts, touch ifcfg-eth0網絡
vi ifcfg-eth0 ,對這個文件進行編輯。具體的編輯內容能夠參考圖片。 session
vi /etc/resolv.conf 進入resolv.conf文件設置dns。 mvc
vim /etc/rc.d/rc.local,配置ifup eth0,讓開始時重啓eth0。
重啓網絡服務,service network restart
咱們滿懷期待的輸入ping 192.168.12.61(這個是主機地址),而後結果啪啪啪打臉,根本就ping不通嘛。
左思右想一下,發現是本地防火牆開啓的問題,那麼咱們關掉防火牆再試一試。果真是防火牆的問題,草他媽。
咱們本地的防火牆確定不能關閉,那麼有沒有一種策略,能實現虛擬機ping通主機同時又保證本地防火牆保持開啓狀態呢?確定是有的,打開Windows防火牆,找到高級設置。
咱們在入站規則中找到「文件和打印機共享(回顯請求 - ICMPv4-In)」,而後啓動該規則便可解決問題。
可是咱們又發現一個問題,咱們ping不通www.baidu.com,這個問題就很操蛋了。
咱們只須要把nameserver 設置爲 114.114.114.114就能解決虛擬機ping不通外網的問題。
關於<mvc:annotation-driven />這個問題,咱們接着上一篇博客講,我上一篇博客的觀點出自於使用@Controller註解爲何要配置<mvc:annotation-driven />,這篇博客做者的觀點是"要使用spring mvc中的@Controller註解,就必需要配置<mvc:annotation-driven />,不然org.springframework.web.servlet.DispatcherServlet沒法找到控制器並把請求分發到控制器。" 當我剛開始學習SpringMVC的時候,就看過這篇文章,這篇文章某些觀點的確解決了個人一些開發問題,可是我上面列舉的做者觀點就錯的有點離譜。 下面我就用代碼去證實這個觀點是錯的!
@RequestMapping("/mvc")
@Controller
public class MvcAnnotationDrivenTestController {
@RequestMapping(value = "/testMvcAnnotationDriven", method = RequestMethod.GET)
public ModelAndView testMvcAnnotationDriven() {
return new ModelAndView("mvc_annotation_driven_test");
}
}
複製代碼
<%@ 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>
複製代碼
@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。
複製代碼
<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.
複製代碼
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(終於能夠還債了)。
@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");
}
}
複製代碼
@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");
}
}
複製代碼
@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");
}
}
複製代碼
<?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>
複製代碼
<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>
複製代碼
[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
複製代碼
[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
###Summary
笨鳥先飛