Extract code samples from docs
See gh-6313
This commit is contained in:
@@ -25,14 +25,9 @@ Doing so enables debug logs for a selection of core loggers and logs a condition
|
||||
=== Disabling Specific Auto-configuration Classes
|
||||
If you find that specific auto-configuration classes that you do not want are being applied, you can use the exclude attribute of `@SpringBootApplication` to disable them, as shown in the following example:
|
||||
|
||||
[source,java,pending-extract=true,indent=0]
|
||||
[source,java,indent=0]
|
||||
----
|
||||
import org.springframework.boot.autoconfigure.*;
|
||||
import org.springframework.boot.autoconfigure.jdbc.*;
|
||||
|
||||
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
|
||||
public class MyApplication {
|
||||
}
|
||||
include::{docs-java}/using/autoconfiguration/disablingspecific/MyApplication.java[]
|
||||
----
|
||||
|
||||
If the class is not on the classpath, you can use the `excludeName` attribute of the annotation and specify the fully qualified name instead.
|
||||
|
||||
@@ -162,12 +162,9 @@ In most cases, you can set this property in your `application.properties` (doing
|
||||
|
||||
If you need to _completely_ disable restart support (for example, because it does not work with a specific library), you need to set the configprop:spring.devtools.restart.enabled[] `System` property to `false` before calling `SpringApplication.run(...)`, as shown in the following example:
|
||||
|
||||
[source,java,pending-extract=true,indent=0]
|
||||
[source,java,indent=0]
|
||||
----
|
||||
public static void main(String[] args) {
|
||||
System.setProperty("spring.devtools.restart.enabled", "false");
|
||||
SpringApplication.run(MyApp.class, args);
|
||||
}
|
||||
include::{docs-java}/devtools/restart/disable/MyApplication.java[]
|
||||
----
|
||||
|
||||
|
||||
|
||||
@@ -1,51 +1,23 @@
|
||||
[[using.spring-beans-and-dependency-injection]]
|
||||
== Spring Beans and Dependency Injection
|
||||
You are free to use any of the standard Spring Framework techniques to define your beans and their injected dependencies.
|
||||
We often find that using `@ComponentScan` (to find your beans) and using `@Autowired` (to do constructor injection) works well.
|
||||
We generally recommend using constructor injection to wire up dependencies and `@ComponentScan` to find beans.
|
||||
|
||||
If you structure your code as suggested above (locating your application class in a root package), you can add `@ComponentScan` without any arguments.
|
||||
If you structure your code as suggested above (locating your application class in a top package), you can add `@ComponentScan` without any arguments or use the `@SpringBootApplication` annotation which implicitly includes it.
|
||||
All of your application components (`@Component`, `@Service`, `@Repository`, `@Controller` etc.) are automatically registered as Spring Beans.
|
||||
|
||||
The following example shows a `@Service` Bean that uses constructor injection to obtain a required `RiskAssessor` bean:
|
||||
|
||||
[source,java,pending-extract=true,indent=0]
|
||||
[source,java,indent=0]
|
||||
----
|
||||
package com.example.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class DatabaseAccountService implements AccountService {
|
||||
|
||||
private final RiskAssessor riskAssessor;
|
||||
|
||||
@Autowired
|
||||
public DatabaseAccountService(RiskAssessor riskAssessor) {
|
||||
this.riskAssessor = riskAssessor;
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
}
|
||||
include::{docs-java}/using/springbeansanddependencyinjection/singleconstructor/DatabaseAccountService.java[]
|
||||
----
|
||||
|
||||
If a bean has one constructor, you can omit the `@Autowired`, as shown in the following example:
|
||||
If a bean has more than one constructor, you'll need to mark the one you want Spring to use with `@Autowired`:
|
||||
|
||||
[source,java,pending-extract=true,indent=0]
|
||||
[source,java,indent=0]
|
||||
----
|
||||
@Service
|
||||
public class DatabaseAccountService implements AccountService {
|
||||
|
||||
private final RiskAssessor riskAssessor;
|
||||
|
||||
public DatabaseAccountService(RiskAssessor riskAssessor) {
|
||||
this.riskAssessor = riskAssessor;
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
}
|
||||
include::{docs-java}/using/springbeansanddependencyinjection/multipleconstructors/DatabaseAccountService.java[]
|
||||
----
|
||||
|
||||
TIP: Notice how using constructor injection lets the `riskAssessor` field be marked as `final`, indicating that it cannot be subsequently changed.
|
||||
|
||||
@@ -31,7 +31,7 @@ The following listing shows a typical layout:
|
||||
com
|
||||
+- example
|
||||
+- myapplication
|
||||
+- Application.java
|
||||
+- MyApplication.java
|
||||
|
|
||||
+- customer
|
||||
| +- Customer.java
|
||||
@@ -46,21 +46,9 @@ The following listing shows a typical layout:
|
||||
+- OrderRepository.java
|
||||
----
|
||||
|
||||
The `Application.java` file would declare the `main` method, along with the basic `@SpringBootApplication`, as follows:
|
||||
The `MyApplication.java` file would declare the `main` method, along with the basic `@SpringBootApplication`, as follows:
|
||||
|
||||
[source,java,pending-extract=true,indent=0]
|
||||
[source,java,indent=0]
|
||||
----
|
||||
package com.example.myapplication;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
include::{docs-java}/using/structuringyourcode/locatingthemainclass/MyApplication.java[]
|
||||
----
|
||||
|
||||
@@ -7,21 +7,9 @@ A single `@SpringBootApplication` annotation can be used to enable those three f
|
||||
* `@ComponentScan`: enable `@Component` scan on the package where the application is located (see <<using#using.structuring-your-code,the best practices>>)
|
||||
* `@Configuration`: allow to register extra beans in the context or import additional configuration classes
|
||||
|
||||
[source,java,pending-extract=true,indent=0]
|
||||
[source,java,indent=0]
|
||||
----
|
||||
package com.example.myapplication;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
include::{docs-java}/using/usingthespringbootapplicationannotation/springapplication/MyApplication.java[]
|
||||
----
|
||||
|
||||
NOTE: `@SpringBootApplication` also provides aliases to customize the attributes of `@EnableAutoConfiguration` and `@ComponentScan`.
|
||||
@@ -31,25 +19,9 @@ NOTE: `@SpringBootApplication` also provides aliases to customize the attributes
|
||||
None of these features are mandatory and you may choose to replace this single annotation by any of the features that it enables.
|
||||
For instance, you may not want to use component scan or configuration properties scan in your application:
|
||||
|
||||
[source,java,pending-extract=true,indent=0]
|
||||
[source,java,indent=0]
|
||||
----
|
||||
package com.example.myapplication;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.context.annotation.ComponentScan
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableAutoConfiguration
|
||||
@Import({ MyConfig.class, MyAnotherConfig.class })
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
include::{docs-java}/using/usingthespringbootapplicationannotation/individualannotations/MyApplication.java[]
|
||||
----
|
||||
|
||||
In this example, `Application` is just like any other Spring Boot application except that `@Component`-annotated classes and `@ConfigurationProperties`-annotated classes are not detected automatically and the user-defined beans are imported explicitly (see `@Import`).
|
||||
|
||||
Reference in New Issue
Block a user