简体   繁体   English

找不到具有自定义注释的Bean

[英]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.

相关问题 使用自定义注释注释的bean的自定义初始化 - Custom initializing of beans annotated with a custom annotation 查找所有具有自定义注释的bean,并从此bean创建解析器 - Find all beans with custom annotation and create resolver from this beans 如何使用带有参数的自定义注释查找CDI bean? - How to find CDI beans with a custom annotation with parameters? 使用@Qualifier 注释抛出“NoUniqueBeanDefinitionException”(发现多个相同类型的bean) - Using @Qualifier annotation throws "NoUniqueBeanDefinitionException" (found multiple beans of same type) 如何更换 <mvc:annotation-driven/> 自定义豆? - How to replace <mvc:annotation-driven/> with custom beans? 使用注入的Spring bean和自定义注释在方法开始时做一些事情 - Do something in beginning of methods with injected spring beans and custom annotation 单元测试时未找到自定义注释 - Custom Annotation not found while unit testing 不兼容的类型。 找到:&#39;org.springframework.beans.factory.annotation.Value&#39;,需要:&#39;long&#39; - incompatible types. Found: 'org.springframework.beans.factory.annotation.Value', required: 'long' 我不知道为什么不能自动装配。 未找到“ObjectMapper”类型的 bean。 (注释错误) - I don't konw Why Could not autowire. No beans of 'ObjectMapper' type found. (Annotation Error) 无法自动接线。 找不到“JobRepositoryTestUtils”类型的 bean。 添加了 SpringBatchTest 注解 - Could not autowire. No beans of 'JobRepositoryTestUtils' type found. With SpringBatchTest Annotation added
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM