spring-framework / org.springframework.beans.factory.support / AbstractBeanDefinitionReader

AbstractBeanDefinitionReader

abstract class AbstractBeanDefinitionReader : EnvironmentCapable, BeanDefinitionReader

Abstract base class for bean definition readers which implement the BeanDefinitionReader interface.

Provides common properties like the bean factory to work on and the class loader to use for loading bean classes.

Author
Juergen Hoeller

Author
Chris Beams

Since
11.12.2003

See Also
BeanDefinitionReaderUtils

Functions

getBeanClassLoader

open fun getBeanClassLoader(): ClassLoader

getBeanFactory

fun getBeanFactory(): BeanDefinitionRegistry

getBeanNameGenerator

open fun getBeanNameGenerator(): BeanNameGenerator

getEnvironment

open fun getEnvironment(): Environment

getRegistry

fun getRegistry(): BeanDefinitionRegistry

getResourceLoader

open fun getResourceLoader(): ResourceLoader

loadBeanDefinitions

open fun loadBeanDefinitions(vararg resources: Resource): Int
open fun loadBeanDefinitions(location: String): Int
open fun loadBeanDefinitions(vararg locations: String): Intopen fun loadBeanDefinitions(location: String, actualResources: MutableSet<Resource>): Int

Load bean definitions from the specified resource location.

The location can also be a location pattern, provided that the ResourceLoader of this bean definition reader is a ResourcePatternResolver.

setBeanClassLoader

open fun setBeanClassLoader(beanClassLoader: ClassLoader): Unit

Set the ClassLoader to use for bean classes.

Default is null, which suggests to not load bean classes eagerly but rather to just register bean definitions with class names, with the corresponding Classes to be resolved later (or never).

setBeanNameGenerator

open fun setBeanNameGenerator(beanNameGenerator: BeanNameGenerator): Unit

Set the BeanNameGenerator to use for anonymous beans (without explicit bean name specified).

Default is a DefaultBeanNameGenerator.

setEnvironment

open fun setEnvironment(environment: Environment): Unit

Set the Environment to use when reading bean definitions. Most often used for evaluating profile information to determine which bean definitions should be read and which should be omitted.

setResourceLoader

open fun setResourceLoader(resourceLoader: ResourceLoader): Unit

Set the ResourceLoader to use for resource locations. If specifying a ResourcePatternResolver, the bean definition reader will be capable of resolving resource patterns to Resource arrays.

Default is PathMatchingResourcePatternResolver, also capable of resource pattern resolving through the ResourcePatternResolver interface.

Setting this to null suggests that absolute resource loading is not available for this bean definition reader.

Inheritors

GroovyBeanDefinitionReader

open class GroovyBeanDefinitionReader : AbstractBeanDefinitionReader, GroovyObject

A Groovy-based reader for Spring bean definitions: like a Groovy builder, but more of a DSL for Spring configuration.

This bean definition reader also understands XML bean definition files, allowing for seamless mixing and matching with Groovy bean definition files.

Typically applied to a org.springframework.beans.factory.support.DefaultListableBeanFactory or a org.springframework.context.support.GenericApplicationContext, but can be used against any BeanDefinitionRegistry implementation.

Example Syntax
 import org.hibernate.SessionFactory import org.apache.commons.dbcp.BasicDataSource def reader = new GroovyBeanDefinitionReader(myApplicationContext) reader.beans { dataSource(BasicDataSource) { // <--- invokeMethod driverClassName = "org.hsqldb.jdbcDriver" url = "jdbc:hsqldb:mem:grailsDB" username = "sa" // <-- setProperty password = "" settings = [mynew:"setting"] } sessionFactory(SessionFactory) { dataSource = dataSource // <-- getProperty for retrieving references } myService(MyService) { nestedBean = { AnotherBean bean -> // <-- setProperty with closure for nested bean dataSource = dataSource } } }

You can also load resources containing beans defined in a Groovy script using either the #loadBeanDefinitions(Resource...) or #loadBeanDefinitions(String...) method, with a script looking similar to the following.

 import org.hibernate.SessionFactory import org.apache.commons.dbcp.BasicDataSource beans { dataSource(BasicDataSource) { driverClassName = "org.hsqldb.jdbcDriver" url = "jdbc:hsqldb:mem:grailsDB" username = "sa" password = "" settings = [mynew:"setting"] } sessionFactory(SessionFactory) { dataSource = dataSource } myService(MyService) { nestedBean = { AnotherBean bean -> dataSource = dataSource } } }

PropertiesBeanDefinitionReader

open class PropertiesBeanDefinitionReader : AbstractBeanDefinitionReader

Bean definition reader for a simple properties format.

Provides bean definition registration methods for Map/Properties and ResourceBundle. Typically applied to a DefaultListableBeanFactory.

Example:

 employee.(class)=MyClass // bean is of class MyClass employee.(abstract)=true // this bean can't be instantiated directly employee.group=Insurance // real property employee.usesDialUp=false // real property (potentially overridden) salesrep.(parent)=employee // derives from "employee" bean definition salesrep.(lazy-init)=true // lazily initialize this singleton bean salesrep.manager(ref)=tony // reference to another bean salesrep.department=Sales // real property techie.(parent)=employee // derives from "employee" bean definition techie.(scope)=prototype // bean is a prototype (not a shared instance) techie.manager(ref)=jeff // reference to another bean techie.department=Engineering // real property techie.usesDialUp=true // real property (overriding parent value) ceo.$0(ref)=secretary // inject 'secretary' bean as 0th constructor arg ceo.$1=1000000 // inject value '1000000' at 1st constructor arg