2016-11-15 26 views
0

正式綁定我有這個類在我的Spring Web模型 - 視圖 - 控制器(MVC)框架。我使用面向方面的編程(AOP),一種編程範式,旨在通過允許的橫切關注分離,以增加模塊性。 一切都很好與此類Spring MVC的:在切入點

@Aspect 
public class MarketingAspect extends ServiceSupport { 

    @Pointcut("execution(* com.tdk.iot.services.client.LicenseService.*(..))") 
    public void handleServiceMethod() { 
    } 

    @Pointcut("execution(* com.tdk.iot.services.client.ApplicantService.*(..))") 
    public void handleApplicantServiceMethod() { 
    } 


    @Before("com.tdk.iot.services.aop.ApplicantAspect.handleServiceMethod()") 
    public void before(JoinPoint _jp) { 
     User user = getLDAPUser(); 
     if(user != null &&((user.getUserRole() != UserRole.MARKETING)) { 
      throw new NoSufficientRoleException(user == null ? null : user.getUserRole(), UserRole.MARKETING); 
     } 
    } 


    @Before("com.tdk.iot.services.aop.ApplicantAspect.handleApplicantServiceMethod()") 
    public void checkRolebefore(JoinPoint _jp) { 
     User user = getLDAPUser(); 
     if(user != null &&((user.getUserRole() != UserRole.MARKETING))) { 
      throw new NoSufficientRoleException(user == null ? null : user.getUserRole(), UserRole.MARKETING); 
     } 
    } 
} 

我已經改變了方法表示法getLDAPUser和現在接收的HttpServletRequest請求作爲參數,所以改性的方法

@Before("com.tdk.iot.services.aop.ApplicantAspect.handleApplicantServiceMethod()") 
public void checkRolebefore(JoinPoint _jp, HttpServletRequest request) { 
    User user = getLDAPUser(request); 
    if(user != null &&((user.getUserRole() != UserRole.MARKETING))) { 
     throw new NoSufficientRoleException(user == null ? null : user.getUserRole(), UserRole.MARKETING); 
    } 
} 

和改性的此方法後我這個錯誤

java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 

在我的XML:

<!-- Scan for aspects --> 
    <aop:aspectj-autoproxy />  
    <bean id="marketingAspect" class="com.tdk.iot.services.aop.MarketingAspect" /> 
+0

並啓用Spring AOP的在你的配置xml文件?在註釋 –

+1

你能證明你的應用程序配置 – kuhajeyan

+0

問題開始時,我修改方法如下:公共無效checkRolebefore(一個JoinPoint _jp,HttpServletRequest的請求){ –

回答

1

首先AspectJ的基礎:錯誤formal unbound in pointcut只是意味着你的建議聲明由相應的切入點不使用(綁定)的參數。您可以綁定參數通過args()this()target()@annotation()

的具體問題是,在你的建議,你聲明參數HttpServletRequest request以通知方法參數。價值從哪裏來?相應的切入點似乎攔截不具有HttpServletRequest類型的任何參數另一個方面的建議方法。所以只要你沒有源代碼,你可以點擊這個servlet請求,你將不得不自己創建一個實例。

我的印象是,你需要學習更多的關於AOP第一。隨意發佈更多的代碼並解釋你想從哪裏獲得對象,然後我可以幫你修復你的代碼。