The location path or paths passed to the ApplicationContext
constructor are resource strings that allow
the container to load from various external resources such as the local file system, Java CLASSPATH
,
and so on.
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
val context = ClassPathXmlApplicationContext("services.xml", "daos.xml")
After exploring the Spring IoC container, you may want to learn more about the Spring Resource
abstraction, which provides a convenient mechanism for reading InputStreams from places defined in the URI
syntax. In particular, Resource
paths are used to build application contexts.
The following example shows the service layer object configuration file (services.xml)
:
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- services -->
<bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="itemDao" ref="itemDao"/>
<!-- additional interoperable objects and configuration for this bean are found here -->
</bean>
<!-- more bean definitions for services are here -->
</beans>
The following example shows the daos.xml
data access objects file:
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="accountDao"
class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao">
<!-- additional interoperable objects and configuration for this bean are found here -->
</bean>
<bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao">
<!-- additional interoperable objects and configuration for this bean are found here -->
</bean>
<!-- more bean definitions for data access objects are here -->
</beans>
In the previous example, the service layer consists of a class PetStoreServiceImpl
and two data access
objects of types JpaAccountDao
and JpaItemDao
(based on the object-relational standard JPA
Object-Relational Mapping). The property name
element refers to the name of a JavaBean property, and
the ref
element refers to the name of another bean definition. This relationship between the
id
and ref
elements expresses the dependency between interacting objects.
Compiling configuration metadata based on XML
This can be useful if the bean definitions span multiple XML files. Typically, a single XML configuration file represents a logical layer or module in the architecture.
You can use the application context designer to load bean definitions from all of these XML fragments. This
constructor occupies multiple Resource
locations. Alternatively, use one or more occurrences of the
element to load bean definitions from another file or files. The following example shows how to do this:
<beans>
<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>
<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
</beans>
In the previous example, external bean definitions are loaded from three files: services.xml
, messageSource.xml
and themeSource.xml.
All location paths refer to the definition file doing the import, so services.xml
must be in the same directory or classpath as the file doing the import, and messageSource.xml
and
themeSource.xml
should be located in resources
, located below the location of the
imported file. As you can see, the leading slash is ignored. However, given that these paths are relative, it is
better not to use the slash at all. The content of the imported files, including the top-level
<bean/>
element, must be a valid XML bean definition according to the Spring Schema.
It is possible, but not recommended, to link to files in parent directories using the relative path "../". This
creates a dependency on a file that is outside of the current application. In particular, such a reference is
not recommended for classpath:
(for example, classpath:../services.xml
), where the
"closest" one is selected using a run-time resolution procedure the root of the classpath and then its parent
directory is looked up. Changes to the class path configuration may result in a different, incorrect directory
being selected.
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, keep in mind that you are tying your application's
configuration to specific absolute locations. It is usually preferable to preserve indirect addressing for such
absolute locations - for example, through "${....}" placeholders that are resolved against JVM system properties
at runtime.
The namespace itself provides the ability to use the import directive. Additional configuration options beyond simple
bean definitions are available in some of the XML namespaces provided by Spring (for example, the
context
and util
namespaces).
Defining bins using Groovy DSL
As an additional example of metadata whose configuration is externalized, bean definitions can also be expressed using a definition via the Groovy DSL in Spring, as is known from the Grails framework. Typically this configuration is found in a ".groovy" file and has the structure shown in the following example:
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
}
}
}
This configuration style is largely equivalent to XML bean definitions and even supports Spring configuration XML
namespaces. It also allows you to import XML bean definition files using the importBeans
directive.