切入Spring @RequestMapping

时间:2012-04-20 09:51:27

标签: spring mapping aop pointcut

我现在正试图在我的Spring项目上启用AOP。我希望在@RequestMapping完成后执行代码(会话清理)。这是我的 applicationContext.xml (只是加载引用的资源

<?xml version="1.0" encoding="UTF-8" standalone="no"?><beans 
xmlns="http://www.springframework.org/schema/beans"  
xmlns:aop="http://www.springframework.org/schema/aop"  
xmlns:context="http://www.springframework.org/schema/context"  
xmlns:jee="http://www.springframework.org/schema/jee" 
xmlns:tx="http://www.springframework.org/schema/tx" 
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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

    <!-- ******************************************************************** -->
    <!-- Include context files from different layers -->
    <!-- ******************************************************************** -->
    <import resource="classpath:appname-security-context.xml"/>
    <import resource="classpath:appname-service-context.xml"/>
    <import resource="classpath:appname-dao-context.xml"/>

</beans>

安全上下文:

<?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:security="http://www.springframework.org/schema/security" 
    xmlns:lang="http://www.springframework.org/schema/lang"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security    http://www.springframework.org/schema/security/spring-security-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<http auto-config="true">
<intercept-url pattern='/pages/login.jsp' />
<intercept-url pattern="/secure/*" access="IS_AUTHENTICATED_REMEMBERED" />
<intercept-url pattern="/admin/**" access="ROLE_ADMIN" />
<form-login login-page="/pages/login.jsp" authentication-failure-url="/pages/login.jsp?login_error=true"  /> 
        <logout logout-success-url="/pages/logout-redirect.jsp" invalidate-session="true" />
        <remember-me key="appnameRMKey" user-service-ref="userDetailsService" />
    </http>
    <authentication-manager alias="authenticationManager" >
    <authentication-provider user-service-ref='userDetailsService' >
        <password-encoder hash="plaintext"/>
    </authentication-provider>
    </authentication-manager>

    <beans:bean id="userDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
        <beans:property name="rolePrefix" value="ROLE_" />
        <beans:property name="dataSource" ref="springSecurityDataSource" />
        <beans:property name="usersByUsernameQuery" value="SELECT username,password,enabled FROM Users WHERE username = ?" />
        <beans:property name="authoritiesByUsernameQuery" value="SELECT u.username, a.authorityname FROM Users u JOIN Users_Authorities ua on u.id = ua.user_id JOIN Authorities a on ua.authorities_id = a.id WHERE u.username = ?" />
    </beans:bean>   

    <!-- ******************************************************************** -->
    <!-- Apply security for all beans where security was set -->
    <!-- ******************************************************************** -->

    <global-method-security jsr250-annotations="enabled" proxy-target-class = "true" secured-annotations="enabled">
        <protect-pointcut expression="execution(* appname.UsersDAO.*(..))" access="IS_AUTHENTICATED_REMEMBERED"/>
        <protect-pointcut expression="execution(* appname.AuthoritiesDAO.*(..))" access="IS_AUTHENTICATED_REMEMBERED"/>
    </global-method-security>
</beans:beans>

我的服务背景:

<?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:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:lang="http://www.springframework.org/schema/lang"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-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/jee 
        http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/lang
        http://www.springframework.org/schema/lang/spring-lang-3.0.xsd">
    <!-- ******************************************************************** -->
    <!-- Scan for service layer annotated beans -->
    <!-- ******************************************************************** -->
<context:component-scan base-package="appname" scoped-proxy="interfaces" />
<aop:config  proxy-target-class="true">
    </aop:config>
    <aop:aspectj-autoproxy proxy-target-class="true">
    </aop:aspectj-autoproxy>
    <!-- ******************************************************************** -->
    <!-- Mark bean transactions as annotation driven -->
    <!-- ******************************************************************** -->
    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

我的网络背景(为了简洁而禁止使用BEAN)!

<mvc:annotation-driven/>
    <mvc:default-servlet-handler/>    
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="multipartResolver" class="org.skyway.spring.util.web.binary.ModelBindingMultipartResolver" />
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
    <bean id="streamedBinaryContentView" class="org.skyway.spring.util.web.binary.ModelAttributeStreamer" />
    <bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" />
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/pages/" />
    </bean>
    <bean id="iPhoneUserAgentViewResolver" class="org.skyway.spring.util.viewresolution.UserAgentViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="agentSubstring" value="iPhone" />
    <property name="prefix" value="/WEB-INF/iphone/" />
    <property name="order" value="0" />
    </bean><bean id="webInfViewResolver" class="org.skyway.spring.util.viewresolution.AbsolutePathViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="order" value="-1" />
    </bean>
    <context:component-scan base-package="appname" scoped-proxy="interfaces" />
    <aop:config  proxy-target-class="true">
    </aop:config>
    <aop:aspectj-autoproxy proxy-target-class="true">
    </aop:aspectj-autoproxy>
    </beans>

这是方面的接口

package appname;
import it.pstmarche.model.HibernateSessionFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public interface SessionInterceptor {

@Pointcut("execution(public * appname.ImplantManager+.*(..))")
public void pc() ;

@Before("pc()")
public void print();
}

实施是微不足道的      @Before( “PC()”)     public void print(){     System.out.println(“关于调用打印Hello World”     }

我还尝试过其他一些表达方式:

  • @Pointcut(“execution(@ org.springframework.web.bind.annotation.RequestMapping public * appname。*(..))”)
  • 和其他许多人
没有运气。我开始认为问题不是表达式(我已经尝试了大约10-15种类型......)但是在上下文中没有考虑该类。

要回答,请考虑:

  • 名称appname例如,obviusly
  • 插入aop:config和aop:aspectj-autoproxy只是一次尝试,因为我还在其他线程中重写了必须在applicationContext加载的每个文件中启用配置。我也试过只插入一个但没有结果
  • 控制器具有带注释的接口+带注释的实现。我也试过没有注释界面或完全删除没有结果

任何帮助? :)

编辑:响应axtavt ..抱歉,我忘了添加我的web.xml,这里有一块

<servlet>
<description>context-servlet</description>
    <servlet-name>appname Servlet</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:appname-web-context.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

我通过web-context.xml文件正确加载了contextConfigLocation。对不起,我忘记了: - (

EDIT2:根据axtavt的建议,我得到了答案!谢谢!

1 个答案:

答案 0 :(得分:0)

AOP是基于每个上下文配置的。

您有两个上下文 - 根Web应用程序上下文(applicationContext.xml,其中包含导入其中的配置文件),以及DispatcherServlet的应用程序上下文,其中声明了控制器bean。

因此,为了将方面应用于控制器,您需要将<aop:aspectj-autoproxy>添加到特定于servlet的上下文中。

另请注意,您不应在不同的上下文中使用<context:component-scan>相同的基础包,请参阅@Service are constructed twice