Java Code Examples for org.springframework.beans.factory.support.RootBeanDefinition#setTargetType()

The following examples show how to use org.springframework.beans.factory.support.RootBeanDefinition#setTargetType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotationConfigApplicationContextTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void individualBeanWithFactoryBeanSupplierAndTargetType() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	RootBeanDefinition bd = new RootBeanDefinition();
	bd.setInstanceSupplier(TypedFactoryBean::new);
	bd.setTargetType(ResolvableType.forClassWithGenerics(FactoryBean.class, String.class));
	bd.setLazyInit(true);
	context.registerBeanDefinition("fb", bd);
	context.refresh();

	assertEquals(String.class, context.getType("fb"));
	assertEquals(FactoryBean.class, context.getType("&fb"));
}
 
Example 2
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testGenericsBasedInjectionWithBeanDefinitionTargetResolvableType() {
	RootBeanDefinition bd1 = new RootBeanDefinition(GenericInterface2Bean.class);
	bd1.setTargetType(ResolvableType.forClassWithGenerics(GenericInterface2Bean.class, String.class));
	bf.registerBeanDefinition("bean1", bd1);
	RootBeanDefinition bd2 = new RootBeanDefinition(GenericInterface2Bean.class);
	bd2.setTargetType(ResolvableType.forClassWithGenerics(GenericInterface2Bean.class, Integer.class));
	bf.registerBeanDefinition("bean2", bd2);
	bf.registerBeanDefinition("bean3", new RootBeanDefinition(MultiGenericFieldInjection.class));

	assertEquals("bean1 a bean2 123", bf.getBean("bean3").toString());
	assertEquals(ResolvableType.forClassWithGenerics(GenericInterface2Bean.class, String.class), bd1.getResolvableType());
	assertEquals(ResolvableType.forClassWithGenerics(GenericInterface2Bean.class, Integer.class), bd2.getResolvableType());
}
 
Example 3
Source File: AnnotationConfigApplicationContextTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void individualBeanWithFactoryBeanSupplierAndTargetType() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	RootBeanDefinition bd = new RootBeanDefinition();
	bd.setInstanceSupplier(TypedFactoryBean::new);
	bd.setTargetType(ResolvableType.forClassWithGenerics(FactoryBean.class, String.class));
	bd.setLazyInit(true);
	context.registerBeanDefinition("fb", bd);
	context.refresh();

	assertEquals(String.class, context.getType("fb"));
	assertEquals(FactoryBean.class, context.getType("&fb"));
}
 
Example 4
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testGenericsBasedInjectionWithBeanDefinitionTargetResolvableType() {
	RootBeanDefinition bd1 = new RootBeanDefinition(GenericInterface2Bean.class);
	bd1.setTargetType(ResolvableType.forClassWithGenerics(GenericInterface2Bean.class, String.class));
	bf.registerBeanDefinition("bean1", bd1);
	RootBeanDefinition bd2 = new RootBeanDefinition(GenericInterface2Bean.class);
	bd2.setTargetType(ResolvableType.forClassWithGenerics(GenericInterface2Bean.class, Integer.class));
	bf.registerBeanDefinition("bean2", bd2);
	bf.registerBeanDefinition("bean3", new RootBeanDefinition(MultiGenericFieldInjection.class));

	assertEquals("bean1 a bean2 123", bf.getBean("bean3").toString());
	assertEquals(ResolvableType.forClassWithGenerics(GenericInterface2Bean.class, String.class), bd1.getResolvableType());
	assertEquals(ResolvableType.forClassWithGenerics(GenericInterface2Bean.class, Integer.class), bd2.getResolvableType());
}
 
Example 5
Source File: DapengComponentScanRegistrar.java    From dapeng-soa with Apache License 2.0 5 votes vote down vote up
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
    for(String name: registry.getBeanDefinitionNames()){
        BeanDefinition definition = registry.getBeanDefinition(name);
        if(definition instanceof AnnotatedBeanDefinition){
            AnnotationMetadata metadata = ((AnnotatedBeanDefinition) definition).getMetadata();
            if(metadata.hasAnnotation(DapengService.class.getName()) ||
                metadata.hasMetaAnnotation(DapengService.class.getName())){

                Map<String, Object> annotationAtts = metadata.getAnnotationAttributes(DapengService.class.getName());

                if (annotationAtts.containsKey("service")) {
                    char[] realServiceNameAsChars = ((Class)annotationAtts.get("service")).getSimpleName().toCharArray();
                    realServiceNameAsChars[0] += 32;
                    String realServiceName = String.valueOf(realServiceNameAsChars);
                    ConstructorArgumentValues paras = new ConstructorArgumentValues();
                    paras.addIndexedArgumentValue(0, new RuntimeBeanReference(realServiceName));
                    paras.addIndexedArgumentValue(1, realServiceName);

                    RootBeanDefinition serviceDef = new RootBeanDefinition(SoaProcessorFactory.class, paras, null);
                    serviceDef.setScope(BeanDefinition.SCOPE_SINGLETON);
                    serviceDef.setTargetType(SoaServiceDefinition.class);

                    registry.registerBeanDefinition(realServiceName + "-definition", serviceDef);
                }
            }
        }
    }
}
 
Example 6
Source File: MSF4JBeanDefinitionRegistryPostProcessor.java    From msf4j with Apache License 2.0 5 votes vote down vote up
private void registerBeanDefinition(BeanDefinitionRegistry registry, String beanName, Class beanClass) {
    RootBeanDefinition beanDefinition =
            new RootBeanDefinition(beanClass);
    beanDefinition.setTargetType(beanClass);
    beanDefinition.setRole(BeanDefinition.ROLE_APPLICATION);
    registry.registerBeanDefinition(beanName, beanDefinition);
}