0% found this document useful (0 votes)
3 views7 pages

Simple Spring Ex

The document outlines exercises for creating a Spring Boot project, focusing on the development of basic beans like Car and Engine, and various dependency injection techniques. It covers concepts such as @Component, @Autowired, @Configuration, bean scope, interface-based design, and layered architecture. Additionally, it includes the implementation of JPA entities and external configuration using application.properties.

Uploaded by

mohansaikovi83
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Simple Spring Ex

The document outlines exercises for creating a Spring Boot project, focusing on the development of basic beans like Car and Engine, and various dependency injection techniques. It covers concepts such as @Component, @Autowired, @Configuration, bean scope, interface-based design, and layered architecture. Additionally, it includes the implementation of JPA entities and external configuration using application.properties.

Uploaded by

mohansaikovi83
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exercise 1 — Create Spring Boot Project and

Basic Beans
Goal

Create a Spring Boot project with two beans:

• Car

• Engine

Use @Component and retrieve beans from the container.

Main Class

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {

ApplicationContext context =
[Link]([Link],
args);

Car car = [Link]([Link]);


Engine engine = [Link]([Link]);

[Link](car);
[Link](engine);
}

Concepts learned

• Spring Boot startup

• IoC container

• @SpringBootApplication

• @Component
Exercise 2 — Field Injection using
@Autowired
Inject Engine into Car.

Concept

• @Autowired

• Field injection

Exercise 3 — Replace Field Injection with


Constructor Injection

Concept

• Constructor injection (best practice)

Exercise 4 — Use Java Con guration with


@Con guration and @Bean
Remove @Component from Car and Engine.

Create con g class.

@Configuration
public class AppConfig {

//todo

Concepts

• @Configuration

• @Bean
fi
fi
fi
• Manual bean creation

Exercise 5 — Bean Scope (Singleton vs


Prototype)
Modify Engine.

@Bean
//todo
public Engine engine() {
return new Engine();
}

Test:

Engine e1 = [Link]([Link]);
Engine e2 = [Link]([Link]);

[Link](e1);
[Link](e2);

Concept

• Bean scope

• Singleton vs Prototype

Exercise 6 — Introduce Interface + Quali er +


Primary
Create interface.

Engine Interface

public interface Engine {

String start();

}
fi
PetrolEngine

@Component
public class PetrolEngine implements Engine {

public String start() {


return "Petrol engine started";
}

DieselEngine

@Component
public class DieselEngine implements Engine {

public String start() {


return "Diesel engine started";
}

Car

@Component
public class Car {

private final Engine engine;

public Car(Engine engine) {


[Link] = engine;
}

Concepts

• Interface-based design

• @Qualifier

• @Primary
Exercise 7 — Layered Architecture
Create packages:

controller
service
repository
model

CarRepository

CarService

Concept

• @Repository

• @Service

• @Controller

Exercise 8 — Add JPA Entity and Repository


Car Entity

@Entity
public class Car {

@Id
private Long id;

private String name;

}
Repository

public interface CarRepo extends JpaRepository<Car, Long> {

Concept

• @Entity

• @Id

• JpaRepository

Exercise 10 — Use [Link]


[Link]

[Link]=9000
[Link]=Toyota
[Link]=car-app

Inject value

@Value("${[Link]}")
private String brand;

Test

[Link]

Concept

• @Value

• External con guration


fi
Final Architecture Students Will Build

[Link]

controller
CarController

service
CarService

repository
CarRepository
CarRepo

model
Car

engine
Engine
PetrolEngine
DieselEngine

config
AppConfig

You might also like