Join the DZone community and get the full member experience.Join For Free
ABOUT SPRING CONFIGURATION
The Spring Framework has forever changed the face of enterprise Java development, making it easier than ever to configure and assemble application objects and services in a loosely-coupled manner. As you develop your Spring-enabled applications, you'll find this reference card to be a handy resource for Spring context configuration. It catalogs the XML elements available as of Spring 2.5, highlighting the most commonly used elements. In addition to Spring XML configuration, there'll also be a guide to Spring's rich set of annotations, which are useful for minimizing the amount of XML needed to configure Spring.
Dependency Injection in a Nutshell
Although the Spring Framework does many things, dependency injection is the foundational functionality provided by the Spring container.
Any non-trivial application is composed of two or more objects that collaborate to perform some business logic. Traditionally, each of those objects is responsible for obtaining references to those objects that it collaborates with (its dependencies). This leads to tightly-coupled and hard-to-test code.
With dependency injection, however, objects are given their dependencies by some external entity. In other words, dependencies are injected into the objects that need them. In the case of a Spring-enabled application, it is the Spring container that injects objects into the objects that depend on them.%$
Configuring Spring with XML
As of Spring 2.0, you are encouraged to use Spring's XML Schema-based configuration, which is more flexible than the legacy DTD-based XML. A typical Spring 2.5 configuration will have, at minimum, the following structure:
<?xml version="1.0" encoding="UTF-8"?> <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 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- place configuration details here --> </beans>
Within the <beans> element, you'll place bean declarations and other elements that configure your application's context. The "beans" namespace was the first and is still the primary namespace in Spring's XML configuration, but it isn't alone. Spring also comes with seven more namespaces that will be described in this reference card. If you wish to use one of the other namespaces, you'll need to be sure to declare them. For example, if you want to use the "context" namespace, you should declare it in XML as follows:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- place configuration details here --> </beans>
Opinions expressed by DZone contributors are their own.