Announcement Announcement Module
Collapse
No announcement yet.
Context Component Scan not finding my custom annotated beans Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Context Component Scan not finding my custom annotated beans

    Hello I am trying to write a custom annotation for a Spring MVC webapp.But somehow annotated classes are not getting detected by Spring applicationContext.Here is my relevent code.

    Annotated class:

    Code:
    @Component
    @MigrationRequired(migrateFrom="Tiff",migrateTo="PDF-A")
    public class WordTemplate extends Template{
    
    }
    CustomAnnotationDefinition:

    Code:
    @Target({ ElementType.TYPE })
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface MigrationRequired {
    
        String migrateFrom() default "Tiff";
        String migrateTo() default "PDF-A";
    }
    Class for detecting Annotation on ContextLoading

    Code:
    public class MigrationHandler implements ApplicationContextAware, InitializingBean {
    
        private ApplicationContext applicationContext;
    
        @Override
        public void afterPropertiesSet() throws Exception {
    
            final Map<String, Object> myMaps = applicationContext.getBeansWithAnnotation(MigrationRequired.class);
            System.out.println("inside after properties set" + myMaps );//**Always giving an empty set**
            for (final Object myMap : myMaps.values()) {
                final Class<? extends Object> myClass = myMap .getClass();
                final MigrationRequired annotation = myClass .getAnnotation(MigrationRequired.class);
                System.out.println("Found myClass: " + myClass + ", with tags: " + annotation.migrateFrom());
            }
        }
    
        @Override
        public void setApplicationContext(final ApplicationContext applicationContext)
                throws BeansException {
            System.out.println("This is called");
            this.applicationContext = applicationContext;
        }
    }
    applicationContext.xml relevant configuration

    Code:
    <bean id="migrationHandler" class="com.test.annotation.MigrationHandler"/>
    
    <context:component-scan base-package="com.test" >
    <context:include-filter type="annotation"expression="com.test.annotation.MigrationRequired"/>
    </context:component-scan>
    <!--Other beans definition-->
    So in method of MigrationHandler afterPropertiesSet()I am always getting myMaps as empty. Am I doing something wrong? Thank you.

  • #2
    Answer to myself:

    Originally posted by piyushpatel2809 View Post
    Hello I am trying to write a custom annotation for a Spring MVC webapp.But somehow annotated classes are not getting detected by Spring applicationContext.Here is my relevent code.

    Annotated class:

    Code:
    @Component
    @MigrationRequired(migrateFrom="Tiff",migrateTo="PDF-A")
    public class WordTemplate extends Template{
    
    }
    CustomAnnotationDefinition:

    Code:
    @Target({ ElementType.TYPE })
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface MigrationRequired {
    
        String migrateFrom() default "Tiff";
        String migrateTo() default "PDF-A";
    }
    Class for detecting Annotation on ContextLoading

    Code:
    public class MigrationHandler implements ApplicationContextAware, InitializingBean {
    
        private ApplicationContext applicationContext;
    
        @Override
        public void afterPropertiesSet() throws Exception {
    
            final Map<String, Object> myMaps = applicationContext.getBeansWithAnnotation(MigrationRequired.class);
            System.out.println("inside after properties set" + myMaps );//**Always giving an empty set**
            for (final Object myMap : myMaps.values()) {
                final Class<? extends Object> myClass = myMap .getClass();
                final MigrationRequired annotation = myClass .getAnnotation(MigrationRequired.class);
                System.out.println("Found myClass: " + myClass + ", with tags: " + annotation.migrateFrom());
            }
        }
    
        @Override
        public void setApplicationContext(final ApplicationContext applicationContext)
                throws BeansException {
            System.out.println("This is called");
            this.applicationContext = applicationContext;
        }
    }
    applicationContext.xml relevant configuration

    Code:
    <bean id="migrationHandler" class="com.test.annotation.MigrationHandler"/>
    
    <context:component-scan base-package="com.test" >
    <context:include-filter type="annotation"expression="com.test.annotation.MigrationRequired"/>
    </context:component-scan>
    <!--Other beans definition-->
    So in method of MigrationHandler afterPropertiesSet()I am always getting myMaps as empty. Am I doing something wrong? Thank you.
    Adding @Component and a name field in annotation have done the trick for me.

    Comment

    Working...
    X