1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| package com.thok.myoa.listener;
import com.thok.myoa.aop.annotation.AccessControl; import lombok.Getter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.mvc.method.RequestMappingInfo; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set;
@Component public class OnStartListener implements ApplicationListener<ContextRefreshedEvent> { private final Logger LOGGER = LoggerFactory.getLogger(OnStartListener.class); @Getter private List<String> urls = new ArrayList<String>(); @Autowired private WebApplicationContext applicationContext;
@Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { LOGGER.info("监听器启动"); RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class); Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods(); for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : map.entrySet()) { RequestMappingInfo requestMappingInfo = entry.getKey(); HandlerMethod method = entry.getValue(); AccessControl accessControl = method.getMethod().getAnnotation(AccessControl.class); if (accessControl != null) { Set<String> patterns = requestMappingInfo.getPatternsCondition().getPatterns(); for (String url : patterns) { LOGGER.info("url: " + url); } LOGGER.info(accessControl.accessType() + " , " + accessControl.transactional()); } } } }
|