A class of mine (MyClass.java
) in a certain package (packageC
) uses an annotation which result in the following error:
NoSuchBeanDefinitionException: No qualifying bean of type [com.foo.SomeAnnotation] is defined
The annotations used are from a 3rd party library (com.foo
)
I am not using XML to configure Spring. What I do is that during the set up of my application I create an AnnotationConfigApplicationContext
object which I use to scan packages for annotated classes and later create beans from that classes.
Application initialiser:
...
final private AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
public void initApp() {
//packageC is not scanned
ctx.scan("packageA", "packageB");
ctx.refresh();
}
// Called by the application when a bean is required
public <A> A getBeanForClass(Class<A> clazz) {
return ctx.getBean(clazz);
}
...
Since I know that the annotations from the 3rd party library cannot be managed by Spring (rather, Spring cannot create beans from that library) I have excluded from the scan the package of mine which containing classes use the 3rd party annotations:
packageC.MyClass
package packageC
import com.foo.Annotation
public class MyClass {
...
@Annotation
public void someMethod() {
...
}
}
My question is why I get the error if I am not telling Spring to create beans from annotated classes in "packageC"???
I have to add that someMethod()
uses an object from a class in a package which is scanned.
I was probably too vague but I would like to focus on the scan
behaviour of AnnotationConfigApplicationContext
rather than trying to start a discussion about implementation details.
In any case here are some details of my application:
Global
object (as per Play-Spring-Jpa tutorial)Deadbolt
(with its @Restrict
annotations)Thank you for any clarification you might give me :)
EDIT:
I have found a related problem on play-framework google group. Please note that there the guy is actually scanning the package with the annotated classes... I am NOT.
You should add an annotation such as @Component
or@Configuration
or @Service
at the head of [com.foo.SomeAnnotation]
class, to make the spring context know it.
is a spring bean.