0
私はSpring AOPとアノテーションの初心者です。 Aspectを使った簡単なプログラムを書こうとしました。私はどこが間違っているのか理解できません。それは自分のアスペクトにあるものを印刷しません。Spring AOP - Aspectを実行できません
package com.business.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@EnableAspectJAutoProxy
@Configuration
public class PrintMain {
public static void main(String[] args) {
// Do I always need to have this. Can't I just use @Autowired to get beans
ApplicationContext ctx = new AnnotationConfigApplicationContext(PrintMain.class);
CheckService ck = (CheckService)ctx.getBean("service");
ck.print();
}
@Bean(name="service")
public CheckService service(){
return new CheckService();
}
}
package com.business.main;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class SimpleAspect {
@Around("execution(* com.business.main.CheckService.*(..))")
public void applyAdvice(){
System.out.println("Aspect executed");
}
}
package com.business.main;
import org.springframework.stereotype.Component;
@Component
public class CheckService{
public void print(){
System.out.println("Executed service method");
}
}
出力:実行されるサービスの方法私は私の局面では、私が持っているものに印刷することを期待
を必要とする)はComponentScan @追加します。しかし、私はなぜそれがインターフェイスなしで動作していないか知りたいです......私は注釈を強制することはできませんCGLIBに関連するものをお読みください。第二に@ ComponentScanなしで動作しないのはなぜですか? – karthik
@karthik Componentは使用していますが、スプリングはBeanとして使用しないため、ApplicationContextには存在しません。 ApplicationContextが接続されている場合、誰がBeanとして配線されるのかを知る必要があります。 ApplicationContextはあなたがComponentを必要としているのか、それを使用しているのかを知らないので、SpringにBeanを必要としていることを伝えるためにXmlやComponentScanを使う必要があります。 – JonahCui
@karthik私の英語はあまり良くありません。私を許してください。 – JonahCui