Spring AOP - 无法执行方面
Spring AOP - Unable to execute 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");
}
}
输出:执行的服务方法
我希望打印 Aspect 中的内容
我认为你的@Component 不工作!
也许,你需要 @ComponentScan
@EnableAspectJAutoProxy
@ComponentScan
@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(TNGPrintMain.class);
CheckService ck = (CheckService)ctx.getBean("service");
ck.print();
}
@Bean(name="service")
public CheckService service(){
return new CheckService();
}
}
我是 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");
}
}
输出:执行的服务方法
我希望打印 Aspect 中的内容
我认为你的@Component 不工作!
也许,你需要 @ComponentScan
@EnableAspectJAutoProxy
@ComponentScan
@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(TNGPrintMain.class);
CheckService ck = (CheckService)ctx.getBean("service");
ck.print();
}
@Bean(name="service")
public CheckService service(){
return new CheckService();
}
}