/** * Check the given merged bean definition, * potentially throwing validation exceptions. * @param mbd the merged bean definition to check * @param beanName the name of the bean * @param args the arguments for bean creation, if any * @throws BeanDefinitionStoreException in case of validation failure */ protected void checkMergedBeanDefinition(RootBeanDefinition mbd, String beanName, Object[] args) throws BeanDefinitionStoreException { // check if bean definition is not abstract if (mbd.isAbstract()) { throw new BeanIsAbstractException(beanName); } // Check validity of the usage of the args parameter. This can // only be used for prototypes constructed via a factory method. if (args != null && !mbd.isPrototype()) { throw new BeanDefinitionStoreException( "Can only specify arguments for the getBean method when referring to a prototype bean definition"); } }
@Test public void testAbstractParentBeans() { DefaultListableBeanFactory parent = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT); parent.preInstantiateSingletons(); assertTrue(parent.isSingleton("inheritedTestBeanWithoutClass")); // abstract beans should not match Map<?, ?> tbs = parent.getBeansOfType(TestBean.class); assertEquals(2, tbs.size()); assertTrue(tbs.containsKey("inheritedTestBeanPrototype")); assertTrue(tbs.containsKey("inheritedTestBeanSingleton")); // abstract bean should throw exception on creation attempt try { parent.getBean("inheritedTestBeanWithoutClass"); fail("Should have thrown BeanIsAbstractException"); } catch (BeanIsAbstractException ex) { // expected } // non-abstract bean should work, even if it serves as parent assertTrue(parent.getBean("inheritedTestBeanPrototype") instanceof TestBean); }
public void perform(TaskRequest req, TaskResponse res) { URL loc = resource.evaluate(req, res); ApplicationContext beans = getApplicationContext(loc, (Boolean) cache.evaluate(req, res)); for (String name : beans.getBeanDefinitionNames()) { try { res.setAttribute(name, beans.getBean(name)); } catch (BeanIsAbstractException biae) { // This is normal -- we can't ask for abstract beans... log.debug("Not adding bean '" + name + "' to TaskResponse because it is abstract."); } } super.performSubtasks(req, res); }
@ManagedAttribute public List<String> getBeansInContext() { List<String> beans = new ArrayList<String>(); for (String beanName : applicationContext.getBeanDefinitionNames()) { Object bean; try { bean = applicationContext.getBean(beanName); } catch (BeanIsAbstractException e) { // skip abstract bean continue; } String beanClass = bean.getClass().getName(); if (!beanClass.startsWith("org.springframework")) { beans.add(format("%s (%s)", beanName, beanClass)); } } return Ordering.natural().sortedCopy(beans); }
@SuppressWarnings("deprecation") public void getNonAnnotatedTransactionalServices() { /* We only want to run getNonAnnotatedTransactionalSerivces once. * The tests actually just read the Maps that are generated here. */ if (incorrectlyAnnotatedTransactionalServices != null) { return; } incorrectlyAnnotatedTransactionalServices = new HashMap<String, Class<? extends Object>>(); nonAnnotatedTransactionalServices = new HashMap<String, String>(); doubleAnnotatedTransactionalServices = new HashMap<String, String>(); String[] beanNames = SpringContext.getBeanNames(); for (String beanName : beanNames) { if ( beanName.endsWith( "-parentBean" ) ) { continue; } Object bean = null; try { bean = SpringContext.getBean(beanName); } catch ( BeanIsAbstractException ex ) { // do nothing, ignore } catch (Exception e) { LOG.warn("Caught exception while trying to obtain service: " + beanName); LOG.warn(e.getClass().getName() + " : " + e.getMessage(), e ); } if (bean != null) { Class<? extends Object> beanClass = bean.getClass(); if (beanClass.getName().matches(".*\\$Proxy.*")) { beanClass = AopUtils.getTargetClass(bean); } if (beanClass.getName().startsWith("org.kuali") && !Modifier.isAbstract(beanClass.getModifiers()) && !beanClass.getName().endsWith("DaoOjb") && !beanClass.getName().endsWith("DaoJdbc") && !beanClass.getName().endsWith("Factory") && !beanClass.getName().contains("Lookupable") && !isClassAnnotated(beanName, beanClass)) { incorrectlyAnnotatedTransactionalServices.put(beanName, beanClass); } } } return; }
/** * Check the given merged bean definition, * potentially throwing validation exceptions. * @param mbd the merged bean definition to check * @param beanName the name of the bean * @param args the arguments for bean creation, if any * @throws BeanDefinitionStoreException in case of validation failure */ protected void checkMergedBeanDefinition(RootBeanDefinition mbd, String beanName, Object[] args) throws BeanDefinitionStoreException { if (mbd.isAbstract()) { throw new BeanIsAbstractException(beanName); } }