打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
spring_mvc_之AOP

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>gjkonline</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();

     }

}

}

这样全部配置完成了!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
spring MVC 项目 WEB-INF下的jsp不能加载css文件
SpringMVC4零配置--web.xml
在Spring 2中整合DWR 2
Java学习之Hessian通信基础
springmvc3.2.2+hibernate4.2.1(一)
通过日志监控并收集 Java 应用程序性能数据
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服