Upgrade to @SpringBootApplication

This commit is contained in:
Greg Turnquist 2015-02-24 15:18:43 -06:00
parent 50d2b0dcdb
commit cee9eabde2
6 changed files with 22 additions and 46 deletions

View File

@ -5,7 +5,6 @@ projects: [spring-batch]
:spring_version: current
:spring_boot_version: 1.2.1.RELEASE
:Component: http://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/stereotype/Component.html
:EnableAutoConfiguration: http://docs.spring.io/spring-boot/docs/{spring_boot_version}/api/org/springframework/boot/autoconfigure/EnableAutoConfiguration.html
:SpringApplication: http://docs.spring.io/spring-boot/docs/{spring_boot_version}/api/org/springframework/boot/SpringApplication.html
:toc:
:icons: font
@ -137,11 +136,14 @@ Although batch processing can be embedded in web apps and WAR files, the simpler
include::complete/src/main/java/hello/Application.java[]
----
The `main()` method defers to the {SpringApplication}[`SpringApplication`] helper class, providing `Application.class` as an argument to its `run()` method. This tells Spring to read the annotation metadata from `Application` and to manage it as a component in the link:/understanding/application-context[Spring application context].
`@SpringBootApplication` is a convenience annotation that adds all of the following:
- `@Configuration` tags the class as a source of bean definitions for the application context.
- `@EnableAutoConfiguration` tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
- Normally you would add `@EnableWebMvc` for a Spring MVC app, but Spring Boot adds it automatically when it sees **spring-webmvc** on the classpath. This flags the application as a web application and activates key behaviors such as setting up a `DispatcherServlet`.
- `@ComponentScan` tells Spring to look for other components, configurations, and services in the the `hello` package, allowing it to find the `HelloController`.
The `@ComponentScan` annotation tells Spring to search recursively through the `hello` package and its children for classes marked directly or indirectly with Spring's {Component}[`@Component`] annotation. This directive ensures that Spring finds and registers `BatchConfiguration`, because it is marked with `@Configuration`, which in turn is a kind of `@Component` annotation.
The {EnableAutoConfiguration}[`@EnableAutoConfiguration`] annotation switches on reasonable default behaviors based on the content of your classpath. For example, it looks for any class that implements the `CommandLineRunner` interface and invokes its `run()` method. In this case, it runs the demo code for this guide.
The `main()` method uses Spring Boot's `SpringApplication.run()` method to launch an application. Did you notice that there wasn't a single line of XML? No **web.xml** file either. This web application is 100% pure Java and you didn't have to deal with configuring any plumbing or infrastructure.
For demonstration purposes, there is code to create a `JdbcTemplate`, query the database, and print out the names of people the batch job inserts.

View File

@ -1,6 +1,5 @@
buildscript {
repositories {
maven { url "https://repo.spring.io/libs-release" }
mavenLocal()
mavenCentral()
}
@ -22,7 +21,6 @@ jar {
repositories {
mavenLocal()
mavenCentral()
maven { url "https://repo.spring.io/libs-release" }
}
dependencies {

View File

@ -34,18 +34,4 @@
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>

View File

@ -4,21 +4,27 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
@ComponentScan
@EnableAutoConfiguration
public class Application {
@SpringBootApplication
public class Application implements CommandLineRunner {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
SpringApplication.run(Application.class, args);
}
List<Person> results = ctx.getBean(JdbcTemplate.class).query("SELECT first_name, last_name FROM people", new RowMapper<Person>() {
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public void run(String... strings) throws Exception {
List<Person> results = jdbcTemplate.query("SELECT first_name, last_name FROM people", new RowMapper<Person>() {
@Override
public Person mapRow(ResultSet rs, int row) throws SQLException {
return new Person(rs.getString(1), rs.getString(2));
@ -28,6 +34,6 @@ public class Application {
for (Person person : results) {
System.out.println("Found <" + person + "> in the database.");
}
}
}
}

View File

@ -1,6 +1,5 @@
buildscript {
repositories {
maven { url "https://repo.spring.io/libs-release" }
mavenLocal()
mavenCentral()
}
@ -22,7 +21,6 @@ jar {
repositories {
mavenLocal()
mavenCentral()
maven { url "https://repo.spring.io/libs-release" }
}
dependencies {

View File

@ -34,18 +34,4 @@
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>