下面列出了org.springframework.stereotype.Component#value ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
/**
* 获取组件名称
*
* @param providerClass 服务提供接口类
* @return 服务名称
*/
protected String getComponentName(final Class<?> providerClass) {
String name = null;
Component component = findAnnotation(providerClass, Component.class);
if (component != null) {
name = component.value();
}
if (isEmpty(name)) {
Service service = findAnnotation(providerClass, Service.class);
if (service != null) {
name = service.value();
}
}
return name;
}
@Override
public void setTargetClass(Class<?> targetClass) {
this.targetClass = targetClass;
if (StringUtils.isBlank(targetObjectId)) {
Component component = AnnotationUtils.findAnnotation(targetClass, Component.class);
String beanName = component.value();
if (StringUtils.isBlank(beanName)) {
beanName = StringUtils.uncapitalize(targetClass.getSimpleName());
}
this.targetObjectId = beanName;
}
}
@Override
protected void process() throws Exception {
Class<?> clazz = this.processCond();
Component component = clazz.getAnnotation(Component.class);
String nodeId = component.value();
this.getSlot().setCondResult(this.getClass().getName(), nodeId);
}
private String getBeanName(final Class<?> clazz) {
final Component componentAnno = clazz.getAnnotation(Component.class);
if(componentAnno != null && !componentAnno.value().isEmpty()) {
return componentAnno.value();
}
final Service serviceAnno = clazz.getAnnotation(Service.class);
if(serviceAnno != null && !serviceAnno.value().isEmpty()) {
return serviceAnno.value();
}
final Repository repositoryAnno = clazz.getAnnotation(Repository.class);
if(repositoryAnno != null && !repositoryAnno.value().isEmpty()) {
return repositoryAnno.value();
}
final Controller controllerAnno = clazz.getAnnotation(Controller.class);
if(controllerAnno != null && !controllerAnno.value().isEmpty()) {
return controllerAnno.value();
}
// ステレオタイプのアノテーションでBean名の指定がない場合は、クラス名の先頭を小文字にした名称とする。
String simpleName = uncapitalize(clazz.getSimpleName());
String[] names = applicationContext.getBeanNamesForType(clazz);
if(names.length == 0) {
return simpleName;
}
// 複数存在する場合、候補の中から一番シンプルな形式の名前で一致するものを選択する
for(String name : names) {
if(name.equals(simpleName)) {
return name;
}
}
// 見つからない場合は、候補の先頭のものにする。
return names[0];
}
public void updateContext(Collection<Class> classes) {
if (beanFactory != null) {
boolean needToRefreshRemotingContext = false;
for (Class clazz : classes) {
Service serviceAnnotation = (Service) clazz.getAnnotation(Service.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 (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();
}
}
}
}
/**
* クラスタイプから、候補となるSpringBean名を取得する
* @param targetClass SpringBeanのクラスタイプ
* @return SpringBean名
*/
private String getBeanName(final Class<?> targetClass) {
final Component componentAnno = targetClass.getAnnotation(Component.class);
if(componentAnno != null && !componentAnno.value().isEmpty()) {
return componentAnno.value();
}
final Service serviceAnno = targetClass.getAnnotation(Service.class);
if(serviceAnno != null && !serviceAnno.value().isEmpty()) {
return serviceAnno.value();
}
final Repository repositoryAnno = targetClass.getAnnotation(Repository.class);
if(repositoryAnno != null && !repositoryAnno.value().isEmpty()) {
return repositoryAnno.value();
}
final Controller controllerAnno = targetClass.getAnnotation(Controller.class);
if(controllerAnno != null && !controllerAnno.value().isEmpty()) {
return controllerAnno.value();
}
// ステレオタイプのアノテーションでBean名の指定がない場合は、クラス名の先頭を小文字にした名称とする。
String simpleName = Utils.uncapitalize(targetClass.getSimpleName());
String[] names = applicationContext.getBeanNamesForType(targetClass);
if(names.length == 0) {
return simpleName;
}
// 複数存在する場合、候補の中から一番シンプルな形式の名前で一致するものを選択する
for(String name : names) {
if(name.equals(simpleName)) {
return name;
}
}
// 見つからない場合は、候補の先頭のものにする。
return names[0];
}