Sunday, September 12, 2021

DI(dependency injection) using example

 Suppose we have a controller ,which accepts the request  and will response , but it depends on business layer.

@Controller

public class TodoController{

@Autowired

TodoBusinessService todoBusinessService;

}

TodoBusinessService depends to another dependency to fetch data from the database,

@Component

public class TodoBusinessService{

@Autowired

TodoDataService todoDataService;

}

now TodoDataService need to fire a query to fetch data from the database .So it depends on JdbcTemplate.

@Component

public class TodoDataService {

@Autowired

JdbcTemplate jdbcTemplate;

}

No comments:

Post a Comment

Abstract factory pattern

When single task can be done by multiple groups/family of objects and decision is taken at the runtime.