2012-04-20 15 views
0

私は今、SpringプロジェクトでAOPを有効にしようとしています。 @RequestMappingが完了した後にコードを実行したい(セッションクリーニング)。Pointcut to Spring @RequestMapping

<?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> 

私のWebコンテキストここに私の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> 

セキュリティコンテキストがあります:(息切れのために省略ビーン定義!!)

<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> 

これは実装がパブリック@Before( "PC()") 自明である態様クラスのインタフェース

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(); 
} 

ありますvoid print(){ System.out.println( "Hello Worldを印刷するための呼び出しについて" )

( "パブリック* APPNAMEをorg.springframework.web.bind.annotation.RequestMapping @実行(*(..))。")

  • @Pointcut
  • と多くを:

    は、私はまた、のようないくつかの他の表現を試してみましたその他

運がない。私は問題が式ではないと思っています(私はすでに約10-15種類を試しましたが...)が、そのクラスは文脈では考慮されていません。

の答えについては、考慮して取る:

  • 名appnameのは、例えば意図され、obviusly
  • AOPを挿入します。configとAOPを:私も他にreadedので、AspectJの-自動プロキシだけ試していますapplicationContextによってロードされるすべてのファイルでconfigを有効にする必要があります。私はまた、1つだけの結果を挿入しようとしましたが、
  • コントローラーに注釈付きインターフェイス+注釈付き実装があります。私はまた、インターフェイスに注釈をつけたり、結果なしで完全に削除したりせずに試しました。

何か助けてください? :)

EDIT:axtavtに応じて..私は私のweb.xmlを追加するのを忘れ申し訳ありませんが、ここで私が正しくウェブのcontext.xmlファイルを経由してcontextConfigLocationを読み込むこと

<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> 

の一部です。申し訳ありません、私はそれを忘れました:-(

EDIT2:axtavtの提案で私のミスの後に答えを得ました!ありがとう!

答えて

0

AOPはコンテキストごとに設定されます。

ルートWebアプリケーションのコンテキスト(applicationContext.xml、これにインポートされた設定ファイルを含む)と、コントローラのBeanが宣言されているアプリケーションコンテキストDispatcherServletの2つのコンテキストがあります。

したがって、コントローラにアスペクトを適用するには、<aop:aspectj-autoproxy>をサーブレット固有のコンテキストに追加する必要があります。

同じ基本パッケージに異なるコンテキストで<context:component-scan>を使用しないでください(@Service are constructed twiceを参照)。

+0

更新された質問。 私はディスパッチャのコンテキストをうまく定義しますが、問題は複数のコンテキストに関連しています。 – Gtazok

+0

更新:ありがとうございます。 DispatcherServletコンテキストの下で正しいパッケージをスキャンするために私が見逃したことが分かりました!私はアプリケーションのコンテキストの他のすべてのファイルの下で正しくスキャンしましたが、これは必要ありませんでした。そして、私はそれを逃しました!もう一度ありがとう! – Gtazok

関連する問題