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

你可能感兴趣的:(spring,AOP,Hibernate,bean,Security)