diff --git a/README.adoc b/README.adoc index 3bdcb64a3a..7d2175fef8 100644 --- a/README.adoc +++ b/README.adoc @@ -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. diff --git a/complete/build.gradle b/complete/build.gradle index a41d48709f..0296b42324 100644 --- a/complete/build.gradle +++ b/complete/build.gradle @@ -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 { diff --git a/complete/pom.xml b/complete/pom.xml index 63d9dd5867..24c3d2829d 100644 --- a/complete/pom.xml +++ b/complete/pom.xml @@ -34,18 +34,4 @@ - - - spring-releases - https://repo.spring.io/libs-release - - - - - - spring-releases - https://repo.spring.io/libs-release - - - diff --git a/complete/src/main/java/hello/Application.java b/complete/src/main/java/hello/Application.java index 95421d728c..00722b7cfa 100644 --- a/complete/src/main/java/hello/Application.java +++ b/complete/src/main/java/hello/Application.java @@ -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 results = ctx.getBean(JdbcTemplate.class).query("SELECT first_name, last_name FROM people", new RowMapper() { + @Autowired + JdbcTemplate jdbcTemplate; + + @Override + public void run(String... strings) throws Exception { + + List results = jdbcTemplate.query("SELECT first_name, last_name FROM people", new RowMapper() { @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."); } - } + } } diff --git a/initial/build.gradle b/initial/build.gradle index a41d48709f..0296b42324 100644 --- a/initial/build.gradle +++ b/initial/build.gradle @@ -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 { diff --git a/initial/pom.xml b/initial/pom.xml index 63d9dd5867..24c3d2829d 100644 --- a/initial/pom.xml +++ b/initial/pom.xml @@ -34,18 +34,4 @@ - - - spring-releases - https://repo.spring.io/libs-release - - - - - - spring-releases - https://repo.spring.io/libs-release - - -