对于Spring来说,我是一个新手,并试图在Spring中使用@Aspect注释向一个简单的方法连接点添加一个简单的建议。
我使用了简单的界面和实现方法
package aspect;
public interface Module {
public void getModuleName();
}
package aspect;
import org.springframework.stereotype.Component;
@Component
public class MyModule implements Module {
@Override
public void getModuleName() {
long t = System.nanoTime();
System.out.println("My Module.");
long t1 = System.nanoTime()-t;
System.out.println("Execution time : "+t1);
}
}
一个简单的建议如下
package aspect;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
public class MyDefinedAspectLogging {
@Pointcut("execution(* aspect.MyModule.getModuleName(..))")
public void dummy() {}
@Before("dummy()")
public void beforeLog()
{
System.out.println("Log Before entering service");
}
@After("dummy()")
public void AfterLog()
{
System.out.println("Log After returning from service");
}
}
我的上下文配置文件如下
package aspect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackageClasses= {MyModule.class})
public class ConfigJava {
@Bean
public MyDefinedAspectLogging getMyDefinedAspectLogging() {
return new MyDefinedAspectLogging();
}
}
我已经实现了一个简单的测试类,如下所示
package aspect;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Mainer {
public static void main(String[] args) {
ApplicationContext context = new
AnnotationConfigApplicationContext(ConfigJava.class);
MyModule m = context.getBean(MyModule.class);
m.getModuleName();
((ConfigurableApplicationContext)context).close();
}
}
当我运行此操作时,我收到错误,如下所示
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [aspect.MyModule] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:371)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968)
at aspect.Mainer.main(Mainer.java:12)
根据我的理解,应该使用组件扫描创建MyModule类。
为了测试这个,我在java配置中注释了我的建议bean定义,如下所示
package aspect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackageClasses= {MyModule.class})
public class ConfigJava {
/* @Bean
public MyDefinedAspectLogging getMyDefinedAspectLogging() {
return new MyDefinedAspectLogging();
}*/
}
在此之后我运行了Mainer类的Main方法并获得了以下输出
Mar 16, 2018 7:17:35 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5fa7e7ff: startup date [Fri Mar 16 19:17:35 IST 2018]; root of context hierarchy
My Module.
Execution time : 101664
Mar 16, 2018 7:17:36 PM org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5fa7e7ff: startup date [Fri Mar 16 19:17:35 IST 2018]; root of context hierarchy
我在这里缺少什么。如何定义advice bean是在Spring上下文中停止创建实现bean。
有些观点我尝试但没有取得任何成功:
更新: 我尝试在切入点表达式中使用Module而不是MyModule
@Pointcut("execution(* aspect.Module.getModuleName(..))")
public void dummy() {}