SSM 利用SpringMVC的AOP来实现后台系统的操作日志记录(Controller)

一、

  1. 引入cglib.jar包
<dependency>
			    <groupId>cglib</groupId>
			    <artifactId>cglib</artifactId>
			    <version>3.2.8</version>
			</dependency>

 2、 dispatcherServlet-servlet.xml添加配置

<!-- 通知spring使用cglib而不是jdk的来生成代理方法 AOP可以拦截到Controller -->
<aop:aspectj-autoproxy proxy-target-class="true" />


    <context:component-scan base-package="com.qding">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>

3、applicationContext.xml

    <context:component-scan base-package="com.qding">           
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

 

二、问题描述

1、当使用Spring AOP对Controller层的Controller类的方法进行切面拦截,不起作用。AOP配置没有任何问题。

    AOP之所以有的人说拦截不到Controller,原因是该注解的Controller已被spring容器内部代理了。我们只要把它交给cglib代理就可以了。Spring MVC的配置文件dispatcherServlet-servlet.xml:

   dispatcherServlet-servlet.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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 
	    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!-- 登录拦截器 
    <mvc:interceptors>
       <bean >
       
       </bean>
    </mvc:interceptors>-->
     <context:annotation-config />
     
    <context:component-scan base-package="com.yc">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>
     
     <aop:aspectj-autoproxy proxy-target-class="true"/>
	 <bean class="com.yc.controller.aop.LogAspect" />
	 
	 
    
	<!--SpringMVC的配置文件,包含网站跳转逻辑的控制,配置  -->
	<context:component-scan base-package="com.yc.controller" use-default-filters="false">
		<!--只扫描控制器。  -->
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- Spring的配置文件,这里主要配置和业务逻辑有关的 -->
	<!--=================== 数据源,事务控制,xxx ================ -->
	<context:property-placeholder location="classpath:properties/*.properties" />	
	<!-- 静态资源解析 -->
	<mvc:resources location="/webframes/" mapping="/webframes/**"/>	
	<!--配置视图解析器,方便页面返回  -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!--两个标准配置  -->
	<!-- 将springmvc不能处理的请求交给tomcat -->
	<mvc:default-servlet-handler/>
	<!-- 能支持springmvc更高级的一些功能,JSR303校验,快捷的ajax...映射动态请求 -->
	<mvc:annotation-driven/>
</beans>

applicationContext.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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
	<context:component-scan base-package="com.yc">
		 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
	</context:component-scan>
		
	<!-- Spring的配置文件,这里主要配置和业务逻辑有关的 -->	
	<context:property-placeholder location="classpath:properties/*.properties" />
	<!--sql server 数据源配置 -->
	<!-- org.springframework.jdbc.datasource.DriverManagerDataSource -->
	<bean id="pooledDataSource" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="filters" value="${filters}" />		
		<property name="maxActive" value="${maxActive}" />		
		<property name="initialSize" value="${initialSize}" />		
		<property name="maxWait" value="${maxWait}" />
		<property name="minIdle" value="${minIdle}" />		
		<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
		<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
		<property name="validationQuery" value="${validationQuery}" />
		<property name="testWhileIdle" value="${testWhileIdle}" />
		<property name="testOnBorrow" value="${testOnBorrow}" />
		<property name="testOnReturn" value="${testOnReturn}" />
		<property name="poolPreparedStatements" value="${poolPreparedStatements}" />
		<property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" />
	</bean>

	<!--================== 配置和MyBatis的整合=============== -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 指定mybatis全局配置文件的位置 -->
		<property name="configLocation" value="classpath:spring/mybatis-config.xml"></property>
		<property name="dataSource" ref="pooledDataSource"></property>
		<!-- 指定mybatis,mapper文件的位置 -->
		<!-- <property name="mapperLocations" value="classpath:mapper/*.xml"></property> -->
	</bean>

	<!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!--扫描所有dao接口的实现,加入到ioc容器中 -->
		<property name="basePackage" value="com.yc.mapper"></property>
	</bean>

	<!-- 配置一个可以执行批量的sqlSession -->
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
		<constructor-arg name="executorType" value="SIMPLE"></constructor-arg>
	</bean>
	<!--============================================= -->

	<!-- ===============事务控制的配置 ================ -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!--控制住数据源 -->
		<property name="dataSource" ref="pooledDataSource"></property>
	</bean>
		
	<!--开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式) -->
	<aop:config>
		<!-- 切入点表达式 -->
		<aop:pointcut expression="execution(* com.yc.service..*(..))"
			id="txPoint" />
		<!-- 配置事务增强 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint" />
	</aop:config>

	<!--配置事务增强,事务如何切入 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>			
			<tx:method name="insert*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="edit*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="new*" propagation="REQUIRED" />  
            <tx:method name="set*" propagation="REQUIRED" />  
            <tx:method name="remove*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="change*" propagation="REQUIRED" />  
            <tx:method name="check*" propagation="REQUIRED" />  
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="*" propagation="REQUIRED" read-only="true" /> 			
		</tx:attributes>
	</tx:advice>
	<!-- Spring配置文件的核心点(数据源、与mybatis的整合,事务控制) -->
			
	<!-- 国际化配置 -->
	<bean id="messageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basenames">
			<list>
				<value>properties/messages</value>
			</list>
		</property>
		<property name="defaultEncoding" value="utf-8" />
	</bean>
	
	
	
</beans>

 3/Spring Aop 执行两次原因(1)

切面bean重复定义
 类中使用了@Component的同时又在springmvc.xml中配置了<bean id="log**" class="com.demo.log.**Aspect"></bean>

方法: 删除两个定义之一

 

 

参考1

cglib

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章