use of org.springframework.stereotype.Controller in project commons by terran4j.
the class Api2DocCollector method toApiFolder.
/**
* 解析 API 组,一组 API 对应一个 Controller 类, 其中每个 method 对应一个 api 。<br>
* 只要有 @ApiDoc 注解,有会生成文档,没有这个注解就不会。
*
* @param bean
* @param beanName
* @return
*/
public ApiFolderObject toApiFolder(Object bean, String beanName) throws BusinessException {
Class<?> clazz = Classes.getTargetClass(bean);
Controller controller = AnnotationUtils.findAnnotation(clazz, Controller.class);
if (controller == null) {
// 不是 Controller 类,不用收集。
return null;
}
if (log.isInfoEnabled()) {
log.info("prepare to get API Info by bean: {}", beanName);
}
List<MappingMethod> methods = MappingMethod.getMappingMethods(clazz);
// Classes.getMethods(RequestMapping.class, clazz);
if (methods == null || methods.size() == 0) {
// }
return null;
}
Api2Doc classApi2Doc = clazz.getAnnotation(Api2Doc.class);
if (classApi2Doc != null && classApi2Doc.ignore()) {
// 整个类的文档被忽略。
if (log.isInfoEnabled()) {
log.info("@Api2Doc ignore = true, no need to get, " + "beanName = {}", beanName);
}
return null;
}
List<MappingMethod> ali2DocMethods = new ArrayList<>();
for (MappingMethod mappingMethod : methods) {
Method method = mappingMethod.getMethod();
Api2Doc api2Doc = method.getAnnotation(Api2Doc.class);
if (classApi2Doc == null && api2Doc == null) {
// 本方法的文档被忽略。
continue;
}
if (api2Doc != null && api2Doc.ignore()) {
// 本方法的文档被忽略。
continue;
}
ali2DocMethods.add(mappingMethod);
}
if (classApi2Doc == null && ali2DocMethods.size() == 0) {
// 整个类中的方法,都忽略从 API 生成文档,不用收集。
if (log.isInfoEnabled()) {
log.info("all method were ignored, no need to get, beanName = {}", beanName);
}
return null;
}
ApiFolderObject folder = new ApiFolderObject();
folder.setSourceClass(clazz);
String id = beanName;
if (classApi2Doc != null && StringUtils.hasText(classApi2Doc.value())) {
id = classApi2Doc.value();
}
if (classApi2Doc != null && StringUtils.hasText(classApi2Doc.id())) {
id = classApi2Doc.id();
}
folder.setId(id);
checkId(id);
String pathPattern = "api2doc/" + id + "/*.md";
try {
Resource[] resources = Classes.scanResources(pathPattern);
if (resources != null && resources.length > 0) {
Map<String, String> mds = new HashMap<>();
for (Resource resource : resources) {
String md = resource.getFilename();
mds.put(ApiFolderObject.name2Id(md), md);
}
folder.setMds(mds);
}
} catch (IOException e) {
String msg = "scan classpath[" + pathPattern + "] failed: " + e.getMessage();
throw new BeanDefinitionStoreException(msg);
}
if (classApi2Doc != null) {
folder.setOrder(classApi2Doc.order());
}
// API 组的名称。
String name = beanName;
RequestMapping classMapping = clazz.getAnnotation(RequestMapping.class);
if (classMapping != null && StringUtils.hasText(classMapping.name())) {
name = classMapping.name();
}
if (classApi2Doc != null && StringUtils.hasText(classApi2Doc.name())) {
name = classApi2Doc.name();
}
folder.setName(name);
// API 组的注释。
ApiComment apiComment = clazz.getAnnotation(ApiComment.class);
folder.setComment(ApiCommentUtils.getComment(apiComment, null, null));
// 在类上的 seeClass ,是默认的。
Class<?> defaultSeeClass = ApiCommentUtils.getDefaultSeeClass(apiComment, null);
// API 组的路径前缀。
String[] basePaths = getPath(classMapping);
// 根据方法生成 API 文档。
List<ApiDocObject> docs = new ArrayList<>();
for (MappingMethod method : ali2DocMethods) {
ApiDocObject doc = getApiDoc(method, basePaths, beanName, classApi2Doc, defaultSeeClass);
if (doc == null) {
continue;
}
String docId = doc.getId();
ApiDocObject existDoc = folder.getDoc(docId);
if (existDoc != null) {
String msg = "文档id值重复: " + docId + "\n" + "如果方法上没有用 @Api2Doc(id = \"xxx\") 来指定文档id,则重载方法会出现此问题。\n" + "请在重载的方法上用 @Api2Doc(id = \"xxx\") 来指定一个不同的文档id";
throw new BeanDefinitionStoreException(msg);
}
docs.add(doc);
if (log.isInfoEnabled()) {
log.info("add doc: {}/{}", folder.getId(), docId);
}
}
Collections.sort(docs);
folder.addDocs(docs);
return folder;
}
use of org.springframework.stereotype.Controller in project tephra by heisedebaise.
the class ClassReloaderImpl method getBeanName.
private String getBeanName(Class<?> clazz) {
Component component = clazz.getAnnotation(Component.class);
if (component != null)
return component.value();
Repository repository = clazz.getAnnotation(Repository.class);
if (repository != null)
return repository.value();
Service service = clazz.getAnnotation(Service.class);
if (service != null)
return service.value();
Controller controller = clazz.getAnnotation(Controller.class);
if (controller != null)
return controller.value();
return null;
}
use of org.springframework.stereotype.Controller in project spring-framework by spring-projects.
the class CglibProxyControllerTests method initServlet.
@SuppressWarnings("serial")
private void initServlet(final Class<?> controllerClass) throws ServletException {
servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(@Nullable WebApplicationContext parent) {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerClass));
DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
autoProxyCreator.setProxyTargetClass(true);
autoProxyCreator.setBeanFactory(wac.getBeanFactory());
wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
Pointcut pointcut = new AnnotationMatchingPointcut(Controller.class);
DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, new SimpleTraceInterceptor(true));
wac.getBeanFactory().registerSingleton("advisor", advisor);
wac.refresh();
return wac;
}
};
servlet.init(new MockServletConfig());
}
use of org.springframework.stereotype.Controller in project cuba by cuba-platform.
the class SpringBeanLoader method updateContext.
public void updateContext(Collection<Class> classes) {
if (beanFactory != null) {
boolean needToRefreshRemotingContext = false;
for (Class clazz : classes) {
Service serviceAnnotation = (Service) clazz.getAnnotation(Service.class);
ManagedBean managedBeanAnnotation = (ManagedBean) clazz.getAnnotation(ManagedBean.class);
Component componentAnnotation = (Component) clazz.getAnnotation(Component.class);
Controller controllerAnnotation = (Controller) clazz.getAnnotation(Controller.class);
String beanName = null;
if (serviceAnnotation != null) {
beanName = serviceAnnotation.value();
} else if (managedBeanAnnotation != null) {
beanName = managedBeanAnnotation.value();
} else if (componentAnnotation != null) {
beanName = componentAnnotation.value();
} else if (controllerAnnotation != null) {
beanName = controllerAnnotation.value();
}
if (StringUtils.isNotBlank(beanName)) {
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(clazz);
Scope scope = (Scope) clazz.getAnnotation(Scope.class);
if (scope != null) {
beanDefinition.setScope(scope.value());
}
beanFactory.registerBeanDefinition(beanName, beanDefinition);
}
if (StringUtils.isNotBlank(beanName)) {
needToRefreshRemotingContext = true;
}
}
if (needToRefreshRemotingContext) {
ApplicationContext remotingContext = RemotingContextHolder.getRemotingApplicationContext();
if (remotingContext != null && remotingContext instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) remotingContext).refresh();
}
}
}
}
use of org.springframework.stereotype.Controller in project ontrack by nemerosa.
the class APIController method getApiInfos.
private List<APIInfo> getApiInfos() {
List<APIInfo> apiInfos = new ArrayList<>();
Collection<Object> controllers = applicationContext.getBeansWithAnnotation(Controller.class).values();
controllers.forEach(controller -> {
APIInfo apiInfo = new APIInfo(cleanProxiedClassName(controller.getClass()), getAPIName(controller.getClass()));
// Root request mapping
RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(controller.getClass(), RequestMapping.class);
// Gets all the methods
ReflectionUtils.doWithMethods(controller.getClass(), method -> {
RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
if (methodAnnotation != null) {
APIMethodInfo apiMethodInfo = collectAPIMethodInfo(apiInfo, method, typeAnnotation, methodAnnotation);
apiInfo.add(apiMethodInfo);
}
}, ReflectionUtils.USER_DECLARED_METHODS);
// OK for this API
apiInfos.add(apiInfo);
});
return apiInfos;
}
Aggregations