导包,
spring 3.1的包全导了 + 依赖包 commons-logging-1.1.1.jar + servlet-api.jar
AOP需要的几个包 aopalliance.jar + aspectjrt.jar + aspectjweaver.jar + cglib-nodep-2.1_3.jar
memcache 需要的包 java_memcached-release_2.6.1.jar +依赖包 slf4j-api-1.6.1.jar + slf4j-simple-1.6.1.jar + commons-pool-1.5.6.jar
C3P0 需要的包 c3p0-0.9.1.2.jar 数据库包 mysql-connector-java-5.1.12-bin.jar
WEB.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>springDemo</display-name> <welcome-file-list> <welcome-file>index.do</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/demo.xml</param-value> </context-param> <servlet> <servlet-name>springContent</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/demo.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springContent</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <filter> <filter-name>DelegatingFilterProxy</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetBeanName</param-name> <param-value>filter_saveuserinfo</param-value> </init-param> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>DelegatingFilterProxy</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> </web-app>
spring配置文件demo.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:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"> <context:annotation-config /> <context:component-scan base-package="com.netel" /> <aop:aspectj-autoproxy /> <!-- C3P0连接池配置 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/keyword_list" /> <property name="user" value="root" /> <property name="password" value="123456" /> </bean> <bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <!-- memcache缓存池 --> <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" destroy-method="shutDown"> <constructor-arg> <value>neeaMemcachedPool</value> </constructor-arg> <property name="servers"> <list> <value>127.0.0.1:12111</value> </list> </property> <property name="initConn"> <value>20</value> </property> <property name="minConn"> <value>10</value> </property> <property name="maxConn"> <value>50</value> </property> <property name="maintSleep"> <value>30000</value> </property> <property name="nagle"> <value>false</value> </property> <property name="socketTO"> <value>3000</value> </property> </bean> <!-- 缓存客服端 --> <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient"> <constructor-arg> <value>neeaMemcachedPool</value> </constructor-arg> </bean> </beans>
package com.netel.web; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.danga.MemCached.MemCachedClient; import com.netel.dao.GetKeyword; @Controller public class Controller_index { @Resource(name="memcachedClient") private MemCachedClient client; @Resource(name="keyword_dao") private GetKeyword dao; public void setClient(MemCachedClient client) { this.client = client; } public void setDao(GetKeyword dao) { this.dao = dao; } @RequestMapping("/index.do") public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView v = new ModelAndView("index.jsp"); System.out.println("进入controller"); String tmp1 = "controller " + client; String tmp2 = "controller " + dao.getAllData(); String tmp3 = "controller " +request.getParameter("name"); System.out.println(tmp1); System.out.println(tmp2); System.out.println(tmp3); v.addObject("tmp1", tmp1); v.addObject("tmp2", tmp2); v.addObject("tmp3", tmp3); return v; } }
filter
package com.netel.web; import java.io.IOException; import javax.annotation.Resource; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.springframework.stereotype.Component; import com.danga.MemCached.MemCachedClient; import com.netel.dao.GetKeyword; @Component("filter_saveuserinfo") public class Filter_saveUserInfo implements Filter { @Resource(name="memcachedClient") private MemCachedClient client; @Resource(name="keyword_dao") private GetKeyword dao; public void setClient(MemCachedClient client) { this.client = client; } public void setDao(GetKeyword dao) { this.dao = dao; } /** * Default constructor. */ public Filter_saveUserInfo() { } /** * @see Filter#destroy() */ public void destroy() { } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("进入filter\t"+ client + "\t" + dao); chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { } }
package com.netel.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import javax.annotation.Resource; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.ResultSetExtractor; import org.springframework.stereotype.Repository; @Repository("keyword_dao") public class GetKeyword_imple implements GetKeyword { @Resource(name="jdbc") private JdbcTemplate jdbc; public void setJdbc(JdbcTemplate jdbc) { this.jdbc = jdbc; } @Override public HashMap<String, String> getAllData() { String sql = "SELECT keyword, count( keyword ) as qty FROM keyword_list GROUP BY keyword ORDER BY qty DESC"; return (HashMap<String, String>) jdbc.query(sql, new ResultSetExtractor<HashMap<String, String>>(){ @Override public HashMap<String, String> extractData(ResultSet rs) throws SQLException, DataAccessException { HashMap<String, String> map = new HashMap<String, String>(); while (rs.next()) { map.put(rs.getString("keyword"), rs.getString("qty")); } return map; } }); } }
AOP
package com.netel.service; import javax.annotation.Resource; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; import com.danga.MemCached.MemCachedClient; import com.netel.dao.GetKeyword; @Aspect @Component public class AOP_loginfo { @Resource(name="memcachedClient") private MemCachedClient client; @Resource(name="keyword_dao") private GetKeyword dao; public void setClient(MemCachedClient client) { this.client = client; } public void setDao(GetKeyword dao) { this.dao = dao; } /** * 所有带RequestMapping注解的方法 */ private final static String el = "@annotation(org.springframework.web.bind.annotation.RequestMapping)"; @Before(el) public void before() { System.out.println("before\t" + client + "\t" + dao); } @After(el) public void after() { System.out.println("after\t" + client + "\t" + dao); } @Around(el) public Object around(ProceedingJoinPoint p) { for (Object obj : p.getArgs()) { System.out.println("参数:" + obj); } Object ob = null; try { System.out.println("around前\t" + client + "\t" + dao); ob = p.proceed(); System.out.println("around后\t" + client + "\t" + dao); } catch (Throwable e) { e.printStackTrace(); } return ob; } @AfterThrowing(value = el, throwing="e") public void throwing(Exception e){ System.out.println("出异常了" + client + "\t" + dao + e); } }
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> wca..........<br> ${tmp1 }<br> ${tmp2 }<br> ${tmp3 }<br> <%=request.getAttribute("tmp1") %> <br> <%=request.getAttribute("tmp2") %> <br> <%=request.getAttribute("tmp3") %> <br> </body> </html>
进入filter com.danga.MemCached.MemCachedClient@e6529c com.netel.dao.GetKeyword_imple@1399ae5 before com.danga.MemCached.MemCachedClient@e5f1d com.netel.dao.GetKeyword_imple@1869929 参数:org.apache.catalina.connector.RequestFacade@1b15387 参数:org.apache.catalina.connector.ResponseFacade@e2da7a around前 com.danga.MemCached.MemCachedClient@e5f1d com.netel.dao.GetKeyword_imple@1869929 进入controller controller com.danga.MemCached.MemCachedClient@e5f1d controller {3=6, 2=4, AA=1, 1=11, 111=1, 4=1, ä¸=3} controller null after com.danga.MemCached.MemCachedClient@e5f1d com.netel.dao.GetKeyword_imple@1869929 around后 com.danga.MemCached.MemCachedClient@e5f1d com.netel.dao.GetKeyword_imple@1869929
wca.......... controller com.danga.MemCached.MemCachedClient@e5f1d controller {3=6, 2=4, AA=1, 1=11, 111=1, 4=1, ä¸=3} controller null controller com.danga.MemCached.MemCachedClient@e5f1d controller {3=6, 2=4, AA=1, 1=11, 111=1, 4=1, ä¸=3} controller null
http://localhost:8080/AnnotationDemo/index.do?name=aaaJSP页面
wca.......... controller com.danga.MemCached.MemCachedClient@e5f1d controller {3=6, 2=4, AA=1, 1=11, 111=1, 4=1, ä¸=3} controller aaa controller com.danga.MemCached.MemCachedClient@e5f1d controller {3=6, 2=4, AA=1, 1=11, 111=1, 4=1, ä¸=3} controller aaa