Springboot初期化beanスキャン
4572 ワード
ここでは主にカスタム注記をスキャンします.
public class AnnotationScan implements ApplicationListener{
@Autowired
RedisTemplate redisTemplate;
private final static Logger logger = LoggerFactory.getLogger(AnnotationScan.class);
/**
*
* @param event
*/
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
HashSet set = new HashSet<>();//
if(event.getApplicationContext().getParent() == null)
{
Map, Object> beans = event.getApplicationContext().getBeansWithAnnotation(RestController.class);// ControllerBean
Set, Object>> entries = beans.entrySet();// Bean
Iterator, Object>> iterator = entries.iterator();
while(iterator.hasNext()){
Map.Entry, Object> map = iterator.next();
Class> aClass = map.getValue().getClass();
Method[] methods = aClass.getMethods();
for (Method method: methods
) {
AuthAnnotation annotation = method.getAnnotation(AuthAnnotation.class);
if(annotation==null){//
logger.warn(" ");
}else{
String[] authorities = annotation.authorities();
for (String authority: authorities
) {
set.add(authority);// ,
}
}
}
}
}
}
構成クラス@Bean
public AnnotationScan annotationScan(){return new AnnotationScan();}