Java Code Examples for org.springframework.beans.factory.FactoryBean

The following examples show how to use org.springframework.beans.factory.FactoryBean. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: blog_demos   Source File: FactoryBeanRegistrySupport.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Obtain an object to expose from the given FactoryBean.
 * @param factory the FactoryBean instance
 * @param beanName the name of the bean
 * @param shouldPostProcess whether the bean is subject for post-processing
 * @return the object obtained from the FactoryBean
 * @throws BeanCreationException if FactoryBean object creation failed
 * @see org.springframework.beans.factory.FactoryBean#getObject()
 */
protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {
	if (factory.isSingleton() && containsSingleton(beanName)) {
		synchronized (getSingletonMutex()) {
			Object object = this.factoryBeanObjectCache.get(beanName);
			if (object == null) {
				object = doGetObjectFromFactoryBean(factory, beanName, shouldPostProcess);
				this.factoryBeanObjectCache.put(beanName, (object != null ? object : NULL_OBJECT));
			}
			return (object != NULL_OBJECT ? object : null);
		}
	}
	else {
		return doGetObjectFromFactoryBean(factory, beanName, shouldPostProcess);
	}
}
 
Example 2
Source Project: java-technology-stack   Source File: AutoProxyCreatorTests.java    License: MIT License 6 votes vote down vote up
@Override
@Nullable
protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String name, @Nullable TargetSource customTargetSource) {
	if (StaticMessageSource.class.equals(beanClass)) {
		return DO_NOT_PROXY;
	}
	else if (name.endsWith("ToBeProxied")) {
		boolean isFactoryBean = FactoryBean.class.isAssignableFrom(beanClass);
		if ((this.proxyFactoryBean && isFactoryBean) || (this.proxyObject && !isFactoryBean)) {
			return new Object[] {this.testInterceptor};
		}
		else {
			return DO_NOT_PROXY;
		}
	}
	else {
		return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
	}
}
 
Example 3
Source Project: cuba   Source File: CubaMethodValidationInterceptor.java    License: Apache License 2.0 6 votes vote down vote up
protected boolean isFactoryBeanMetadataMethod(Method method) {
    Class<?> clazz = method.getDeclaringClass();

    // Call from interface-based proxy handle, allowing for an efficient check?
    if (clazz.isInterface()) {
        return ((clazz == FactoryBean.class || clazz == SmartFactoryBean.class) &&
                !method.getName().equals("getObject"));
    }

    // Call from CGLIB proxy handle, potentially implementing a FactoryBean method?
    Class<?> factoryBeanType = null;
    if (SmartFactoryBean.class.isAssignableFrom(clazz)) {
        factoryBeanType = SmartFactoryBean.class;
    } else if (FactoryBean.class.isAssignableFrom(clazz)) {
        factoryBeanType = FactoryBean.class;
    }
    return (factoryBeanType != null && !method.getName().equals("getObject") &&
            ClassUtils.hasMethod(factoryBeanType, method.getName(), method.getParameterTypes()));
}
 
Example 4
Source Project: java-technology-stack   Source File: StaticListableBeanFactory.java    License: MIT License 6 votes vote down vote up
@Override
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
	String beanName = BeanFactoryUtils.transformedBeanName(name);

	Object bean = this.beans.get(beanName);
	if (bean == null) {
		throw new NoSuchBeanDefinitionException(beanName,
				"Defined beans are [" + StringUtils.collectionToCommaDelimitedString(this.beans.keySet()) + "]");
	}

	if (bean instanceof FactoryBean && !BeanFactoryUtils.isFactoryDereference(name)) {
		// If it's a FactoryBean, we want to look at what it creates, not the factory class.
		return ((FactoryBean<?>) bean).getObjectType();
	}
	return bean.getClass();
}
 
Example 5
Source Project: spring-analysis-note   Source File: Spr8954Tests.java    License: MIT License 6 votes vote down vote up
@Test
public void repro() {
	AnnotationConfigApplicationContext bf = new AnnotationConfigApplicationContext();
	bf.registerBeanDefinition("fooConfig", new RootBeanDefinition(FooConfig.class));
	bf.getBeanFactory().addBeanPostProcessor(new PredictingBPP());
	bf.refresh();

	assertThat(bf.getBean("foo"), instanceOf(Foo.class));
	assertThat(bf.getBean("&foo"), instanceOf(FooFactoryBean.class));

	assertThat(bf.isTypeMatch("&foo", FactoryBean.class), is(true));

	@SuppressWarnings("rawtypes")
	Map<String, FactoryBean> fbBeans = bf.getBeansOfType(FactoryBean.class);
	assertThat(1, equalTo(fbBeans.size()));
	assertThat("&foo", equalTo(fbBeans.keySet().iterator().next()));

	Map<String, AnInterface> aiBeans = bf.getBeansOfType(AnInterface.class);
	assertThat(1, equalTo(aiBeans.size()));
	assertThat("&foo", equalTo(aiBeans.keySet().iterator().next()));
}
 
Example 6
Source Project: konker-platform   Source File: SecurityTestConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public FactoryBean<Tenant> tenantFactoryBean() {
    return new FactoryBean<Tenant>() {
        @Override
        public Tenant getObject() throws Exception {
            return Tenant.builder()
                .id("tenant_id")
                .domainName("domainName")
                .name("Konker").build();
        }

        @Override
        public Class<?> getObjectType() {
            return Tenant.class;
        }

        @Override
        public boolean isSingleton() {
            return false;
        }
    };
}
 
Example 7
/**
 * Determine the type for the given FactoryBean.
 * @param factoryBean the FactoryBean instance to check
 * @return the FactoryBean's object type,
 * or {@code null} if the type cannot be determined yet
 */
protected Class<?> getTypeForFactoryBean(final FactoryBean<?> factoryBean) {
	try {
		if (System.getSecurityManager() != null) {
			return AccessController.doPrivileged(new PrivilegedAction<Class<?>>() {
				@Override
				public Class<?> run() {
					return factoryBean.getObjectType();
				}
			}, getAccessControlContext());
		}
		else {
			return factoryBean.getObjectType();
		}
	}
	catch (Throwable ex) {
		// Thrown from the FactoryBean's getObjectType implementation.
		logger.warn("FactoryBean threw exception from getObjectType, despite the contract saying " +
				"that it should return null if the type of its object cannot be determined yet", ex);
		return null;
	}
}
 
Example 8
Source Project: spring-analysis-note   Source File: AutoProxyCreatorTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBean() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
	sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);
	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(factory));

	TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(3, tapc.testInterceptor.nrOfInvocations);
}
 
Example 9
Source Project: spring-analysis-note   Source File: AutoProxyCreatorTests.java    License: MIT License 6 votes vote down vote up
@Override
@Nullable
protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String name, @Nullable TargetSource customTargetSource) {
	if (StaticMessageSource.class.equals(beanClass)) {
		return DO_NOT_PROXY;
	}
	else if (name.endsWith("ToBeProxied")) {
		boolean isFactoryBean = FactoryBean.class.isAssignableFrom(beanClass);
		if ((this.proxyFactoryBean && isFactoryBean) || (this.proxyObject && !isFactoryBean)) {
			return new Object[] {this.testInterceptor};
		}
		else {
			return DO_NOT_PROXY;
		}
	}
	else {
		return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
	}
}
 
Example 10
@Override
public String[] getBeanNamesForType(ResolvableType type) {
	boolean isFactoryType = (type != null && FactoryBean.class.isAssignableFrom(type.getRawClass()));
	List<String> matches = new ArrayList<String>();
	for (Map.Entry<String, Object> entry : this.beans.entrySet()) {
		String name = entry.getKey();
		Object beanInstance = entry.getValue();
		if (beanInstance instanceof FactoryBean && !isFactoryType) {
			Class<?> objectType = ((FactoryBean<?>) beanInstance).getObjectType();
			if (objectType != null && (type == null || type.isAssignableFrom(objectType))) {
				matches.add(name);
			}
		}
		else {
			if (type == null || type.isInstance(beanInstance)) {
				matches.add(name);
			}
		}
	}
	return StringUtils.toStringArray(matches);
}
 
Example 11
Source Project: spring-analysis-note   Source File: FactoryBeanRegistrySupport.java    License: MIT License 6 votes vote down vote up
/**
 * Determine the type for the given FactoryBean.
 * @param factoryBean the FactoryBean instance to check
 * @return the FactoryBean's object type,
 * or {@code null} if the type cannot be determined yet
 */
@Nullable
protected Class<?> getTypeForFactoryBean(final FactoryBean<?> factoryBean) {
	try {
		if (System.getSecurityManager() != null) {
			return AccessController.doPrivileged((PrivilegedAction<Class<?>>)
					factoryBean::getObjectType, getAccessControlContext());
		}
		else {
			return factoryBean.getObjectType();
		}
	}
	catch (Throwable ex) {
		// Thrown from the FactoryBean's getObjectType implementation.
		logger.info("FactoryBean threw exception from getObjectType, despite the contract saying " +
				"that it should return null if the type of its object cannot be determined yet", ex);
		return null;
	}
}
 
Example 12
protected String getClassName(Object bean, BeanDefinition beanDefinition){
    String className = null;
    if(bean instanceof FactoryBean){
        className = ((FactoryBean)bean).getObjectType().getName();
    }else if(beanDefinition instanceof AnnotatedBeanDefinition){
        MethodMetadata methodMetadata = ((AnnotatedBeanDefinition)beanDefinition).getFactoryMethodMetadata();

        if(methodMetadata != null){
            className = methodMetadata.getReturnTypeName();
        }else{
            className = ((AnnotatedBeanDefinition)beanDefinition).getMetadata().getClassName();
        }
    }else if(beanDefinition instanceof RootBeanDefinition){
        className = bean.getClass().getName();
    }else if(bean instanceof Proxy){
        className = getClassNameFromProxy(bean);
    }else{
        className = beanDefinition.getBeanClassName();
    }
    return className;
}
 
Example 13
Source Project: konker-platform   Source File: SecurityTestConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public FactoryBean<Tenant> tenantFactoryBean() {
    return new FactoryBean<Tenant>() {
        @Override
        public Tenant getObject() throws Exception {
            return Tenant.builder()
                .id("tenant_id")
                .domainName("domainName")
                .name("Konker").build();
        }

        @Override
        public Class<?> getObjectType() {
            return Tenant.class;
        }

        @Override
        public boolean isSingleton() {
            return false;
        }
    };
}
 
Example 14
Source Project: blog_demos   Source File: FactoryBeanRegistrySupport.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Determine the type for the given FactoryBean.
 * @param factoryBean the FactoryBean instance to check
 * @return the FactoryBean's object type,
 * or {@code null} if the type cannot be determined yet
 */
protected Class<?> getTypeForFactoryBean(final FactoryBean<?> factoryBean) {
	try {
		if (System.getSecurityManager() != null) {
			return AccessController.doPrivileged(new PrivilegedAction<Class<?>>() {
				@Override
				public Class<?> run() {
					return factoryBean.getObjectType();
				}
			}, getAccessControlContext());
		}
		else {
			return factoryBean.getObjectType();
		}
	}
	catch (Throwable ex) {
		// Thrown from the FactoryBean's getObjectType implementation.
		logger.warn("FactoryBean threw exception from getObjectType, despite the contract saying " +
				"that it should return null if the type of its object cannot be determined yet", ex);
		return null;
	}
}
 
Example 15
Source Project: blog_demos   Source File: AbstractBeanFactory.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Determine the bean type for the given FactoryBean definition, as far as possible.
 * Only called if there is no singleton instance registered for the target bean already.
 * <p>The default implementation creates the FactoryBean via {@code getBean}
 * to call its {@code getObjectType} method. Subclasses are encouraged to optimize
 * this, typically by just instantiating the FactoryBean but not populating it yet,
 * trying whether its {@code getObjectType} method already returns a type.
 * If no type found, a full FactoryBean creation as performed by this implementation
 * should be used as fallback.
 * @param beanName the name of the bean
 * @param mbd the merged bean definition for the bean
 * @return the type for the bean if determinable, or {@code null} else
 * @see org.springframework.beans.factory.FactoryBean#getObjectType()
 * @see #getBean(String)
 */
protected Class<?> getTypeForFactoryBean(String beanName, RootBeanDefinition mbd) {
	if (!mbd.isSingleton()) {
		return null;
	}
	try {
		FactoryBean<?> factoryBean = doGetBean(FACTORY_BEAN_PREFIX + beanName, FactoryBean.class, null, true);
		return getTypeForFactoryBean(factoryBean);
	}
	catch (BeanCreationException ex) {
		// Can only happen when getting a FactoryBean.
		if (logger.isDebugEnabled()) {
			logger.debug("Ignoring bean creation exception on FactoryBean type check: " + ex);
		}
		onSuppressedException(ex);
		return null;
	}
}
 
Example 16
Source Project: spring-cloud-task   Source File: SimpleTaskRepository.java    License: Apache License 2.0 6 votes vote down vote up
public SimpleTaskRepository(FactoryBean<TaskExecutionDao> taskExecutionDaoFactoryBean,
		Integer maxExitMessageSize, Integer maxTaskNameSize,
		Integer maxErrorMessageSize) {
	Assert.notNull(taskExecutionDaoFactoryBean,
			"A FactoryBean that provides a TaskExecutionDao is required");
	if (maxTaskNameSize != null) {
		this.maxTaskNameSize = maxTaskNameSize;
	}
	if (maxExitMessageSize != null) {
		this.maxExitMessageSize = maxExitMessageSize;
	}
	if (maxErrorMessageSize != null) {
		this.maxErrorMessageSize = maxErrorMessageSize;
	}
	this.taskExecutionDaoFactoryBean = taskExecutionDaoFactoryBean;
}
 
Example 17
Source Project: konker-platform   Source File: SecurityTestConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public FactoryBean<Tenant> tenantFactoryBean() {
    return new FactoryBean<Tenant>() {
        @Override
        public Tenant getObject() throws Exception {
            return Tenant.builder()
                .id("tenant_id")
                .domainName("domainName")
                .name("Konker").build();
        }

        @Override
        public Class<?> getObjectType() {
            return Tenant.class;
        }

        @Override
        public boolean isSingleton() {
            return false;
        }
    };
}
 
Example 18
Source Project: konker-platform   Source File: SecurityTestConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public FactoryBean<Tenant> tenantFactoryBean() {
    return new FactoryBean<Tenant>() {
        @Override
        public Tenant getObject() {
            return Tenant.builder()
                .id("tenant_id")
                .domainName("domainName")
                .name("Konker").build();
        }

        @Override
        public Class<?> getObjectType() {
            return Tenant.class;
        }

        @Override
        public boolean isSingleton() {
            return false;
        }
    };
}
 
Example 19
@Override
protected Class<?> predictBeanType(String beanName, RootBeanDefinition mbd, Class<?>... typesToMatch) {
	Class<?> targetType = determineTargetType(beanName, mbd, typesToMatch);

	// Apply SmartInstantiationAwareBeanPostProcessors to predict the
	// eventual type after a before-instantiation shortcut.
	if (targetType != null && !mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
		for (BeanPostProcessor bp : getBeanPostProcessors()) {
			if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
				SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
				Class<?> predicted = ibp.predictBeanType(targetType, beanName);
				if (predicted != null && (typesToMatch.length != 1 || FactoryBean.class != typesToMatch[0] ||
						FactoryBean.class.isAssignableFrom(predicted))) {
					return predicted;
				}
			}
		}
	}
	return targetType;
}
 
Example 20
Source Project: spring-analysis-note   Source File: BeanNameAutoProxyCreator.java    License: MIT License 5 votes vote down vote up
/**
 * Identify as bean to proxy if the bean name is in the configured list of names.
 */
@Override
@Nullable
protected Object[] getAdvicesAndAdvisorsForBean(
		Class<?> beanClass, String beanName, @Nullable TargetSource targetSource) {

	if (this.beanNames != null) {
		for (String mappedName : this.beanNames) {
			if (FactoryBean.class.isAssignableFrom(beanClass)) {
				if (!mappedName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) {
					continue;
				}
				mappedName = mappedName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length());
			}
			if (isMatch(beanName, mappedName)) {
				return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
			}
			BeanFactory beanFactory = getBeanFactory();
			if (beanFactory != null) {
				String[] aliases = beanFactory.getAliases(beanName);
				for (String alias : aliases) {
					if (isMatch(alias, mappedName)) {
						return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
					}
				}
			}
		}
	}
	return DO_NOT_PROXY;
}
 
Example 21
/**
 * Get a FactoryBean for the given bean if possible.
 * @param beanName the name of the bean
 * @param beanInstance the corresponding bean instance
 * @return the bean instance as FactoryBean
 * @throws BeansException if the given bean cannot be exposed as a FactoryBean
 */
protected FactoryBean<?> getFactoryBean(String beanName, Object beanInstance) throws BeansException {
	if (!(beanInstance instanceof FactoryBean)) {
		throw new BeanCreationException(beanName,
				"Bean instance of type [" + beanInstance.getClass() + "] is not a FactoryBean");
	}
	return (FactoryBean<?>) beanInstance;
}
 
Example 22
Source Project: COLA   Source File: ColaMockContext.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
public MockServiceModel getMonitorMockByTarget(Object bean){
    return monitorList.stream().filter(p->{
        Class targetCls = bean.getClass();
        if(bean instanceof FactoryBean){
            targetCls = ((FactoryBean)bean).getObjectType();
        }
        if(p.getInterfaceCls().isAssignableFrom(targetCls)){
            return true;
        }
        return false;
    }).findFirst().orElse(null);
}
 
Example 23
Source Project: spring-analysis-note   Source File: GroovyScriptFactoryTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testRefreshableFactoryBean() {
	ApplicationContext context = new ClassPathXmlApplicationContext("groovyContext.xml", getClass());
	Object factory = context.getBean("&refreshableFactory");
	assertTrue(factory instanceof FactoryBean);
	Object result = context.getBean("refreshableFactory");
	assertTrue(result instanceof String);
	assertEquals("test", result);
}
 
Example 24
Source Project: spring-analysis-note   Source File: Spr15275Tests.java    License: MIT License 5 votes vote down vote up
@Bean
public FactoryBean<Foo> foo() {
	return new FactoryBean<Foo>() {
		@Override
		public Foo getObject() {
			return new Foo("x");
		}
		@Override
		public Class<?> getObjectType() {
			return Foo.class;
		}
	};
}
 
Example 25
@Override
@SuppressWarnings("unchecked")
public <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
		throws BeansException {

	boolean isFactoryType = (type != null && FactoryBean.class.isAssignableFrom(type));
	Map<String, T> matches = new HashMap<String, T>();

	for (Map.Entry<String, Object> entry : this.beans.entrySet()) {
		String beanName = entry.getKey();
		Object beanInstance = entry.getValue();
		// Is bean a FactoryBean?
		if (beanInstance instanceof FactoryBean && !isFactoryType) {
			// Match object created by FactoryBean.
			FactoryBean<?> factory = (FactoryBean<?>) beanInstance;
			Class<?> objectType = factory.getObjectType();
			if ((includeNonSingletons || factory.isSingleton()) &&
					objectType != null && (type == null || type.isAssignableFrom(objectType))) {
				matches.put(beanName, getBean(beanName, type));
			}
		}
		else {
			if (type == null || type.isInstance(beanInstance)) {
				// If type to match is FactoryBean, return FactoryBean itself.
				// Else, return bean instance.
				if (isFactoryType) {
					beanName = FACTORY_BEAN_PREFIX + beanName;
				}
				matches.put(beanName, (T) beanInstance);
			}
		}
	}
	return matches;
}
 
Example 26
@Test
public void testFactoryBean() {
	ApplicationContext context = new ClassPathXmlApplicationContext("groovyContext.xml", getClass());
	Object factory = context.getBean("&factory");
	assertTrue(factory instanceof FactoryBean);
	Object result = context.getBean("factory");
	assertTrue(result instanceof String);
	assertEquals("test", result);
}
 
Example 27
@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 28
Source Project: java-technology-stack   Source File: AutoProxyCreatorTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBeanAndProxyObjectOnly() {
	StaticApplicationContext sac = new StaticApplicationContext();

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("proxyFactoryBean", "false");
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs);

	sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);

	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
	assertFalse(AopUtils.isAopProxy(factory));

	TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);

	TestBean tb2 = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertSame(tb, tb2);
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	tb2.getAge();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
Example 29
Source Project: spring-init   Source File: SampleConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public FactoryBean<Foo> foo() {
	return new FactoryBean<Foo>() {

		@Override
		public Foo getObject() throws Exception {
			return new Foo();
		}

		@Override
		public Class<?> getObjectType() {
			return Foo.class;
		}
	};
}
 
Example 30
/**
 * Introspect the factory method signatures on the given bean class,
 * trying to find a common {@code FactoryBean} object type declared there.
 * @param beanClass the bean class to find the factory method on
 * @param factoryMethodName the name of the factory method
 * @return the common {@code FactoryBean} object type, or {@code null} if none
 */
private Class<?> getTypeForFactoryBeanFromMethod(Class<?> beanClass, final String factoryMethodName) {
	class Holder { Class<?> value = null; }
	final Holder objectType = new Holder();

	// CGLIB subclass methods hide generic parameters; look at the original user class.
	Class<?> fbClass = ClassUtils.getUserClass(beanClass);

	// Find the given factory method, taking into account that in the case of
	// @Bean methods, there may be parameters present.
	ReflectionUtils.doWithMethods(fbClass,
			new ReflectionUtils.MethodCallback() {
				@Override
				public void doWith(Method method) {
					if (method.getName().equals(factoryMethodName) &&
							FactoryBean.class.isAssignableFrom(method.getReturnType())) {
						Class<?> currentType = GenericTypeResolver.resolveReturnTypeArgument(
								method, FactoryBean.class);
						if (currentType != null) {
							objectType.value = ClassUtils.determineCommonAncestor(currentType, objectType.value);
						}
					}
				}
			});

	return (objectType.value != null && Object.class != objectType.value ? objectType.value : null);
}
 
Example 31
Source Project: spring-analysis-note   Source File: BeanNamePointcutTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testMatchingFactoryBeanObject() {
	assertTrue("Matching bean must be advised (proxied)", this.testFactoryBean1 instanceof Advised);
	assertEquals("myValue", this.testFactoryBean1.get("myKey"));
	assertEquals("myValue", this.testFactoryBean1.get("myKey"));
	assertEquals("Advice not executed: must have been", 2, this.counterAspect.getCount());
	FactoryBean<?> fb = (FactoryBean<?>) ctx.getBean("&testFactoryBean1");
	assertTrue("FactoryBean itself must *not* be advised", !(fb instanceof Advised));
}
 
Example 32
Source Project: spring-analysis-note   Source File: AutoProxyCreatorTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBeanAndProxyObjectOnly() {
	StaticApplicationContext sac = new StaticApplicationContext();

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("proxyFactoryBean", "false");
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs);

	sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);

	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
	assertFalse(AopUtils.isAopProxy(factory));

	TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);

	TestBean tb2 = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertSame(tb, tb2);
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	tb2.getAge();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
Example 33
@Override
Type discoverFunctionType(Object function, String... names) {
	if (function instanceof RoutingFunction) {
		return FunctionType.of(FunctionContextUtils.findType(applicationContext.getBeanFactory(), names)).getType();
	}
	else if (function instanceof FunctionRegistration) {
		return ((FunctionRegistration) function).getType().getType();
	}
	boolean beanDefinitionExists = false;
	for (int i = 0; i < names.length && !beanDefinitionExists; i++) {
		beanDefinitionExists = this.applicationContext.getBeanFactory().containsBeanDefinition(names[i]);
		if (this.applicationContext.containsBean("&" + names[i])) {
			Class<?> objectType = this.applicationContext.getBean("&" + names[i], FactoryBean.class)
				.getObjectType();
			return FunctionTypeUtils.discoverFunctionTypeFromClass(objectType);
		}
	}
	if (!beanDefinitionExists) {
		logger.info("BeanDefinition for function name(s) '" + Arrays.asList(names) +
			"' can not be located. FunctionType will be based on " + function.getClass());
	}

	Type type = FunctionTypeUtils.discoverFunctionTypeFromClass(function.getClass());
	if (beanDefinitionExists) {
		Type t = FunctionTypeUtils.getImmediateGenericType(type, 0);
		if (t == null || t == Object.class) {
			type = FunctionType.of(FunctionContextUtils.findType(this.applicationContext.getBeanFactory(), names)).getType();
		}
	}
	return type;
}
 
Example 34
private void registerFeignClient(BeanDefinitionRegistry registry,
		AnnotationMetadata annotationMetadata, Map<String, Object> attributes) {
	String className = annotationMetadata.getClassName();
	BeanDefinitionBuilder definition = BeanDefinitionBuilder
			.genericBeanDefinition(FeignClientFactoryBean.class);
	validate(attributes);
	definition.addPropertyValue("url", getUrl(attributes));
	definition.addPropertyValue("path", getPath(attributes));
	String name = getName(attributes);
	definition.addPropertyValue("name", name);
	String contextId = getContextId(attributes);
	definition.addPropertyValue("contextId", contextId);
	definition.addPropertyValue("type", className);
	definition.addPropertyValue("decode404", attributes.get("decode404"));
	definition.addPropertyValue("fallback", attributes.get("fallback"));
	definition.addPropertyValue("fallbackFactory", attributes.get("fallbackFactory"));
	definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);

	String alias = contextId + "FeignClient";
	AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
	beanDefinition.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, className);

	// has a default, won't be null
	boolean primary = (Boolean) attributes.get("primary");

	beanDefinition.setPrimary(primary);

	String qualifier = getQualifier(attributes);
	if (StringUtils.hasText(qualifier)) {
		alias = qualifier;
	}

	BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className,
			new String[] { alias });
	BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
}
 
Example 35
Source Project: spring-analysis-note   Source File: StaticListableBeanFactory.java    License: MIT License 5 votes vote down vote up
@Override
public Object getBean(String name) throws BeansException {
	String beanName = BeanFactoryUtils.transformedBeanName(name);
	Object bean = this.beans.get(beanName);

	if (bean == null) {
		throw new NoSuchBeanDefinitionException(beanName,
				"Defined beans are [" + StringUtils.collectionToCommaDelimitedString(this.beans.keySet()) + "]");
	}

	// Don't let calling code try to dereference the
	// bean factory if the bean isn't a factory
	if (BeanFactoryUtils.isFactoryDereference(name) && !(bean instanceof FactoryBean)) {
		throw new BeanIsNotAFactoryException(beanName, bean.getClass());
	}

	if (bean instanceof FactoryBean && !BeanFactoryUtils.isFactoryDereference(name)) {
		try {
			Object exposedObject = ((FactoryBean<?>) bean).getObject();
			if (exposedObject == null) {
				throw new BeanCreationException(beanName, "FactoryBean exposed null object");
			}
			return exposedObject;
		}
		catch (Exception ex) {
			throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", ex);
		}
	}
	else {
		return bean;
	}
}
 
Example 36
Source Project: spring-analysis-note   Source File: StaticListableBeanFactory.java    License: MIT License 5 votes vote down vote up
@Override
public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
	Object bean = getBean(name);
	// In case of FactoryBean, return prototype status of created object.
	return ((bean instanceof SmartFactoryBean && ((SmartFactoryBean<?>) bean).isPrototype()) ||
			(bean instanceof FactoryBean && !((FactoryBean<?>) bean).isSingleton()));
}
 
Example 37
Source Project: spring-analysis-note   Source File: FactoryBeanRegistrySupport.java    License: MIT License 5 votes vote down vote up
/**
 * Get a FactoryBean for the given bean if possible.
 * @param beanName the name of the bean
 * @param beanInstance the corresponding bean instance
 * @return the bean instance as FactoryBean
 * @throws BeansException if the given bean cannot be exposed as a FactoryBean
 */
protected FactoryBean<?> getFactoryBean(String beanName, Object beanInstance) throws BeansException {
	if (!(beanInstance instanceof FactoryBean)) {
		throw new BeanCreationException(beanName,
				"Bean instance of type [" + beanInstance.getClass() + "] is not a FactoryBean");
	}
	return (FactoryBean<?>) beanInstance;
}
 
Example 38
@Override
public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
	Object bean = getBean(name);
	// In case of FactoryBean, return prototype status of created object.
	return ((bean instanceof SmartFactoryBean && ((SmartFactoryBean<?>) bean).isPrototype()) ||
			(bean instanceof FactoryBean && !((FactoryBean<?>) bean).isSingleton()));
}
 
Example 39
Source Project: spring-analysis-note   Source File: AbstractBeanFactory.java    License: MIT License 5 votes vote down vote up
@Override
public boolean isFactoryBean(String name) throws NoSuchBeanDefinitionException {
	String beanName = transformedBeanName(name);
	Object beanInstance = getSingleton(beanName, false);
	if (beanInstance != null) {
		return (beanInstance instanceof FactoryBean);
	}
	// No singleton instance found -> check bean definition.
	if (!containsBeanDefinition(beanName) && getParentBeanFactory() instanceof ConfigurableBeanFactory) {
		// No bean definition found in this factory -> delegate to parent.
		return ((ConfigurableBeanFactory) getParentBeanFactory()).isFactoryBean(name);
	}
	return isFactoryBean(beanName, getMergedLocalBeanDefinition(beanName));
}
 
Example 40
/**
 * Introspect the factory method signatures on the given bean class,
 * trying to find a common {@code FactoryBean} object type declared there.
 * @param beanClass the bean class to find the factory method on
 * @param factoryMethodName the name of the factory method
 * @return the common {@code FactoryBean} object type, or {@code null} if none
 */
@Nullable
private Class<?> getTypeForFactoryBeanFromMethod(Class<?> beanClass, final String factoryMethodName) {

	/**
	 * Holder used to keep a reference to a {@code Class} value.
	 */
	class Holder {

		@Nullable
		Class<?> value = null;
	}

	final Holder objectType = new Holder();

	// CGLIB subclass methods hide generic parameters; look at the original user class.
	Class<?> fbClass = ClassUtils.getUserClass(beanClass);

	// Find the given factory method, taking into account that in the case of
	// @Bean methods, there may be parameters present.
	ReflectionUtils.doWithMethods(fbClass, method -> {
		if (method.getName().equals(factoryMethodName) &&
				FactoryBean.class.isAssignableFrom(method.getReturnType())) {
			Class<?> currentType = GenericTypeResolver.resolveReturnTypeArgument(method, FactoryBean.class);
			if (currentType != null) {
				objectType.value = ClassUtils.determineCommonAncestor(currentType, objectType.value);
			}
		}
	}, ReflectionUtils.USER_DECLARED_METHODS);

	return (objectType.value != null && Object.class != objectType.value ? objectType.value : null);
}