Besides the @Import, the @ImportResource can also be used to import configuration
·
from XML files, which means you can use XML and Java configuration classes in a
mix-and-match way, although we do not recommend doing that. Mixing XML and
Java configuration will make your application harder to maintain, because you
need to scan through both XML files and Java classes to search for a specific bean.
The @ComponentScan defines the packages that Spring should scan for annotations
·
for bean definitions. It's the same as the <context:component-scan> tag in the XML
configuration.
The other annotations are quite self-explanatory. The @Lazy annotation instructs
·
Spring to instantiate the bean only when requested (same as the lazy-init="true"
in XML), and @DependsOn tells Spring that a certain bean depends on some other
beans, so Spring will make sure that those beans will be instantiated first. The
@Scope annotation is to define the bean's scope.
Application infrastructure services can also be defined in Java classes. For
·
example, the @EnableTransactionManagement defines that we will use Spring's
transaction management feature, which will be discussed further in Chapter 12.
You may also notice the @Autowired property of the env variable, which is of the
·
Environment type. This is the Environment abstraction feature that Spring 3.1
provides. We will discuss it later in this chapter.
Running the testing program again yields the following output:
Spring 3 Java Configuration Rocks!
This is the message defined in the message.properties file.
Java or XML Configuration?
As you already saw, using Java classes can achieve the same level of ApplicationContext configuration as
XML. So, which one should you use? The consideration is quite like the one of whether we should use
XML or Java annotations for DI configuration. Each approach has its own pros and cons. However, the
recommendation is the same; that is, when you and your team decide on the approach to use, stick to it
and keep the configuration style persistent, instead of scattered around between Java class and XML
files. This will make the maintenance work much more difficult.
Profiles
Another interesting feature that Spring 3.1 brings to us as developers is the concept of configuration
profiles. Basically, a profile instructs Spring to configure only the ApplicationContext that was defined
when the specified profile was active. In this section we'll demonstrate the usage of profiles in a simple
program.
An Example of Using the Spring Profiles Feature
Let's say there is a service called FoodProviderService that is responsible for providing food to schools,
including kindergarten and high school. The FoodProviderService interface has only one method called
provideLunchSet(), which will produce the lunch set to each student for the calling school. A lunch set
is a list of Food objects, which is very simple class that has only a name attribute. Listing 5-49 shows the
Food class.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home