ssm 自定義aop不生效 不執行

 

在shtml

sm裏本身寫了個aop切面,經過註解配置,發現不生效,最後找到緣由,是spring配置時,只掃描了controller和service,並無掃描aop類java

applicationContent。xmlweb

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"  
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	           http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
			   http://www.springframework.org/schema/context
	           http://www.springframework.org/schema/context/spring-context-4.2.xsd
	           http://www.springframework.org/schema/tx
	           http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	           http://www.springframework.org/schema/aop
	           http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
	           http://www.springframework.org/schema/jdbc 
	           http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
	    
	           ">
<!-- <context:component-scan base-package="com.sunny" />
 -->	
 <context:component-scan base-package="com.sunny">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
 <!-- 加載資源文件 其中包含變量信息,必須在Spring配置文件的最前面加載,即第一個加載 -->
	<context:property-placeholder location="classpath:persistence-jdbc.properties" />
  
	 
	<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">  
        <property name="poolProperties">  
            <bean class="org.apache.tomcat.jdbc.pool.PoolProperties">  
                <property name="url" value="${jdbc.url}"/>  
                <property name="driverClassName" value="${jdbc.driver}"/>  
                <property name="username" value="${jdbc.username}"/>  
                <property name="password" value="${jdbc.password}"/>  
                <property name="jmxEnabled" value="true"/>  
                <property name="testWhileIdle" value="false"/>  
                <property name="testOnBorrow" value="true"/>  
                <property name="validationInterval" value="30000"/>  
                <property name="testOnReturn" value="false"/>  
                <property name="validationQuery" value="select 1"/>  
                <property name="timeBetweenEvictionRunsMillis" value="30000"/>  
                <property name="maxActive" value="50"/>  
                <property name="maxIdle" value="10"/>  
                <property name="initialSize" value="20"/>  
                <property name="maxWait" value="10000"/>  
                <property name="removeAbandonedTimeout" value="60"/>  
                <property name="minEvictableIdleTimeMillis" value="30000"/>  
                <property name="minIdle" value="10"/>  
                <property name="logAbandoned" value="true"/>  
                <property name="removeAbandoned" value="true"/>  
                <property name="jdbcInterceptors" value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"/>  
            </bean>  
        </property>  
    </bean>  
	
	
	  <bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"
        lazy-init="false" autowire="default" >
        	<property name="dataSource" ref="dataSource" />
    </bean>

	<!-- ========================================分隔線========================================= -->
	<!-- 配置Spring的事務管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 實例化sqlSessionFactory時須要使用上述配置好的數據源以及SQL映射文件 -->
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
		<property name="mapperLocations" value="classpath*:/com/sunny/model/mapper/*.xml" />
	</bean>	
	
	
	<bean id="log4jdbcInterceptor" class="net.sf.log4jdbc.DataSourceSpyInterceptor" />
	<bean id="interceptorTest" class="framework.InterceptorTest" />
	<bean id="dataSourceLog4jdbcAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="interceptorNames">
			<list>
				<value>log4jdbcInterceptor</value>
			</list>
		</property>
		<property name="beanNames">
			<list>
				<value>dataSource</value>
			</list>
		</property>
	</bean>
	

  <!-- 開啓shiro註解-->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
          depends-on="lifecycleBeanPostProcessor">
        <property name="proxyTargetClass" value="true" />
    </bean>

    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>
 
	
		
	<!-- 配置通知  暫時未啓用 -->
<!-- 	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED"/>
			<tx:method name="add*" propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="insert*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
			<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
			<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
		</tx:attributes>
	</tx:advice> -->
	
	<!-- 配置切面  暫時未啓用-->
<!-- 	<aop:config>
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.sunny.service.impl.*.*(..))"/>
	</aop:config> -->	

</beans>

 

 

init-servlet.xmlspring

<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	           http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
			   http://www.springframework.org/schema/context
	           http://www.springframework.org/schema/context/spring-context-4.2.xsd
	           http://www.springframework.org/schema/tx
	           http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	           http://www.springframework.org/schema/aop
	           http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
	           http://www.springframework.org/schema/mvc  
      		   http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
	<!-- 啓動自動掃描 該包下全部的Bean(@Controller) -->
	<context:component-scan base-package="com.sunny" />
<!-- 掃描aop的包  -->
	<context:component-scan base-package="framework.aop" />
	
	<context:annotation-config />
	<!-- 配置織入@Aspectj切面  -->
	<aop:aspectj-autoproxy proxy-target-class="true" />
	
	<mvc:interceptors>
           		 <bean class="framework.interceptors.UserSessionInterceptor"></bean>
	 </mvc:interceptors>
	
	<!-- mvc 配置 自動配置一些默認設置和註解驅動 -->
	<mvc:annotation-driven />
	<!-- 對於一些靜態資源的設置 -->
	<mvc:default-servlet-handler />

<!-- ============================== springframework.web.servlet.view ============================== -->
	<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"  >
	 <property name="order" value="0" />
	</bean>

	<!-- 根據客戶端的不一樣的請求決定不一樣的 view進行響應, 如 /blog/1.json /blog/1.xml -->
	<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"  >
		 
		<property name="viewResolvers">
			<list>
				<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
					<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
					<property name="contentType" value="text/html" />
					<property name="prefix" value="/jsp" />
					<property name="suffix" value=".jsp" />
				</bean>
			</list>
		</property>
		 <property name="order" value="1" />
	</bean>

	<!-- 默認的視圖解析器 在上邊的解析錯誤時使用 (默認使用html)- -->
	<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
		<property name="contentType" value="text/html" />
		<property name="prefix" value="/WEB-INF/pages" />
		<property name="suffix" value=".jsp" />
		 <property name="order" value="2" />
	</bean>

	<!-- json view -->
	<bean id="defaultJsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />

	<!-- 上傳文件的最大尺寸爲100MB,maxUploadSize屬性的限制不是針對單個文件,而是全部文件的容量之和 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="104857600" />
		<property name="maxInMemorySize" value="40960" />
	</bean>
	<!-- 在超出上傳文件限制時的跳轉頁面,此時尚未進入到Controller方法中 -->
	<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">/front/errorUploadSize</prop>
			</props>
		</property>
	</bean>
	
 

	<!-- 定義視圖解析器 -->
	<!-- <bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean> -->


	 <!-- 全局異常處理器 -->
	<!--<bean class="com.sunny.exception.ExceptionResolver"></bean> -->
</beans>
package framework.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class WxUserDzUserUpdateAspect {

	private static final Log logger = LogFactory.getLog(WxUserDzUserUpdateAspect.class);

	/**
	 * 對註釋有RequestMapping標記的方法,進行AOP切入攔截
	 */
	@Pointcut(" execution (* com.sunny.service.impl..*.*(..))")
	public void controllerPointcut() {

	}

	@After(value="controllerPointcut()")
	public void after(JoinPoint joinPoint) {
		System.out.println("---------------------------joinPoint");

	}

	@SuppressWarnings("unchecked")
	@Around("controllerPointcut()  ")
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("---------------------------joinPoint");
		return joinPoint.proceed(joinPoint.getArgs());
	}

	/**
	 * 異常統一處理
	 * 
	 * @Title: afterThrowing
	 * @Description:
	 * @author weifa.Yang
	 * @date 2016-8-3 下午04:19:57
	 * @param e
	 * @return void
	 */
	@AfterThrowing(pointcut = "controllerPointcut() ", throwing = "e")
	public void afterThrowing(JoinPoint joinPoint, Throwable e) {
		logger.error("展客[RPC],請求接口[" + joinPoint.getTarget().getClass().getName() + "],方法[" + joinPoint.getSignature().getName() + "],方法拋出異常:" + e.getMessage());
	}

	/**
	 * 返回值日誌記錄
	 * 
	 * @Title: afterReturning
	 * @Description:
	 * @author weifa.Yang
	 * @date 2016-8-3 下午04:20:05
	 * @param joinPoint
	 * @param result
	 * @return void
	 */
	@AfterReturning(pointcut = "controllerPointcut() ", returning = "result")
	public void afterReturning(JoinPoint joinPoint, Object result) {
		logger.info("響應展客[RPC]接口[" + joinPoint.getSignature().getName() + "],返回結果:");
	}
}

添加掃描後 ,能夠執行了sql

可是這樣有個隱患,就是springmvc配置了全路徑掃描,而不是最正確的只掃描controller,這就會致使沒有事務加強,因此我就改爲init-servlet。xml只掃描controller,結果這樣致使aop沒法生效。express

緣由是,Service的掃描在spring容器內,而不是在springmvc子容器內,配置aop應該在application。xml裏,在裏面加上        <aop:aspectj-autoproxy proxy-target-class="true"/>   開啓aop就能夠了apache

相關文章
相關標籤/搜索