나는 스프링 3에서 빌드 된 자바 애플리케이션을 가지고있다.이 프로젝트는 의존성 (dependency)으로서 다른 항아리를 가지고있다.Spring AOP가 런타임에 외부 항아리를 짜 내지 않는 이유는 무엇입니까?
이 종속성은 @org.aspectj.lang.annotation.Aspect
클래스 (예 : com.aspectprovider.aspects.MyAspect
)를 포함합니다. 인터페이스 Foo
을 구현하는 클래스에서 메소드를 짜기위한 조언은 @Before
입니다. Foo
인터페이스는 "프로젝트"내부 또는 다른 항아리에있을 수 있습니다
@Before("execution(* com.project.Foo.save(..))")
:처럼 뭔가. 이 예제에서는 중요하지 않습니다.
내 프로젝트에는 Foo
을 구현하는 클래스가 포함되어 있습니다. 그것들은 내가 물론 길들이기를 원하는 수업들입니다. 나는 또한 빈으로 화면을 선언하고 일부 속성 주입
<aop:aspectj-autoproxy />
:
<bean id="myAspect" class="com.aspectprovider.aspects.MyAspect"
factory-method="aspectOf" >
<property name="someproperty" value="somevalue" />
</bean>
트로프 로깅 내가 볼 수
내 Spring 애플리케이션 컨텍스트 구성 파일 (applicationContext.xml
)
MyAspect
이 인스턴스화되고 특성이 주입됩니다. 그러나 save 메소드는 인터셉트되지 않습니다. 이게 문제 야.
jar 파일에서 Spring이있는 응용 프로그램으로 aspect 클래스를 복사하면 작동합니다. 이러한 측면이 외부 항아리에 포함되어있을 때 save 메서드는 가로 챌 수 없습니다. 모든 단서?
편집 : 기본적으로 내 applicationContext.xml
가 다음 줄
//in a JSF managed bean
@Inject
private Foo myFoo; //there's a implementation of Foo in a package that spring is looking at. So it is injected correctly.
public String someAction() {
myFoo.save("something"); //the @Before advice is only called if the class containing the aspect is not in an external jar
}
//in a class with a main method
void main(String[] ars) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//right after the previous line, I can see in the log that MyAspect is instantiated.
Foo myFoo = ac.getBean(Foo.class);
myFoo.save("something"); //the @Before advice is only called if the class containing the aspect is not in an external jar
}
: 나는 푸의 저장 메소드를 호출하고 어떻게
<context:annotation-config />
<context:component-scan base-package="com.project" />
<context:component-scan base-package="com.aspectprovider.aspects" />
<aop:aspectj-autoproxy />
<bean id="myAspect" class="com.aspectprovider.aspects.MyAspect" factory-method="aspectOf" >
<property name="someproperty" value="somevalue" />
</bean>
을 나는 같은 것을 넣을 필요가 있다고 생각하지 않습니다
<context:component-scan base-package="com.project">
<context:include-filter type="aspectj" expression="com.aspectprovider.aspects.*" />
</context:component-scan>
당신은 J2EE의 environement 아래를 실행하고 있습니까? Weblogic websphere jboss 또는 심지어 바람둥이? –
바람둥이입니다. 하지만 스프링 컨텍스트를 "손으로로드"콘솔을 통해 실행하는 경우 동일한 문제가 있습니다. – bluefoot
어떻게'save' 메소드를 호출할까요? Spring이 코드를 제공한다는 참조를 통해 호출하지 않는다면 pointcut는 호출되지 않을 것입니다 *. 일반적인 실수는 'this'(명시 적 으로든 묵시적이든)를 통해 호출하는 것인데, 이는 wrapped 인스턴스에서 직접 호출되며 bean 자체에서는 직접 호출되지 않습니다. –