aopalliance.intercept 实际应用

   XML文件配置:

   <?xml version="1.0" encoding="UTF-8"?>
<!--http://www.springframework.org/dtd/-->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

 "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>


<bean id="authManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager"/>
  </property>
  <property name="target">
   <ref local="authManagerTargetIntercepted"/>
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>

 

 <bean id="authManagerTargetIntercepted" class="org.springframework.aop.framework.ProxyFactoryBean">
     <property name="interceptorNames">
            <list>
              <value>sheetAuthManagerInterceptor</value>
              <value>authManagerTarget</value>
            </list>
     </property>
 </bean>


 <bean id="authManagerTarget" class="com.dd.ee.security.hibernate.HibernateAuthManager">
  <property name="hibernateTemplate">
   <ref bean="hibernateTemplate"/>
  </property>
 </bean>



 <bean id="sheetAuthManagerInterceptor" class="com.dd.ff.sheet.security.SheetAuthManagerInterceptor">
  <property name="codeManager">
   <ref bean="codeManager"/>
  </property>
 </bean>
</beans>

=======================================================================

   MethodInterceptor代码

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

 

public class tAuthManagerInterceptor implements MethodInterceptor{

 

   private final static String INTERCEPT_METHOD1 = "getAuthToken";
  private final static String INTERCEPT_EXTERNAL_METHOD1 = "getExternalAuthToken";
 private CodeManager codeManager;

 /**
  * @return Returns the codeManager.
  */
 public CodeManager getCodeManager() {
  return codeManager;
 }
 /**
  * @param codeManager The codeManager to set.
  */
 public void setCodeManager(CodeManager codeManager) {
  this.codeManager = codeManager;
 }
 /* (non-Javadoc)
  * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
  */
 public Object invoke(MethodInvocation methodInvocation) throws Throwable {

  Object retVal = methodInvocation.proceed();

  String methodName = methodInvocation.getMethod().getName();
  try {
   if (INTERCEPT_METHOD1.equals(methodName)||INTERCEPT_EXTERNAL_METHOD1.equals(methodName)) {
    process_buildAuthToken(methodInvocation, (AuthToken) retVal);
   }
  } catch (Exception e) {
   logger.warn("[w] not set AuthToken attribute flowDept. caused: " + e.getMessage());
 }

  return retVal;
 }

 

 

 private void process_buildAuthToken(MethodInvocation methodInvocation, AuthToken authToken) throws Throwable {
  List deptAncestors = (List) authToken.getAttribute("deptAncestors");
  String flowDept = getFlowStartDept(deptAncestors);
  
  if (flowDept != null&&(!"AHA".equals(flowDept))){
 自己要做的事情

  }

}


 }

 

 

 =======================================================

 

   应用代码

 

 AuthManager authManager = (AuthManager) getBean(AuthManager.BEAN_ID);
   AuthToken authToken = authManager.getAuthToken(userCode, password);

 

==============

附件JAR

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值