获取注解的属性

注解如下

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface TestAno {
    String anoKey();
}

在类上如下使用注解

@TestAno(anoKey= "bb-service-test")
@Service("bbService")
public class BBServiceImpl extends AAService {

则可通过getAnnotation()方法获取该类的某个注解的属性

	// spring获取所有的该类型的bean
	String[] beanNames = applicationContext.getBeanNamesForType(AAService.class);
	if(beanNames != null) {
		for (String beanName : beanNames) {
			AAService aaService = (AAService) applicationContext.getBean(beanName);
			TestAno testAno = aaService.getClass().getAnnotation(TestAno.class);
			// 打印该类的注解的anoKey属性
			log.info("testAno:{}", testAno.anoKey);
        }
    }

会打印

testAno:bb-service-test

你可能感兴趣的:(程序人生)