关于spring mvc 配置aop的分享

http://www.360doc.com/content/13/0105/10/4280915_258300428.shtml
Spring MVC 的AOP配置:
第一,WEB.XML,这里跟正常的Spring MVC一样,
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>groups</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>
<description></description>
<display-name>GoodIndexChartServlet</display-name>
<servlet-name>GoodIndexChartServlet</servlet-name>
<servlet-class>com.servlet.GoodIndexChartServlet</servlet-class>
</servlet>

<servlet>
<description></description>
<display-name>GoodIndexInitServlet</display-name>
<servlet-name>GoodIndexInitServlet</servlet-name>
<servlet-class>com.servlet.GoodIndexInitServlet</servlet-class>
<!-- <load-on-startup>0</load-on-startup>-->
</servlet>
<servlet-mapping>
<servlet-name>GoodIndexChartServlet</servlet-name>
<url-pattern>/goodindexchart</url-pattern>
</servlet-mapping>
<!---->
<servlet-mapping>
<servlet-name>GoodIndexInitServlet</servlet-name>
<url-pattern>/goodindexInit</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>gjkonline</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.swf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.txt</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.xml</url-pattern>
</servlet-mapping>
<filter>
<description>no description</description>
<display-name>EncodingFilter</display-name>
<filter-name>EncodingFilter</filter-name>
<filter-class>
com.gjkonline.filter.EncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
    <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>RequestTokenServlet</servlet-name>
        <servlet-class>net.oauth.example.provider.servlets.RequestTokenServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>AuthorizationServlet</servlet-name>
        <servlet-class>net.oauth.example.provider.servlets.AuthorizationServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>AccessTokenServlet</servlet-name>
        <servlet-class>net.oauth.example.provider.servlets.AccessTokenServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>EchoServlet</servlet-name>
        <servlet-class>net.oauth.example.provider.servlets.EchoServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RequestTokenServlet</servlet-name>
        <url-pattern>/request_token</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>AuthorizationServlet</servlet-name>
        <url-pattern>/authorize</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>AccessTokenServlet</servlet-name>
        <url-pattern>/access_token</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>EchoServlet</servlet-name>
        <url-pattern>/echo</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>
第二、
Spring MVC 中web.xml红色部分的配置文件,文件名是gjkonline-servlet.xml,跟web.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy/>
<context:annotation-config />
<!-- Auto scan, declare the location path --> 
<context:component-scan base-package="com.gjkonline.springmvc.rest" /> 
<!-- Using annontation --> 
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- Resolve the view, declare the prefix and suffix --> 
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
p:prefix="/view/" p:suffix=".jsp" p:viewClass="org.springframework.web.servlet.view.JstlView" /> 

<bean id="multipartResolver" 
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" 
p:defaultEncoding="utf-8" />
</beans>
此文件中红色标注的一定要加,要不然AOP切点,切不进去,这跟其它的框架下的AOP配置最大的区别;
第三,
applicationContext.xml这个配置文件没什么特别之处,我把关于AOP的配置,都放在了applicationContext-aop.xml文件中,具体引入部分是,
<import resource="applicationContext-aop.xml"/>
第四、
applicationContext-aop.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
         
<context:annotation-config />
<context:component-scan base-package="com.gjkonline.aop" />
</beans>
第五:拦截器的JAVA类:
package com.gjkonline.aop;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.gjkonline.springmvc.rest.base.HomeBaseController;
import com.ibatis.dao.impl.CommonDAO;
@Component 
@Aspect
public class LogServiceXMLAop {
private CommonDAO commonDAO;
@Autowired
public void setCommonDAO(CommonDAO commonDAO) {
this.commonDAO = commonDAO;
}
    @Before("execution(* com.gjkonline.springmvc.rest.DataHomeController.*Fws(..))") 
public void beforeMethod(JoinPoint jp) {
    try{
Object[] args = jp.getArgs();
String user="";
String url="";
for (int i = 0; i < args.length; i++){
if(args[i] instanceof HttpServletRequest)
{
HttpServletRequest request= (HttpServletRequest)args[i];
user = new HomeBaseController().getRestUserId(request);
url=request.getRequestURI();
}
}
this.commonDAO.insertLog(user, "宏观接口调用", url, "", "class com.gjkonline.springmvc.rest.macro.DataHomeController");
    }
    catch(Exception e){
    e.printStackTrace();
    }
}
}
这样全部配置完成了!

猜你喜欢

转载自sunnyboo.iteye.com/blog/2058831