Results 1 to 2 of 2

Thread: Context Component Scan not finding my custom annotated beans

  1. #1

    Default 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. #2

    Default Answer to myself:

    Quote 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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •