Core Technologies
Core Technologies
Core Technologies
Version 5.3.22
Back to index
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 1/23
7/31/22, 5:51 PM Core Technologies
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 2/23
7/31/22, 5:51 PM Core Technologies
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 3/23
7/31/22, 5:51 PM Core Technologies
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 4/23
7/31/22, 5:51 PM Core Technologies
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 5/23
7/31/22, 5:51 PM Core Technologies
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 6/23
7/31/22, 5:51 PM Core Technologies
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 7/23
7/31/22, 5:51 PM Core Technologies
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 8/23
7/31/22, 5:51 PM Core Technologies
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 9/23
7/31/22, 5:51 PM Core Technologies
DefaultAdvisorAutoProxyCreator
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 10/23
7/31/22, 5:51 PM Core Technologies
Using <util:map/>
Using <util:set/>
10.1.2. The aop Schema
10.1.3. The context Schema
Using <property-placeholder/>
Using <annotation-config/>
Using <component-scan/>
Using <load-time-weaver/>
Using <spring-configured/>
Using <mbean-export/>
10.1.4. The Beans Schema
10.2. XML Schema Authoring
10.2.1. Authoring the Schema
10.2.2. Coding a NamespaceHandler
10.2.3. Using BeanDefinitionParser
10.2.4. Registering the Handler and the Schema
Writing META-INF/spring.handlers
Writing 'META-INF/spring.schemas'
10.2.5. Using a Custom Extension in Your Spring XML Configuration
10.2.6. More Detailed Examples
Nesting Custom Elements within Custom Elements
Custom Attributes on “Normal” Elements
10.3. Application Startup Steps
This part of the reference documentation covers all the technologies that are absolutely integral to the
Spring Framework.
Foremost amongst these is the Spring Framework’s Inversion of Control (IoC) container. A thorough
treatment of the Spring Framework’s IoC container is closely followed by comprehensive coverage of
Spring’s Aspect-Oriented Programming (AOP) technologies. The Spring Framework has its own AOP
framework, which is conceptually easy to understand and which successfully addresses the 80% sweet
spot of AOP requirements in Java enterprise programming.
Coverage of Spring’s integration with AspectJ (currently the richest — in terms of features — and
certainly most mature AOP implementation in the Java enterprise space) is also provided.
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 11/23
7/31/22, 5:51 PM Core Technologies
The org.springframework.beans and org.springframework.context packages are the basis for Spring
Framework’s IoC container. The BeanFactory interface provides an advanced configuration mechanism
capable of managing any type of object. ApplicationContext is a sub-interface of BeanFactory . It
adds:
Event publication
Application-layer specific contexts such as the WebApplicationContext for use in web applications.
In short, the BeanFactory provides the configuration framework and basic functionality, and
the ApplicationContext adds more enterprise-specific functionality. The ApplicationContext is a
complete superset of the BeanFactory and is used exclusively in this chapter in descriptions of Spring’s
IoC container. For more information on using the BeanFactory instead of the ApplicationContext, see
the section covering the BeanFactory API.
In Spring, the objects that form the backbone of your application and that are managed by the Spring
IoC container are called beans. A bean is an object that is instantiated, assembled, and managed by a
Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and
the dependencies among them, are reflected in the configuration metadata used by a container.
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 12/23
7/31/22, 5:51 PM Core Technologies
The configuration metadata is represented in XML, Java annotations, or Java code. It lets you express
the objects that compose your application and the rich interdependencies between those objects.
Several implementations of the ApplicationContext interface are supplied with Spring. In stand-alone
applications, it is common to create an instance
of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext . While XML has been the
traditional format for defining configuration metadata, you can instruct the container to use Java
annotations or code as the metadata format by providing a small amount of XML configuration to
declaratively enable support for these additional metadata formats.
In most application scenarios, explicit user code is not required to instantiate one or more instances of
a Spring IoC container. For example, in a web application scenario, a simple eight (or so) lines of
boilerplate web descriptor XML in the web.xml file of the application typically suffices (see Convenient
ApplicationContext Instantiation for Web Applications). If you use the Spring Tools for Eclipse (an
Eclipse-powered development environment), you can easily create this boilerplate configuration with a
few mouse clicks or keystrokes.
The following diagram shows a high-level view of how Spring works. Your application classes are
combined with configuration metadata so that, after the ApplicationContext is created and initialized,
you have a fully configured and executable system or application.
Configuration metadata is traditionally supplied in a simple and intuitive XML format, which is what most
of this chapter uses to convey key concepts and features of the Spring IoC container.
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 13/23
7/31/22, 5:51 PM Core Technologies
XML-based metadata is not the only allowed form of configuration metadata. The Spring IoC
container itself is totally decoupled from the format in which this configuration metadata is actually
written. These days, many developers choose Java-based configuration for their Spring
applications.
For information about using other forms of metadata with the Spring container, see:
Java-based configuration: Starting with Spring 3.0, many features provided by the Spring
JavaConfig project became part of the core Spring Framework. Thus, you can define beans external
to your application classes by using Java rather than XML files. To use these new features, see
the @Configuration , @Bean , @Import , and @DependsOn annotations.
Spring configuration consists of at least one and typically more than one bean definition that the
container must manage. XML-based configuration metadata configures these beans
as <bean/> elements inside a top-level <beans/> element. Java configuration typically uses @Bean -
annotated methods within a @Configuration class.
These bean definitions correspond to the actual objects that make up your application. Typically, you
define service layer objects, data access objects (DAOs), presentation objects such as
Struts Action instances, infrastructure objects such as Hibernate SessionFactories , JMS Queues ,
and so forth. Typically, one does not configure fine-grained domain objects in the container, because it
is usually the responsibility of DAOs and business logic to create and load domain objects. However,
you can use Spring’s integration with AspectJ to configure objects that have been created outside the
control of an IoC container. See Using AspectJ to dependency-inject domain objects with Spring.
The following example shows the basic structure of XML-based configuration metadata:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
</bean>
</bean>
</beans>
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 14/23
7/31/22, 5:51 PM Core Technologies
The class attribute defines the type of the bean and uses the fully qualified
classname.
The value of the id attribute refers to collaborating objects. The XML for referring to collaborating
objects is not shown in this example. See Dependencies for more information.
Java Kotlin
After you learn about Spring’s IoC container, you may want to know more about
Spring’s Resource abstraction (as described in Resources), which provides a convenient
mechanism for reading an InputStream from locations defined in a URI syntax. In
particular, Resource paths are used to construct applications contexts, as described
in Application Contexts and Resource Paths.
The following example shows the service layer objects (services.xml) configuration file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- additional collaborators and configuration for this bean go here -->
</bean>
</beans>
The following example shows the data access objects daos.xml file:
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 15/23
7/31/22, 5:51 PM Core Technologies
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="accountDao"
class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao">
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for data access objects go here -->
</beans>
In the preceding example, the service layer consists of the PetStoreServiceImpl class and two data
access objects of the types JpaAccountDao and JpaItemDao (based on the JPA Object-Relational
Mapping standard). The property name element refers to the name of the JavaBean property, and
the ref element refers to the name of another bean definition. This linkage
between id and ref elements expresses the dependency between collaborating objects. For details
of configuring an object’s dependencies, see Dependencies.
You can use the application context constructor to load bean definitions from all these XML fragments.
This constructor takes multiple Resource locations, as was shown in the previous section. Alternatively,
use one or more occurrences of the <import/> element to load bean definitions from another file or
files. The following example shows how to do so:
<beans>
<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>
</beans>
In the preceding example, external bean definitions are loaded from three
files: services.xml , messageSource.xml , and themeSource.xml . All location paths are relative to the
definition file doing the importing, so services.xml must be in the same directory or classpath location
as the file doing the importing, while messageSource.xml and themeSource.xml must be in
a resources location below the location of the importing file. As you can see, a leading slash is
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 16/23
7/31/22, 5:51 PM Core Technologies
ignored. However, given that these paths are relative, it is better form not to use the slash at all. The
contents of the files being imported, including the top level <beans/> element, must be valid XML bean
definitions, according to the Spring Schema.
It is possible, but not recommended, to reference files in parent directories using a relative "../"
path. Doing so creates a dependency on a file that is outside the current application. In particular,
this reference is not recommended for classpath: URLs (for
example, classpath:../services.xml ), where the runtime resolution process chooses the
“nearest” classpath root and then looks into its parent directory. Classpath configuration changes
may lead to the choice of a different, incorrect directory.
You can always use fully qualified resource locations instead of relative paths: for
example, file:C:/config/services.xml or classpath:/config/services.xml . However, be aware
that you are coupling your application’s configuration to specific absolute locations. It is generally
preferable to keep an indirection for such absolute locations — for example, through "${…}"
placeholders that are resolved against JVM system properties at runtime.
The namespace itself provides the import directive feature. Further configuration features beyond plain
bean definitions are available in a selection of XML namespaces provided by Spring — for example,
the context and util namespaces.
beans {
dataSource(BasicDataSource) {
driverClassName = "org.hsqldb.jdbcDriver"
url = "jdbc:hsqldb:mem:grailsDB"
username = "sa"
password = ""
settings = [mynew:"setting"]
sessionFactory(SessionFactory) {
dataSource = dataSource
myService(MyService) {
dataSource = dataSource
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 17/23
7/31/22, 5:51 PM Core Technologies
This configuration style is largely equivalent to XML bean definitions and even supports Spring’s XML
configuration namespaces. It also allows for importing XML bean definition files through
an importBeans directive.
The ApplicationContext lets you read bean definitions and access them, as the following example
shows:
Java Kotlin
With Groovy configuration, bootstrapping looks very similar. It has a different context implementation
class which is Groovy-aware (but also understands XML bean definitions). The following example
shows Groovy configuration:
Java Kotlin
The most flexible variant is GenericApplicationContext in combination with reader delegates — for
example, with XmlBeanDefinitionReader for XML files, as the following example shows:
Java Kotlin
context.refresh();
You can also use the GroovyBeanDefinitionReader for Groovy files, as the following example shows:
Java Kotlin
context.refresh();
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 18/23
7/31/22, 5:51 PM Core Technologies
You can mix and match such reader delegates on the same ApplicationContext , reading bean
definitions from diverse configuration sources.
You can then use getBean to retrieve instances of your beans. The ApplicationContext interface has
a few other methods for retrieving beans, but, ideally, your application code should never use them.
Indeed, your application code should have no calls to the getBean() method at all and thus have no
dependency on Spring APIs at all. For example, Spring’s integration with web frameworks provides
dependency injection for various web framework components such as controllers and JSF-managed
beans, letting you declare a dependency on a specific bean through metadata (such as an autowiring
annotation).
Within the container itself, these bean definitions are represented as BeanDefinition objects, which
contain (among other information) the following metadata:
A package-qualified class name: typically, the actual implementation class of the bean being
defined.
Bean behavioral configuration elements, which state how the bean should behave in the container
(scope, lifecycle callbacks, and so forth).
References to other beans that are needed for the bean to do its work. These references are also
called collaborators or dependencies.
Other configuration settings to set in the newly created object — for example, the size limit of the
pool or the number of connections to use in a bean that manages a connection pool.
This metadata translates to a set of properties that make up each bean definition. The following table
describes these properties:
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 19/23
7/31/22, 5:51 PM Core Technologies
In addition to bean definitions that contain information on how to create a specific bean,
the ApplicationContext implementations also permit the registration of existing objects that are
created outside the container (by users). This is done by accessing the
ApplicationContext’s BeanFactory through the getBeanFactory() method, which returns
the DefaultListableBeanFactory implementation. DefaultListableBeanFactory supports this
registration through the registerSingleton(..) and registerBeanDefinition(..) methods. However,
typical applications work solely with beans defined through regular bean definition metadata.
Bean metadata and manually supplied singleton instances need to be registered as early as
possible, in order for the container to properly reason about them during autowiring and other
introspection steps. While overriding existing metadata and existing singleton instances is
supported to some degree, the registration of new beans at runtime (concurrently with live access
to the factory) is not officially supported and may lead to concurrent access exceptions,
inconsistent state in the bean container, or both.
In XML-based configuration metadata, you use the id attribute, the name attribute, or both to specify
the bean identifiers. The id attribute lets you specify exactly one id. Conventionally, these names are
alphanumeric ('myBean', 'someService', etc.), but they can contain special characters as well. If you
want to introduce other aliases for the bean, you can also specify them in the name attribute, separated
by a comma ( , ), semicolon ( ; ), or white space. As a historical note, in versions prior to Spring 3.1,
the id attribute was defined as an xsd:ID type, which constrained possible characters. As of 3.1, it is
defined as an xsd:string type. Note that bean id uniqueness is still enforced by the container,
though no longer by XML parsers.
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 20/23
7/31/22, 5:51 PM Core Technologies
You are not required to supply a name or an id for a bean. If you do not supply
a name or id explicitly, the container generates a unique name for that bean. However, if you want to
refer to that bean by name, through the use of the ref element or a Service Locator style lookup, you
must provide a name. Motivations for not supplying a name are related to using inner
beans and autowiring collaborators.
Naming beans consistently makes your configuration easier to read and understand. Also, if you
use Spring AOP, it helps a lot when applying advice to a set of beans related by name.
With component scanning in the classpath, Spring generates bean names for unnamed
components, following the rules described earlier: essentially, taking the simple class name and
turning its initial character to lower-case. However, in the (unusual) special case when there is
more than one character and both the first and second characters are upper case, the original
casing gets preserved. These are the same rules as defined
by java.beans.Introspector.decapitalize (which Spring uses here).
Specifying all aliases where the bean is actually defined is not always adequate, however. It is
sometimes desirable to introduce an alias for a bean that is defined elsewhere. This is commonly the
case in large systems where configuration is split amongst each subsystem, with each subsystem
having its own set of object definitions. In XML-based configuration metadata, you can use
the <alias/> element to accomplish this. The following example shows how to do so:
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 21/23
7/31/22, 5:51 PM Core Technologies
In this case, a bean (in the same container) named fromName may also, after the use of this alias
definition, be referred to as toName .
For example, the configuration metadata for subsystem A may refer to a DataSource by the name
of subsystemA-dataSource . The configuration metadata for subsystem B may refer to a DataSource by
the name of subsystemB-dataSource . When composing the main application that uses both these
subsystems, the main application refers to the DataSource by the name of myApp-dataSource . To have
all three names refer to the same object, you can add the following alias definitions to the configuration
metadata:
Now each component and the main application can refer to the dataSource through a name that is
unique and guaranteed not to clash with any other definition (effectively creating a namespace), yet
they refer to the same bean.
Java-configuration
If you use Javaconfiguration, the @Bean annotation can be used to provide aliases. See Using
the @Bean Annotation for details.
If you use XML-based configuration metadata, you specify the type (or class) of object that is to be
instantiated in the class attribute of the <bean/> element. This class attribute (which, internally, is
a Class property on a BeanDefinition instance) is usually mandatory. (For exceptions,
see Instantiation by Using an Instance Factory Method and Bean Definition Inheritance.) You can use
the Class property in one of two ways:
Typically, to specify the bean class to be constructed in the case where the container itself directly
creates the bean by calling its constructor reflectively, somewhat equivalent to Java code with
the new operator.
To specify the actual class containing the static factory method that is invoked to create the
object, in the less common case where the container invokes a static factory method on a class
to create the bean. The object type returned from the invocation of the static factory method may
be the same class or another class entirely.
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 22/23
7/31/22, 5:51 PM Core Technologies
For example, if you have a class called SomeThing in the com.example package, and
this SomeThing class has a static nested class called OtherThing , they can be separated by a
dollar sign ( $ ) or a dot ( . ). So the value of the class attribute in a bean definition would
be com.example.SomeThing$OtherThing or com.example.SomeThing.OtherThing .
The Spring IoC container can manage virtually any class you want it to manage. It is not limited to
managing true JavaBeans. Most Spring users prefer actual JavaBeans with only a default (no-
argument) constructor and appropriate setters and getters modeled after the properties in the container.
You can also have more exotic non-bean-style classes in your container. If, for example, you need to
use a legacy connection pool that absolutely does not adhere to the JavaBean specification, Spring can
manage it as well.
With XML-based configuration metadata you can specify your bean class as follows:
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core 23/23