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 1 ■ Your First <strong>Spring</strong> Application<br />

Then, in your main method, you are calling the <strong>Spring</strong> container by using the ApplicationContext class. This<br />

class, with help from the other annotations (@Config, @ComponentScan, and @Bean), will create the container and wire<br />

everything up for ready to use, so when you call the context.getBean method the <strong>Spring</strong> Container already knows<br />

what object to use.<br />

Note that if you change your implementation, it will be the only class to change; the other classes will remain<br />

intact. This will create an extensible and robust application, even if it is as simple as the Hello World example.<br />

■ ■Note If you need to get more information about dependency injection and how it is used with the <strong>Spring</strong> <strong>Framework</strong>,<br />

I recommend the Pro <strong>Spring</strong> series of books from Apress.<br />

In the following chapters, you will get more details on all of the features of the <strong>Spring</strong> <strong>Framework</strong>, the <strong>Spring</strong><br />

Extensions, and subprojects, plus how you can use them.<br />

<strong>Spring</strong> Boot: Even Easier<br />

With the release of the <strong>Spring</strong> <strong>Framework</strong> 4, the <strong>Spring</strong> team also released a new extension of the <strong>Spring</strong> technology:<br />

<strong>Spring</strong> Boot. You are going to do a small example (the famous “Hello World”) using this new technology and yes, this<br />

is also <strong>Spring</strong>.<br />

<strong>Spring</strong> Boot makes development even easier. Any <strong>Spring</strong> application can be reduced to less code with minimum<br />

effort, and it will be production-ready. Let’s create the folder structure and add the following files:<br />

• build.gradle<br />

••• src<br />

••• main<br />

••• java<br />

••• com<br />

••• apress<br />

••• isf<br />

••• spring<br />

• HelloWorldController.java<br />

This shows the structure you are going to use and the HelloWorldController.java file that it will run. Note that<br />

it is necessary to create your build.gradle file to run your example (see Listing 1-5).<br />

Listing 1-5. build.gradle<br />

apply plugin: 'application'<br />

mainClassName = System.getProperty("mainClass")<br />

dependencies {<br />

compile("org.springframework.boot:spring-boot-starter-web:1.0.2.RELEASE")<br />

}<br />

repositories {<br />

mavenCentral()<br />

maven {<br />

8

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

Saved successfully!

Ooh no, something went wrong!