Key Components of Spring Boot
Key Components of Spring Boot
Key Components of Spring Boot
Certainly! Let's delve deeper into each key component of Spring Boot and
provide examples to illustrate how they work.
Example: `spring-boot-starter-web`
```xml
<!-- Add the web starter to your pom.xml -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
```
With this starter, you get all the dependencies for building web
applications, including Spring MVC, embedded Tomcat, and Jackson for
JSON processing.
```xml
<!-- Add the JPA starter to your pom.xml -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
```
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
```
The **Command Line Interface (CLI)** allows you to quickly prototype with
Spring. You can write Groovy scripts and run them directly without
needing to set up a complete project.
```groovy
// hello.groovy
@RestController
class ThisWillActuallyRun {
@RequestMapping("/")
String home() {
"Hello, World!"
}
}
```
```bash
$ spring run hello.groovy
```
```xml
<!-- Add the Actuator starter to your pom.xml -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
```
This adds several endpoints you can use to monitor your application:
```xml
<!-- Add the DevTools starter to your pom.xml -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
```
Example:
```java
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
```
7. Externalized Configuration
**Externalized Configuration** allows you to define configuration
properties in external files like `application.properties` or
`application.yml`.
Example: `application.properties`
```properties
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=password
```
```java
@Value("${server.port}")
private int serverPort;
```
```java
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyApplicationTests {
@Autowired
private MyService myService;
@Test
public void contextLoads() {
assertNotNull(myService);
}
}
```
```xml
<!-- Add the Security starter to your pom.xml -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
```
By default, this will secure all endpoints with basic authentication. You can
customize security settings by extending
`WebSecurityConfigurerAdapter`.
```java
// Entity class
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// Repository interface
public interface UserRepository extends JpaRepository<User, Long> {
}
// Service class
@Service
public class UserService {
@Autowired
private UserRepository userRepository;