下面列出了怎么用org.springframework.beans.factory.BeanIsAbstractException的API类实例代码及写法,或者点击链接到github查看源代码。
@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);
}
@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);
}
/**
* 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);
}
@ParameterizedTest(name = "{0} profile")
@ValueSource(strings = {
"",
"web/desktop/chrome",
"web/phone/iphone",
"web/tablet/ipad",
"web/desktop/chrome/mobile_emulation/phone",
"web/desktop/chrome/mobile_emulation/tablet",
"web/desktop/chrome/mobile_emulation/responsive"
})
void testBeanFactory(String profile)
{
System.setProperty(CONFIGURATION_PROFILE, profile);
System.setProperty(CONFIGURATION_ENVIRONMENTS, "integrationtest");
BeanFactory.open();
for (String beanName : BeanFactory.getBeanDefinitionNames())
{
try
{
BeanFactory.getBean(beanName).hashCode();
}
catch (@SuppressWarnings("unused") BeanIsAbstractException e)
{
// ignored
}
}
}
@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, @Nullable Object[] args)
throws BeanDefinitionStoreException {
if (mbd.isAbstract()) {
throw new BeanIsAbstractException(beanName);
}
}
/**
* 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, @Nullable Object[] args)
throws BeanDefinitionStoreException {
if (mbd.isAbstract()) {
throw new BeanIsAbstractException(beanName);
}
}
/**
* 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);
}
}
/**
* 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);
}
}