0
私はCraig Wallsの "Spring in Action第4版"という本から春を学んでいます。インターフェイスで宣言されたメソッドにアドバイスを適用しようとしていますが、例外が発生しています。何も実装していないクラスに同じアドバイスを適用すると、すべて正常に動作します。Spring AOPはインターフェイスにアドバイスを適用できません
春バージョン - 4.3.2
ヘルプをいただければ幸いです。
例外:
Exception in thread "main"org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.fifczan.bean.UserService] is defined
コード:
インタフェース:
package com.fifczan.bean;
public interface Service {
void doTask();
}
実装:
package com.fifczan.bean;
import org.springframework.stereotype.Component;
@Component
public class UserService implements Service {
public void doTask() {
System.out.println("doing task");
}
}
アスペクト:
package com.fifczan;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class UserAspect {
//If i change Service(interface) to UserService(implementation)
//in pointcut I am getting the same exception
@Before("execution(* com.fifczan.bean.Service.doTask(..))")
public void userAdvice(){
System.out.println("doing sth before method doTask");
}
}
構成:
package com.fifczan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class AspectJAutoProxyConfig {
}
メイン:
package com.fifczan;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.fifczan.bean.UserService;
public class AspectJAutoProxyTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AspectJAutoProxyConfig.class);
UserService userService= ctx.getBean(UserService.class);
userService.doTask();
}
}
ありがとう、これは私の問題を解決しました –