springmvc 中配置aop


  之前自己搭建了springmvc+spring+mybaits/hibernate 的框架,並在applicationcontext.xml中配置了aop,但 發現aop根本不生效,而不用框架的話則可以生效。

  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" 
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-2.5.xsd 
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="springframe"></context:component-scan>
<context:property-placeholder location="classpath:/configFile/c3p0.properties" ignore-unresolvable="true"/>

<bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<property name="driverClass" value="${dataSource.driverClassName}"></property>
<property name="jdbcUrl" value="${dataSource.url}"></property>
<property name="user" value="${dataSource.username}"></property>
<property name="password" value="${dataSource.password}"></property>

</bean>

<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:mapper/*_mapper.xml"/>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
<property name="basePackage" value="springframe.dao" /> 
</bean>

<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
<aop:config>
<aop:aspect ref="aopBean" >
<aop:pointcut expression="execution(* springframe.service.EmployeeService.*(..))" id="pointcut"/>
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
<aop:around method="around" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config> 

<!-- 文件上傳 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5242880"/>
</bean>

</beans>

 

 

springmvc的配置文件,spring-servlet.xml的配置如下

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
<!-- 啟動注解驅動的Spring MVC功能,注冊請求url和注解POJO類方法的映射--> 
<mvc:annotation-driven /> 
<mvc:resources location="/resources/" mapping="/resources/**"/> 
<!-- 啟動包掃描功能,以便注冊帶有@Controller、@Service、@repository、@Component等注解的類成為spring的bean --> 
<context:component-scan base-package="springframe" /> 

<!-- 對模型視圖名稱的解析,在請求時模型視圖名稱添加前后綴 --> 
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".jsp" />

</beans>

 

觀察發現,在application.xml和spring-servlet.xml中都配置了

<context:component-scan base-package="springframe" />

 

 這樣的話,aop就不生效了

 

 問題: aop是配置在application.xml中的,容器啟動的時候開始掃描,此時本來aop是可以生效的,但在spring-servlet.xml中又再次掃描了一次指定包下的類,但spring-servlet.xml只負責controller,其它的一概不管,這樣就導致了aop與service關聯的實效,從而導致,aop不生效。

 

解決辦法:spring容器正常掃描包,不過一般不讓它掃描controller,因為springmvc還要再次掃描controller;springmvc配置只讓它掃描controller,這樣就避免了aop類與service失去關聯,避免aop不生效。

 

----------初次發表博客,請多關照。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM