I've annotated a method with @Async
in the hope that it will be run asynchronously. However, when I print out the thread name from the thread, it is still 'main' just as it is when I run it without the @Async
annotation. It also returns about as slowly (700ms) which makes me think that it's not getting run asynchronously at all.
The bean is annotated with @Component
, is getting its dependencies successfully wired in with @Autowired
. I'm creating the bean with
ApplicationContext context = new AnnotationConfigApplicationContext(MyClassConfig.class);
MyClass myClass = context.getBean(MyClass.class);
Is there something I'm doing wrong?
Many thanks.
EDIT: added more code
The config class looks like this:
@Configuration
@ComponentScan
@EnableAsync
public class MyClassConfig {
@Bean
public MyClass myClass(){
return new MyClass();
}
}
The async method is:
@Async
public void doSth(){
...
}
Another thought is, does it have to return a Future? I don't care about the result - it's being evaluated for its side effects - but I also read that it should return a Future?
The code that calls doSth() is just
doSth()
called from another method in the class.