Java annotation scanning with spring

后端 未结 4 1468
Happy的楠姐
Happy的楠姐 2021-01-07 04:37

I have few classes that I need to annotate with a name so I defined my annotation as

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @i         


        
4条回答
  •  悲哀的现实
    2021-01-07 05:07

    You can provide a custom BeanNameGenerator to the ClassPathBeanDefinitionScanner, which can look for the value of your annotation and return that as the bean name.

    I think an implementation along these lines should work for you.

    package org.bk.lmt.services;
    
    import java.util.Map;
    import java.util.Set;
    
    import org.springframework.context.annotation.AnnotationBeanNameGenerator;
    public class CustomBeanNameGenerator extends AnnotationBeanNameGenerator{
        @Override
        protected boolean isStereotypeWithNameValue(String annotationType,
                Set metaAnnotationTypes, Map attributes) {
    
            return annotationType.equals("services.JsonUnmarshallable");
        }
    }
    

    Add this to your previous scanner code: scanner.setBeanNameGenerator(new CustomBeanNameGenerator());

提交回复
热议问题