/** * Check whether given bean definition is a valid {@link Provider} or {@link Path} resource. * @param definition Bean definition * @param beanClass Bean class * @return <code>true</code> if it is a valid JAX-RS resource class */ private static boolean isJaxrsResourceClass(BeanDefinition definition, Class<?> beanClass) { // check Provider if (beanClass.isAnnotationPresent(Provider.class)) { if (!definition.isSingleton()) { throw new BeanDefinitionValidationException("Invalid JAX-RS @Provider bean definition for bean class [" + beanClass + "]: JAX-RS providers must be singleton beans"); } return true; } // check Path resource Class<?> pathClass = AnnotationUtils.getClassWithAnnotation(beanClass, Path.class); if (pathClass != null) { return true; } return false; }
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if (applicationContext instanceof WebApplicationContext) { setWebApplicationContext((WebApplicationContext)applicationContext); if (getGlobalBeanId() != null) { WebappResourceManager other = (WebappResourceManager) ComponentManager.getInstance().get(getGlobalBeanId()); if (other != null) { other.setWebApplicationContext((WebApplicationContext) applicationContext); } else { ComponentManager.getInstance().loadComponent(getGlobalBeanId(), this); } } } else { throw new BeanDefinitionValidationException( "WebappResourceManagerImpl must be used in a Web application context"); } }
/** * Register bean. * * @param beanName the bean name * @param beanDefinition the bean definition * @return the boolean */ public static synchronized boolean registerBean(String beanName, BeanDefinition beanDefinition) { Guard.shouldNotBeEmpty(beanName, "beanName"); shouldNotBeNull(beanDefinition, "beanDefinition"); if (isBeanNameInUse(beanName)) throw new BeanDefinitionValidationException("이미 등록된 Bean입니다. beanName=" + beanName); if (log.isInfoEnabled()) log.info("새로운 Bean을 등록합니다. beanName=[{}], beanDefinition=[{}]", beanName, beanDefinition); try { getContext().registerBeanDefinition(beanName, beanDefinition); return true; } catch (Exception e) { log.error("새로운 Bean 등록에 실패했습니다. beanName=" + beanName, e); } return false; }
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException { Assert.hasText(beanName, "Bean name must not be empty"); Assert.notNull(beanDefinition, "BeanDefinition must not be null"); if (beanDefinition instanceof AbstractBeanDefinition) { try { ((AbstractBeanDefinition) beanDefinition).validate(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName, "Validation of bean definition failed", ex); } } synchronized (this.beanDefinitionMap) { Object oldBeanDefinition = this.beanDefinitionMap.get(beanName); if (oldBeanDefinition != null) { if (!this.allowBeanDefinitionOverriding) { throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName, "Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName + "': There is already [" + oldBeanDefinition + "] bound."); } else { if (this.logger.isInfoEnabled()) { this.logger.info("Overriding bean definition for bean '" + beanName + "': replacing [" + oldBeanDefinition + "] with [" + beanDefinition + "]"); } } } else { this.beanDefinitionNames.add(beanName); this.frozenBeanDefinitionNames = null; } this.beanDefinitionMap.put(beanName, beanDefinition); } resetBeanDefinition(beanName); }
@Override public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) { // We only apply special treatment to ScriptFactory implementations here. if (!ScriptFactory.class.isAssignableFrom(beanClass)) { return null; } BeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName); String scriptFactoryBeanName = SCRIPT_FACTORY_NAME_PREFIX + beanName; String scriptedObjectBeanName = SCRIPTED_OBJECT_NAME_PREFIX + beanName; prepareScriptBeans(bd, scriptFactoryBeanName, scriptedObjectBeanName); ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class); ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator()); boolean isFactoryBean = false; try { Class<?> scriptedObjectType = scriptFactory.getScriptedObjectType(scriptSource); // Returned type may be null if the factory is unable to determine the type. if (scriptedObjectType != null) { isFactoryBean = FactoryBean.class.isAssignableFrom(scriptedObjectType); } } catch (Exception ex) { throw new BeanCreationException(beanName, "Could not determine scripted object type for " + scriptFactory, ex); } long refreshCheckDelay = resolveRefreshCheckDelay(bd); if (refreshCheckDelay >= 0) { Class<?>[] interfaces = scriptFactory.getScriptInterfaces(); RefreshableScriptTargetSource ts = new RefreshableScriptTargetSource(this.scriptBeanFactory, scriptedObjectBeanName, scriptFactory, scriptSource, isFactoryBean); boolean proxyTargetClass = resolveProxyTargetClass(bd); String language = (String) bd.getAttribute(LANGUAGE_ATTRIBUTE); if (proxyTargetClass && (language == null || !language.equals("groovy"))) { throw new BeanDefinitionValidationException( "Cannot use proxyTargetClass=true with script beans where language is not 'groovy': '" + language + "'"); } ts.setRefreshCheckDelay(refreshCheckDelay); return createRefreshableProxy(ts, interfaces, proxyTargetClass); } if (isFactoryBean) { scriptedObjectBeanName = BeanFactory.FACTORY_BEAN_PREFIX + scriptedObjectBeanName; } return this.scriptBeanFactory.getBean(scriptedObjectBeanName); }
/** * Try to obtain the actual bean class of the bean with given name. * @param beanName Bean name * @param beanDefinition Optional bean definition * @param beanFactory Bean factory (not null) * @param classLoader ClassLoader to use * @return The bean class, or <code>null</code> if cannot be detected */ public static Class<?> getBeanClass(String beanName, BeanDefinition beanDefinition, ConfigurableListableBeanFactory beanFactory, ClassLoader classLoader) { ObjectUtils.argumentNotNull(beanFactory, "Bean factory must be not null"); BeanDefinition definition = beanDefinition; if (definition == null && beanName != null) { definition = beanFactory.getBeanDefinition(beanName); } if (definition == null) { throw new BeanDefinitionValidationException("Missing bean definition for bean name [" + beanName + "]"); } // check Root definition if (definition instanceof RootBeanDefinition) { RootBeanDefinition rootBeanDef = (RootBeanDefinition) definition; try { if (rootBeanDef.getBeanClass() != null) { return rootBeanDef.getBeanClass(); } } catch (@SuppressWarnings("unused") IllegalStateException e) { // factory bean: ignore } } // Check factory bean if (definition.getFactoryMethodName() == null) { // factory class if (definition.getBeanClassName() != null) { return getBeanClass(beanName, definition.getBeanClassName(), classLoader); } } else { // factory method String factoryClassName = getBeanFactoryClassName(definition, beanFactory); if (factoryClassName != null) { Class<?> factoryClass = getBeanClass(beanName, factoryClassName, classLoader); if (factoryClass != null) { for (Method method : factoryClass.getMethods()) { if (method.getName().equals(definition.getFactoryMethodName())) { return method.getReturnType(); } } } } } // check beans defined using @Configuration Object source = definition.getSource(); if (source instanceof StandardMethodMetadata) { StandardMethodMetadata metadata = (StandardMethodMetadata) source; return metadata.getIntrospectedMethod().getReturnType(); } return null; }
@SuppressWarnings("rawtypes") @Override public Object postProcessBeforeInstantiation(Class beanClass, String beanName) { // We only apply special treatment to ScriptFactory implementations // here. if (!ScriptFactory.class.isAssignableFrom(beanClass)) { return null; } BeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName); String scriptFactoryBeanName = SCRIPT_FACTORY_NAME_PREFIX + beanName; String scriptedObjectBeanName = SCRIPTED_OBJECT_NAME_PREFIX + beanName; prepareScriptBeans(bd, scriptFactoryBeanName, scriptedObjectBeanName); ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class); ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator()); boolean isFactoryBean = false; try { Class<?> scriptedObjectType = scriptFactory.getScriptedObjectType(scriptSource); // Returned type may be null if the factory is unable to determine // the type. if (scriptedObjectType != null) { isFactoryBean = FactoryBean.class.isAssignableFrom(scriptedObjectType); } } catch (Exception ex) { throw new BeanCreationException(beanName, "Could not determine scripted object type for " + scriptFactory, ex); } long refreshCheckDelay = resolveRefreshCheckDelay(bd); if (refreshCheckDelay >= 0) { Class<?>[] interfaces = scriptFactory.getScriptInterfaces(); RefreshableScriptTargetSource ts = new RefreshableScriptTargetSource(this.scriptBeanFactory, scriptedObjectBeanName, scriptFactory, scriptSource, isFactoryBean); boolean proxyTargetClass = resolveProxyTargetClass(bd); String language = (String) bd.getAttribute(LANGUAGE_ATTRIBUTE); if (proxyTargetClass && (language == null || !language.equals("groovy"))) { throw new BeanDefinitionValidationException( "Cannot use proxyTargetClass=true with script beans where language is not 'groovy': '" + language + "'"); } ts.setRefreshCheckDelay(refreshCheckDelay); return createRefreshableProxy(ts, interfaces, proxyTargetClass); } if (isFactoryBean) { scriptedObjectBeanName = BeanFactory.FACTORY_BEAN_PREFIX + scriptedObjectBeanName; } return this.scriptBeanFactory.getBean(scriptedObjectBeanName); }
@Override protected List<String> analyzeDependencies(List<String> configLocations) throws Exception { ContextAnalyzer analyzer = new ContextAnalyzer(); List<Exception> exceptions = new LinkedList<>(); List<String> limitedLocations = new ArrayList<>(); for (String loc : configLocations) { BeanDefinitionRegistry beanFactory = getBeanFactory(loc); String[] beanNames = beanFactory.getBeanDefinitionNames(); for (String beanName : beanNames) { BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); try { if (isExport(beanDefinition)) { analyzer.addExport(beanDefinition); if (checkForRunOnly(beanName)) { limitedLocations.add(loc); } } else if (isImport(beanDefinition)) { analyzer.addImport(beanDefinition); } else if (beanDefinition.getBeanClassName() != null) { checkClassExist(loc, beanName, beanDefinition.getBeanClassName()); } } catch (Exception ex) { exceptions.add(ex); } } } analyzer.areThereExportsWithoutImport(); if (analyzer.areThereImportsWithoutExports() || !analyzer.areImportsTypesCorrect()) { exceptions.add(new BeanDefinitionValidationException( "There are severe errors while parsing contexts. See logs for details")); } if (!exceptions.isEmpty()) { for (Exception exception : exceptions) { log.error(exception.getMessage()); } throw exceptions.get(0); } DependencySorter sorter = new DependencySorter(configLocations.toArray(new String[0]), analyzer.getImports(), analyzer.getExports()); sorter.setProhibitCycles(prohibitCycles); locationsGraph = new LocationsGraph(analyzer.getImports(), analyzer.getExports()); List<String> analyzedConfigLocations = locationsGraph.filterConfigLocations(limitedLocations, sorter.sort()); log.info("ordered list of the contexts: {}", analyzedConfigLocations); return analyzedConfigLocations; }