本文整理汇总了Java中org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean方法的典型用法代码示例。如果您正苦于以下问题:Java DefaultListableBeanFactory.getBean方法的具体用法?Java DefaultListableBeanFactory.getBean怎么用?Java DefaultListableBeanFactory.getBean使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.beans.factory.support.DefaultListableBeanFactory
的用法示例。
在下文中一共展示了DefaultListableBeanFactory.getBean方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: instantiateComponent
import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
public static <T> T instantiateComponent(DefaultListableBeanFactory beanFactory, Class<T> cls, Annotation annotation) {
T extractor;
try {
extractor = beanFactory.getBean(cls);
} catch (BeansException e) {
try {
extractor = (T) beanFactory.getBean(cls.getName());
} catch (BeansException e1) {
BeanDefinitionBuilder beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(cls.getName());
beanFactory.registerBeanDefinition(cls.getName(), beanDefinition.getBeanDefinition());
extractor = (T) beanFactory.getBean(cls.getName());
}
}
return extractor;
}
示例2: initMethod
import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void initMethod() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource("NestedBeansElementAttributeRecursionTests-init-destroy-context.xml", this.getClass()));
InitDestroyBean beanA = bf.getBean("beanA", InitDestroyBean.class);
InitDestroyBean beanB = bf.getBean("beanB", InitDestroyBean.class);
InitDestroyBean beanC = bf.getBean("beanC", InitDestroyBean.class);
InitDestroyBean beanD = bf.getBean("beanD", InitDestroyBean.class);
assertThat(beanA.initMethod1Called, is(true));
assertThat(beanB.initMethod2Called, is(true));
assertThat(beanC.initMethod3Called, is(true));
assertThat(beanD.initMethod2Called, is(true));
bf.destroySingletons();
assertThat(beanA.destroyMethod1Called, is(true));
assertThat(beanB.destroyMethod2Called, is(true));
assertThat(beanC.destroyMethod3Called, is(true));
assertThat(beanD.destroyMethod2Called, is(true));
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:24,代码来源:NestedBeansElementAttributeRecursionTests.java
示例3: testObjectFactoryMethodInjectionIntoPrototypeBean
import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testObjectFactoryMethodInjectionIntoPrototypeBean() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
RootBeanDefinition annotatedBeanDefinition = new RootBeanDefinition(ObjectFactoryQualifierMethodInjectionBean.class);
annotatedBeanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
bf.registerBeanDefinition("annotatedBean", annotatedBeanDefinition);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean"));
bf.registerBeanDefinition("testBean", bd);
bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class));
ObjectFactoryQualifierMethodInjectionBean bean = (ObjectFactoryQualifierMethodInjectionBean) bf.getBean("annotatedBean");
assertSame(bf.getBean("testBean"), bean.getTestBean());
ObjectFactoryQualifierMethodInjectionBean anotherBean = (ObjectFactoryQualifierMethodInjectionBean) bf.getBean("annotatedBean");
assertNotSame(anotherBean, bean);
assertSame(bf.getBean("testBean"), bean.getTestBean());
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:22,代码来源:InjectAnnotationBeanPostProcessorTests.java
示例4: testGenericsBasedInjectionIntoTypeVariableSelectingBestMatchAgainstFactoryMethodSignature
import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
@Ignore // SPR-11521
public void testGenericsBasedInjectionIntoTypeVariableSelectingBestMatchAgainstFactoryMethodSignature() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
RootBeanDefinition bd = new RootBeanDefinition(GenericInterface1Impl.class);
bd.setFactoryMethodName("createErased");
bf.registerBeanDefinition("bean1", bd);
bf.registerBeanDefinition("bean2", new RootBeanDefinition(GenericInterface2Impl.class));
bf.registerBeanDefinition("bean2a", new RootBeanDefinition(ReallyGenericInterface2Impl.class));
bf.registerBeanDefinition("bean2b", new RootBeanDefinition(PlainGenericInterface2Impl.class));
GenericInterface1Impl bean1 = (GenericInterface1Impl) bf.getBean("bean1");
GenericInterface2Impl bean2 = (GenericInterface2Impl) bf.getBean("bean2");
assertSame(bean2, bean1.gi2);
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:20,代码来源:AutowiredAnnotationBeanPostProcessorTests.java
示例5: testConstructorResourceInjectionWithNoCandidatesAndNoFallback
import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testConstructorResourceInjectionWithNoCandidatesAndNoFallback() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorWithoutFallbackBean.class));
try {
bf.getBean("annotatedBean");
fail("Should have thrown UnsatisfiedDependencyException");
}
catch (UnsatisfiedDependencyException ex) {
// expected
}
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:17,代码来源:AutowiredAnnotationBeanPostProcessorTests.java
示例6: testPrototypeWithArrayConversionForFactoryMethod
import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testPrototypeWithArrayConversionForFactoryMethod() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
List<String> list = new ManagedList<String>();
list.add("myName");
list.add("myBeanName");
RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
bd.setFactoryMethodName("create");
bd.getConstructorArgumentValues().addGenericArgumentValue(list);
lbf.registerBeanDefinition("test", bd);
DerivedTestBean tb = (DerivedTestBean) lbf.getBean("test");
assertEquals("myName", tb.getName());
assertEquals("myBeanName", tb.getBeanName());
DerivedTestBean tb2 = (DerivedTestBean) lbf.getBean("test");
assertTrue(tb != tb2);
assertEquals("myName", tb2.getName());
assertEquals("myBeanName", tb2.getBeanName());
}
示例7: get
import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
/**
* Factory method to get the {@link BeanTypeRegistry} for a given {@link BeanFactory}.
*
* @param beanFactory the source bean factory
* @return the {@link BeanTypeRegistry} for the given bean factory
*/
static BeanTypeRegistry get(ListableBeanFactory beanFactory) {
Assert.isInstanceOf(DefaultListableBeanFactory.class, beanFactory);
DefaultListableBeanFactory listableBeanFactory = (DefaultListableBeanFactory) beanFactory;
Assert.isTrue(listableBeanFactory.isAllowEagerClassLoading(), "Bean factory must allow eager class loading");
if (!listableBeanFactory.containsLocalBean(BEAN_NAME)) {
BeanDefinition bd = new RootBeanDefinition(BeanTypeRegistry.class);
bd.getConstructorArgumentValues().addIndexedArgumentValue(0, beanFactory);
listableBeanFactory.registerBeanDefinition(BEAN_NAME, bd);
}
return listableBeanFactory.getBean(BEAN_NAME, BeanTypeRegistry.class);
}
示例8: getBean
import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
/**
* 从缺省的bean工厂中获取指定名称的bean实例
* @param name
* @param args
* @return
*/
public static Object getBean(String name,Object[] args) {
DefaultListableBeanFactory beanFactory = getDefaultBeanFactory();
if(args == null)
return beanFactory.getBean(name,args);
else
return beanFactory.getBean(name,args); //这个方法有问题,Spring不支持构造函数传参,将来修改
}
示例9: createTemporaryBean
import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
private static <T> T createTemporaryBean(Class<T> type,
ConfigurableListableBeanFactory parent, BeanDefinition definition) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(
parent);
beanFactory.registerBeanDefinition(type.getName(), definition);
return beanFactory.getBean(type);
}
示例10: testDefault
import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
private static void testDefault() {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource res = resolver.getResource("classpath:spring-context.xml");
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
reader.loadBeanDefinitions(res);
Mars mars = bf.getBean("mars", Mars.class);
System.out.println(mars.getCnName() + ":" + mars.getAge());
}
示例11: testPropertyPathFactoryBeanAsInnerBean
import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testPropertyPathFactoryBeanAsInnerBean() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONTEXT);
TestBean spouse = (TestBean) xbf.getBean("otb.spouse");
TestBean tbWithInner = (TestBean) xbf.getBean("tbWithInner");
assertSame(spouse, tbWithInner.getSpouse());
assertTrue(!tbWithInner.getFriends().isEmpty());
assertSame(spouse, tbWithInner.getFriends().iterator().next());
}
示例12: testExtendedEjbInjection
import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testExtendedEjbInjection() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerResolvableDependency(BeanFactory.class, bf);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ExtendedEjbInjectionBean.class));
TestBean tb = new TestBean();
bf.registerSingleton("testBean", tb);
TestBean tb2 = new TestBean();
bf.registerSingleton("testBean2", tb2);
TestBean tb3 = new TestBean();
bf.registerSingleton("testBean3", tb3);
TestBean tb4 = new TestBean();
bf.registerSingleton("testBean4", tb4);
NestedTestBean tb6 = new NestedTestBean();
bf.registerSingleton("xy", tb6);
bf.registerAlias("xy", "testBean9");
ExtendedEjbInjectionBean bean = (ExtendedEjbInjectionBean) bf.getBean("annotatedBean");
assertTrue(bean.initCalled);
assertTrue(bean.init2Called);
assertSame(tb, bean.getTestBean());
assertSame(tb2, bean.getTestBean2());
assertSame(tb4, bean.getTestBean3());
assertSame(tb3, bean.getTestBean4());
assertSame(tb6, bean.testBean5);
assertSame(tb6, bean.testBean6);
assertSame(bf, bean.beanFactory);
bf.destroySingletons();
assertTrue(bean.destroyCalled);
assertTrue(bean.destroy2Called);
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:37,代码来源:CommonAnnotationBeanPostProcessorTests.java
示例13: testFactoryMethodsWithAutowire
import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testFactoryMethodsWithAutowire() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
FactoryMethods fm = (FactoryMethods) xbf.getBean("fullWithAutowire");
assertEquals(27, fm.getNum());
assertEquals("gotchaAutowired", fm.getName());
assertEquals("Juergen", fm.getTestBean().getName());
}
示例14: propertyWithNameEndingInRef
import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void propertyWithNameEndingInRef() throws Exception {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions(
new ClassPathResource("simplePropertyNamespaceHandlerTests.xml", getClass()));
ITestBean sally = (TestBean) beanFactory.getBean("derivedSally");
assertEquals("r", sally.getSpouse().getName());
}
示例15: testGetBeanByTypeWithMultiplePriority
import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testGetBeanByTypeWithMultiplePriority() throws Exception {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class);
RootBeanDefinition bd2 = new RootBeanDefinition(HighPriorityTestBean.class);
lbf.registerBeanDefinition("bd1", bd1);
lbf.registerBeanDefinition("bd2", bd2);
thrown.expect(NoUniqueBeanDefinitionException.class);
thrown.expectMessage(containsString("Multiple beans found with the same priority"));
thrown.expectMessage(containsString("5")); // conflicting priority
lbf.getBean(TestBean.class);
}