25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3 ■ Applying Different Configurations<br />

In Listing 3-14, you are now using the GenericGroovyApplicationContext class that will load your Groovy script,<br />

by passing the groovy file's classpath where it is located (META-INF/spring/mydocuments.groovy); it will start up<br />

your <strong>Spring</strong> Container by creating all the necessary instances, wiring up your classes and having them ready when you<br />

need them. Remember that you are now using Groovy! to configure your <strong>Spring</strong> container.<br />

And now run your unit test with the following command:<br />

gradle test<br />

Which Configuration to Choose?<br />

Every type of configuration has some differences and some applicable usage (see Table 3-3).<br />

Table 3-3. Differences and Usage<br />

Type of Configuration<br />

XML<br />

Annotations<br />

Java Bean Configuration<br />

Groovy Bean Configuration<br />

Usage<br />

This can be used with third party libraries and/or different development<br />

environments. It’s easy to read and follow but it can grow, and even though it can be<br />

split into different files, you need to keep track of them.<br />

It is another way to do configurations, but here you are attaching the <strong>Spring</strong><br />

context to your application. This can be avoided by using your own domain-based<br />

annotations and just these can be annotated.<br />

This is now one of the preferred methods for developers who hate to struggle with<br />

the XML, and it can be used for beans or components that don’t change too much.<br />

This is a new complete feature that can be used with the power of the Groovy<br />

programming language; it’s simple and there’s less to type.<br />

So it will depend on your needs and how you want to manage your development cycle. Or you may have a team<br />

that prefers some configuration because it is easy to manage and doesn’t affect your development goals.<br />

Summary<br />

In this chapter, you saw ways to accomplish the same behavior using different styles or conventions to create<br />

beans. You saw the different classes that load up your beans, such as ClassPathXmlApplicationContext,<br />

AnnotationConfigApplicationContext, and the new GenericGroovyApplicationContext.<br />

You saw how you can annotate classes so the <strong>Spring</strong> container knows about the beans to use, its dependencies,<br />

and relationship with other classes. You saw how the @Repository marks your class as a <strong>Spring</strong>-managed component<br />

that act as a data access object.<br />

Also, you saw how to use the Java configuration class and its @Configuration and @Bean annotations to create<br />

the beans and their relationship. And you saw how to use Groovy to expose the beans using the Groovy programming<br />

language.<br />

In the next chapter, you are going to start adding more features to your <strong>Spring</strong> application, My Documents.<br />

You’ll learn how the <strong>Spring</strong> container initiates your classes and the different approaches you can take in order to get<br />

different instances of the same class.<br />

43

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!