Top Description Fields Constructors Methods
org.springframework.jndi.support

public Class SimpleJndiBeanFactory

extends JndiLocatorSupport
implements BeanFactory
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.BeanFactory
Imports
java.util.Collections, .HashMap, .HashSet, .Map, .Set, javax.naming.NameNotFoundException, .NamingException, org.springframework.beans.BeansException, org.springframework.beans.factory.BeanDefinitionStoreException, .BeanFactory, .BeanNotOfRequiredTypeException, .NoSuchBeanDefinitionException, .NoUniqueBeanDefinitionException, .ObjectProvider, org.springframework.core.ResolvableType, org.springframework.jndi.JndiLocatorSupport, .TypeMismatchNamingException, org.springframework.lang.Nullable

Simple JNDI-based implementation of Spring's org.springframework.beans.factory.BeanFactory interface. Does not support enumerating bean definitions, hence doesn't implement the org.springframework.beans.factory.ListableBeanFactory interface.

This factory resolves given bean names as JNDI names within the Jakarta EE application's "java:comp/env/" namespace. It caches the resolved types for all obtained objects, and optionally also caches shareable objects (if they are explicitly marked as shareable resource).

The main intent of this factory is usage in combination with Spring's org.springframework.context.annotation.CommonAnnotationBeanPostProcessor, configured as "resourceFactory" for resolving @Resource annotations as JNDI objects without intermediate bean definitions. It may be used for similar lookup scenarios as well, of course, in particular if BeanFactory-style type checking is required.

Author
Juergen Hoeller
Since
2.5
See Also
org.springframework.beans.factory.support.DefaultListableBeanFactory, org.springframework.context.annotation.CommonAnnotationBeanPostProcessor

Field Summary

Modifier and TypeField and Description
private final Map<String, Class<?>>
resourceTypes

Cache of the types of nonshareable resources: bean name to bean type.

private final Set<String>
shareableResources

JNDI names of resources that are known to be shareable, i.e. can be cached

private final Map<String, Object>
singletonObjects

Cache of shareable singleton objects: bean name to bean instance.

Inherited from org.springframework.jndi.JndiLocatorSupport:
CONTAINER_PREFIX

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addShareableResource(String
the JNDI name (typically within the "java:comp/env/" namespace)
shareableResource
)

Add the name of a shareable JNDI resource, which this factory is allowed to cache once obtained.

public boolean
containsBean(String
the name of the bean to query
name
)

Implements org.springframework.beans.factory.BeanFactory.containsBean.

Does this bean factory contain a bean definition or externally registered singleton instance with the given name?

private <T> T
doGetSingleton(String name, Class<T> requiredType)

private Class<?>
public String[]
getAliases(String
the bean name to check for aliases
name
)

Implements org.springframework.beans.factory.BeanFactory.getAliases.

Return the aliases for the given bean name, if any.

public Object
getBean(String
the name of the bean to retrieve
name
)

Implements org.springframework.beans.factory.BeanFactory.getBean.

Return an instance, which may be shared or independent, of the specified bean.

public <T> T
getBean(String
the name of the bean to retrieve
name
,
Class<T>
type the bean must match; can be an interface or superclass
requiredType
)

Implements org.springframework.beans.factory.BeanFactory.getBean.

Return an instance, which may be shared or independent, of the specified bean.

public Object
getBean(String
the name of the bean to retrieve
name
,
Object...
arguments to use when creating a bean instance using explicit arguments (only applied when creating a new instance as opposed to retrieving an existing one)
args
)

Implements org.springframework.beans.factory.BeanFactory.getBean.

Return an instance, which may be shared or independent, of the specified bean.

public <T> T
getBean(Class<T>
type the bean must match; can be an interface or superclass
requiredType
)

Implements org.springframework.beans.factory.BeanFactory.getBean.

Return the bean instance that uniquely matches the given object type, if any.

public <T> T
getBean(Class<T>
type the bean must match; can be an interface or superclass
requiredType
,
Object...
arguments to use when creating a bean instance using explicit arguments (only applied when creating a new instance as opposed to retrieving an existing one)
args
)

Implements org.springframework.beans.factory.BeanFactory.getBean.

Return an instance, which may be shared or independent, of the specified bean.

public <T> ObjectProvider<T>
getBeanProvider(Class<T>
type the bean must match; can be an interface or superclass
requiredType
)

Implements org.springframework.beans.factory.BeanFactory.getBeanProvider.

Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options.

public <T> ObjectProvider<T>
getBeanProvider(ResolvableType
type the bean must match; can be a generic type declaration
requiredType
)

Implements org.springframework.beans.factory.BeanFactory.getBeanProvider.

Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options.

public Class<?>
getType(String
the name of the bean to query
name
)

Implements org.springframework.beans.factory.BeanFactory.getType.

Determine the type of the bean with the given name.

public Class<?>
getType(String
the name of the bean to query
name
,
boolean
whether a FactoryBean may get initialized just for the purpose of determining its object type
allowFactoryBeanInit
)

Implements org.springframework.beans.factory.BeanFactory.getType.

Determine the type of the bean with the given name.

public boolean
isPrototype(String
the name of the bean to query
name
)

Implements org.springframework.beans.factory.BeanFactory.isPrototype.

Is this bean a prototype?

public boolean
isSingleton(String
the name of the bean to query
name
)

Implements org.springframework.beans.factory.BeanFactory.isSingleton.

Is this bean a shared singleton?

public boolean
isTypeMatch(String
the name of the bean to query
name
,
ResolvableType
the type to match against (as a ResolvableType)
typeToMatch
)

Implements org.springframework.beans.factory.BeanFactory.isTypeMatch.

Check whether the bean with the given name matches the specified type.

public boolean
isTypeMatch(String
the name of the bean to query
name
,
Class<?>
the type to match against (as a Class)
typeToMatch
)

Implements org.springframework.beans.factory.BeanFactory.isTypeMatch.

Check whether the bean with the given name matches the specified type.

public void
setShareableResources(String...
the JNDI names (typically within the "java:comp/env/" namespace)
shareableResources
)

Set a list of names of shareable JNDI resources, which this factory is allowed to cache once obtained.

Inherited from org.springframework.jndi.JndiLocatorSupport:
convertJndiNameisResourceReflookuplookupsetResourceRef

Field Detail

resourceTypesback to summary
private final Map<String, Class<?>> resourceTypes

Cache of the types of nonshareable resources: bean name to bean type.

shareableResourcesback to summary
private final Set<String> shareableResources

JNDI names of resources that are known to be shareable, i.e. can be cached

singletonObjectsback to summary
private final Map<String, Object> singletonObjects

Cache of shareable singleton objects: bean name to bean instance.

Constructor Detail

SimpleJndiBeanFactoryback to summary
public SimpleJndiBeanFactory()

Method Detail

addShareableResourceback to summary
public void addShareableResource(String shareableResource)

Add the name of a shareable JNDI resource, which this factory is allowed to cache once obtained.

Parameters
shareableResource:String

the JNDI name (typically within the "java:comp/env/" namespace)

containsBeanback to summary
public boolean containsBean(String name)

Implements org.springframework.beans.factory.BeanFactory.containsBean.

Doc from org.springframework.beans.factory.BeanFactory.containsBean.

Does this bean factory contain a bean definition or externally registered singleton instance with the given name?

If the given name is an alias, it will be translated back to the corresponding canonical bean name.

If this factory is hierarchical, will ask any parent factory if the bean cannot be found in this factory instance.

If a bean definition or singleton instance matching the given name is found, this method will return true whether the named bean definition is concrete or abstract, lazy or eager, in scope or not. Therefore, note that a true return value from this method does not necessarily indicate that getBean will be able to obtain an instance for the same name.

Parameters
name:String

the name of the bean to query

Returns:boolean

whether a bean with the given name is present

Annotations
@Override
doGetSingletonback to summary
private <T> T doGetSingleton(String name, Class<T> requiredType) throws NamingException
Annotations
@SuppressWarnings:unchecked
doGetTypeback to summary
private Class<?> doGetType(String name) throws NamingException
getAliasesback to summary
public String[] getAliases(String name)

Implements org.springframework.beans.factory.BeanFactory.getAliases.

Doc from org.springframework.beans.factory.BeanFactory.getAliases.

Return the aliases for the given bean name, if any.

All of those aliases point to the same bean when used in a getBean call.

If the given name is an alias, the corresponding original bean name and other aliases (if any) will be returned, with the original bean name being the first element in the array.

Will ask the parent factory if the bean cannot be found in this factory instance.

Parameters
name:String

the bean name to check for aliases

Returns:String[]

the aliases, or an empty array if none

Annotations
@Override
getBeanback to summary
public Object getBean(String name) throws BeansException

Implements org.springframework.beans.factory.BeanFactory.getBean.

Doc from org.springframework.beans.factory.BeanFactory.getBean.

Return an instance, which may be shared or independent, of the specified bean.

This method allows a Spring BeanFactory to be used as a replacement for the Singleton or Prototype design pattern. Callers may retain references to returned objects in the case of Singleton beans.

Translates aliases back to the corresponding canonical bean name.

Will ask the parent factory if the bean cannot be found in this factory instance.

Parameters
name:String

the name of the bean to retrieve

Returns:Object

an instance of the bean. Note that the return value will never be null but possibly a stub for null returned from a factory method, to be checked via equals(null). Consider using getBeanProvider(Class) for resolving optional dependencies.

Annotations
@Override
Exceptions
BeansException:
if the bean could not be obtained
getBeanback to summary
public <T> T getBean(String name, Class<T> requiredType) throws BeansException

Implements org.springframework.beans.factory.BeanFactory.getBean.

Doc from org.springframework.beans.factory.BeanFactory.getBean.

Return an instance, which may be shared or independent, of the specified bean.

Behaves the same as getBean(String), but provides a measure of type safety by throwing a BeanNotOfRequiredTypeException if the bean is not of the required type. This means that ClassCastException can't be thrown on casting the result correctly, as can happen with getBean(String).

Translates aliases back to the corresponding canonical bean name.

Will ask the parent factory if the bean cannot be found in this factory instance.

Parameters
name:String

the name of the bean to retrieve

requiredType:Class<T>

type the bean must match; can be an interface or superclass

Returns:T

an instance of the bean. Note that the return value will never be null. In case of a stub for null from a factory method having been resolved for the requested bean, a BeanNotOfRequiredTypeException against the NullBean stub will be raised. Consider using getBeanProvider(Class) for resolving optional dependencies.

Annotations
@Override
Exceptions
BeansException:
if the bean could not be created
getBeanback to summary
public Object getBean(String name, Object... args) throws BeansException

Implements org.springframework.beans.factory.BeanFactory.getBean.

Doc from org.springframework.beans.factory.BeanFactory.getBean.

Return an instance, which may be shared or independent, of the specified bean.

Allows for specifying explicit constructor arguments / factory method arguments, overriding the specified default arguments (if any) in the bean definition. Note that the provided arguments need to match a specific candidate constructor / factory method in the order of declared parameters.

Parameters
name:String

the name of the bean to retrieve

args:Object[]

arguments to use when creating a bean instance using explicit arguments (only applied when creating a new instance as opposed to retrieving an existing one)

Returns:Object

an instance of the bean

Annotations
@Override
Exceptions
BeansException:
if the bean could not be created
getBeanback to summary
public <T> T getBean(Class<T> requiredType) throws BeansException

Implements org.springframework.beans.factory.BeanFactory.getBean.

Doc from org.springframework.beans.factory.BeanFactory.getBean.

Return the bean instance that uniquely matches the given object type, if any.

This method goes into ListableBeanFactory by-type lookup territory but may also be translated into a conventional by-name lookup based on the name of the given type. For more extensive retrieval operations across sets of beans, use ListableBeanFactory and/or BeanFactoryUtils.

Parameters
requiredType:Class<T>

type the bean must match; can be an interface or superclass

Returns:T

an instance of the single bean matching the required type

Annotations
@Override
Exceptions
BeansException:
if the bean could not be created
getBeanback to summary
public <T> T getBean(Class<T> requiredType, Object... args) throws BeansException

Implements org.springframework.beans.factory.BeanFactory.getBean.

Doc from org.springframework.beans.factory.BeanFactory.getBean.

Return an instance, which may be shared or independent, of the specified bean.

Allows for specifying explicit constructor arguments / factory method arguments, overriding the specified default arguments (if any) in the bean definition. Note that the provided arguments need to match a specific candidate constructor / factory method in the order of declared parameters.

This method goes into ListableBeanFactory by-type lookup territory but may also be translated into a conventional by-name lookup based on the name of the given type. For more extensive retrieval operations across sets of beans, use ListableBeanFactory and/or BeanFactoryUtils.

Parameters
requiredType:Class<T>

type the bean must match; can be an interface or superclass

args:Object[]

arguments to use when creating a bean instance using explicit arguments (only applied when creating a new instance as opposed to retrieving an existing one)

Returns:T

an instance of the bean

Annotations
@Override
Exceptions
BeansException:
if the bean could not be created
getBeanProviderback to summary
public <T> ObjectProvider<T> getBeanProvider(Class<T> requiredType)

Implements org.springframework.beans.factory.BeanFactory.getBeanProvider.

Doc from org.springframework.beans.factory.BeanFactory.getBeanProvider.

Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options.

For matching a generic type, consider getBeanProvider(ResolvableType).

Parameters
requiredType:Class<T>

type the bean must match; can be an interface or superclass

Returns:ObjectProvider<T>

a corresponding provider handle

Annotations
@Override
getBeanProviderback to summary
public <T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType)

Implements org.springframework.beans.factory.BeanFactory.getBeanProvider.

Doc from org.springframework.beans.factory.BeanFactory.getBeanProvider.

Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options. This variant allows for specifying a generic type to match, similar to reflective injection points with generic type declarations in method/constructor parameters.

Note that collections of beans are not supported here, in contrast to reflective injection points. For programmatically retrieving a list of beans matching a specific type, specify the actual bean type as an argument here and subsequently use ObjectProvider#orderedStream() or its lazy streaming/iteration options.

Also, generics matching is strict here, as per the Java assignment rules. For lenient fallback matching with unchecked semantics (similar to the ´unchecked´ Java compiler warning), consider calling getBeanProvider(Class) with the raw type as a second step if no full generic match is available with this variant.

Parameters
requiredType:ResolvableType

type the bean must match; can be a generic type declaration

Returns:ObjectProvider<T>

a corresponding provider handle

Annotations
@Override
getTypeback to summary
public Class<?> getType(String name) throws NoSuchBeanDefinitionException

Implements org.springframework.beans.factory.BeanFactory.getType.

Doc from org.springframework.beans.factory.BeanFactory.getType.

Determine the type of the bean with the given name. More specifically, determine the type of object that getBean would return for the given name.

For a FactoryBean, return the type of object that the FactoryBean creates, as exposed by FactoryBean#getObjectType(). This may lead to the initialization of a previously uninitialized FactoryBean (see getType(String, boolean)).

Translates aliases back to the corresponding canonical bean name.

Will ask the parent factory if the bean cannot be found in this factory instance.

Parameters
name:String

the name of the bean to query

Returns:Class<?>

the type of the bean, or null if not determinable

Annotations
@Override
@Nullable
Exceptions
NoSuchBeanDefinitionException:
if there is no bean with the given name
getTypeback to summary
public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException

Implements org.springframework.beans.factory.BeanFactory.getType.

Doc from org.springframework.beans.factory.BeanFactory.getType.

Determine the type of the bean with the given name. More specifically, determine the type of object that getBean would return for the given name.

For a FactoryBean, return the type of object that the FactoryBean creates, as exposed by FactoryBean#getObjectType(). Depending on the allowFactoryBeanInit flag, this may lead to the initialization of a previously uninitialized FactoryBean if no early type information is available.

Translates aliases back to the corresponding canonical bean name.

Will ask the parent factory if the bean cannot be found in this factory instance.

Parameters
name:String

the name of the bean to query

allowFactoryBeanInit:boolean

whether a FactoryBean may get initialized just for the purpose of determining its object type

Returns:Class<?>

the type of the bean, or null if not determinable

Annotations
@Override
@Nullable
Exceptions
NoSuchBeanDefinitionException:
if there is no bean with the given name
isPrototypeback to summary
public boolean isPrototype(String name) throws NoSuchBeanDefinitionException

Implements org.springframework.beans.factory.BeanFactory.isPrototype.

Doc from org.springframework.beans.factory.BeanFactory.isPrototype.

Is this bean a prototype? That is, will getBean always return independent instances?

Note

This method returning false does not clearly indicate a singleton object. It indicates non-independent instances, which may correspond to a scoped bean as well. Use the isSingleton operation to explicitly check for a shared singleton instance.

Translates aliases back to the corresponding canonical bean name.

Will ask the parent factory if the bean cannot be found in this factory instance.

Parameters
name:String

the name of the bean to query

Returns:boolean

whether this bean will always deliver independent instances

Annotations
@Override
Exceptions
NoSuchBeanDefinitionException:
if there is no bean with the given name
isSingletonback to summary
public boolean isSingleton(String name) throws NoSuchBeanDefinitionException

Implements org.springframework.beans.factory.BeanFactory.isSingleton.

Doc from org.springframework.beans.factory.BeanFactory.isSingleton.

Is this bean a shared singleton? That is, will getBean always return the same instance?

Note

This method returning false does not clearly indicate independent instances. It indicates non-singleton instances, which may correspond to a scoped bean as well. Use the isPrototype operation to explicitly check for independent instances.

Translates aliases back to the corresponding canonical bean name.

Will ask the parent factory if the bean cannot be found in this factory instance.

Parameters
name:String

the name of the bean to query

Returns:boolean

whether this bean corresponds to a singleton instance

Annotations
@Override
Exceptions
NoSuchBeanDefinitionException:
if there is no bean with the given name
isTypeMatchback to summary
public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException

Implements org.springframework.beans.factory.BeanFactory.isTypeMatch.

Doc from org.springframework.beans.factory.BeanFactory.isTypeMatch.

Check whether the bean with the given name matches the specified type. More specifically, check whether a getBean call for the given name would return an object that is assignable to the specified target type.

Translates aliases back to the corresponding canonical bean name.

Will ask the parent factory if the bean cannot be found in this factory instance.

Parameters
name:String

the name of the bean to query

typeToMatch:ResolvableType

the type to match against (as a ResolvableType)

Returns:boolean

true if the bean type matches, false if it doesn't match or cannot be determined yet

Annotations
@Override
Exceptions
NoSuchBeanDefinitionException:
if there is no bean with the given name
isTypeMatchback to summary
public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException

Implements org.springframework.beans.factory.BeanFactory.isTypeMatch.

Doc from org.springframework.beans.factory.BeanFactory.isTypeMatch.

Check whether the bean with the given name matches the specified type. More specifically, check whether a getBean call for the given name would return an object that is assignable to the specified target type.

Translates aliases back to the corresponding canonical bean name.

Will ask the parent factory if the bean cannot be found in this factory instance.

Parameters
name:String

the name of the bean to query

typeToMatch:Class<?>

the type to match against (as a Class)

Returns:boolean

true if the bean type matches, false if it doesn't match or cannot be determined yet

Annotations
@Override
Exceptions
NoSuchBeanDefinitionException:
if there is no bean with the given name
setShareableResourcesback to summary
public void setShareableResources(String... shareableResources)

Set a list of names of shareable JNDI resources, which this factory is allowed to cache once obtained.

Parameters
shareableResources:String[]

the JNDI names (typically within the "java:comp/env/" namespace)