2012-06-25 4 views
1

다른 클래스의 특정 메서드가 호출 될 때 메서드를 트리거하려고합니다. 그 이유는 @Pointcut을 사용하려고 생각한 이유입니다.스프링 AOP @Pointcut 트리거링을하지 않음

아래의 코드는 내가 코딩하고있는 코드와 거의 동일하므로 추가 할 부분이 없습니다.

@Aspect 
public class OrgManagerSynchronizer { 

    @Pointcut("execution(* com.alvin.OrgManager.getOrg(..))") 
    public void classMethods() {} 

    @Before("classMethods()") 
    public void synchronize(JoinPoint jp) { 
     //code should be executed. but does not execute. 
    } 
} 

내 .XML에서

이 지정되었다 :

aop:aspectj-autoproxy 

더 내가 추가해야

public class OrgManagerImpl implements OrgManager { 
    public IOrg getOrg(String orgShortName) { 
    } 
} 

이 트리거해야하는 클래스이다? 다음에 무엇을할지?

+0

매번, 그것은) (AOP의 부분은 getOrg로 직접 이동 건너; – Alvin

+0

'component : scan'에'OrgManagerSynchronizer'를 추가 했습니까? – xyz

+0

xml 구성을 게시 할 수 있습니까? OrgManagerImpl이 Spring 빈인지 여부를 확인하십시오. –

답변

0

아래 사항을 확인하십시오.

1) OrgManagerImpl이 컨텍스트 xml에서 bean으로 정의되어 있는지 확인하거나 사용자가 가지고있는 컨텍스트 xml 또는 해당 클래스의 패키지에서 @Component &으로 표시되어 있는지 확인하십시오.

2) 위의 것은 정확하면 다음이 포인트 컷을 차단 모든 방법을 얻을

@Pointcut("execution(* get*(..))") 

다음과 같이 포인트 컷 변경해보십시오. 이 시점에서 동기화 메소드가 작동하는지 확인하십시오. 작동한다면 최소한 스프링 구성은 정상입니다. pointcut 표현식을 수정하면됩니다. 그러나 이것도 작동하지 않는다면 스프링 솝 구성 그 자체에 문제가 있습니다. 그래서 우리는 그것들에 집중할 수 있습니다.

또한이 다음 작업처럼 등 상황에 맞는 XML, 콩 자바 클래스를 확인할 필요가

0

두 가지를 좀 더 정보를 제공하지 않을 경우.

  1. AOP : AspectJ를-자동 프록시가
  2. 구성
  3. 에서 활성화되는 포인트 컷/화면/대상 봄 콩에게 있습니다

아래 내 XML의 구성 예이다.

<?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:context="http://www.springframework.org/schema/context" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 

    <aop:aspectj-autoproxy/> 
    <context:component-scan base-package="com.techoffice"/> 

    <bean class="com.techoffice.example.ExampleAspect"/> 

</beans> 

ExampleAspect.java 내가 코드를 디버깅

package com.techoffice.example; 

import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.After; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.aspectj.lang.annotation.Pointcut; 

@Aspect 
public class ExampleAspect { 

    @Pointcut("execution (* com.techoffice..*(..))")   
    public void anyRun() {} 

    @Before(value="anyRun()") 
    public void beforeAnyRun(JoinPoint jointPoint){ 
     System.out.println("Before " + jointPoint.getClass().getName() + "." + jointPoint.getSignature().getName()); 
    } 

    @After(value="anyRun()") 
    public void afterAnyRun(JoinPoint jointPoint){ 
     System.out.println("After " + jointPoint.getClass().getName() + "." + jointPoint.getSignature().getName()); 
    } 
} 

HelloWorldExample

package com.techoffice.example; 

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.stereotype.Component; 

@Component 
public class HelloWorldExample { 

    public void run(){ 
     System.out.println("run"); 
    } 

    public static void main(String[] args){ 
     ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 
     HelloWorldExample helloWorldExample = context.getBean(HelloWorldExample.class); 
     helloWorldExample.run(); 
    } 
}