[英]Beans with custom annotation not found
I am trying to find beans with custom annotations after startup of my spring mvc app, but am failing miserably :) 我的spring mvc应用程序启动后,我试图找到带有自定义注释的bean,但失败了:)
Bean with a custom annotation. 具有自定义注释的Bean。
@Animal("Tiger")
public class Tiger implements Cat{
public boolean evil = true;
private int teeth;
public Tiger() {
}
}
The custom annotation 自定义注释
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) //can use in class only.
public @interface Animal {
String value();
}
Then I have a Zoo class that tries to find all beans annotated with @Animal after server startup 然后我有一个Zoo类,该类试图在服务器启动后查找所有带@Animal注释的bean。
public class Zoo implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
ApplicationContext appCntxt = ((ContextRefreshedEvent) event).getApplicationContext();
System.out.println("Say hello to the family!")
Map<String,Object> beans = appCntxt.getBeansWithAnnotation(Animal.class);
}
}
}
And the Zoo is defined in my application-context.xml Zoo是在我的application-context.xml中定义的
<bean id="Zoo" class="com.app.zoo"/>
The println is being executed but beans.size() is always empty. 正在执行println,但beans.size()始终为空。 Ie, no beans can be found. 即,找不到任何豆类。
Any ideas? 有任何想法吗? Don't know if my poor animals are running wild or are simply dead. 不知道我的可怜动物是在野外奔跑还是死了。 :) :)
That's not a Spring bean, it's just a POJO with an annotation. 那不是Spring bean,只是带有注释的POJO。 Annotate your annotation with @Component
for what I think you are looking for. 用@Component
注释我认为要查找的注释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.