Dependency Injection & Inversion of Control

Dependency Injection & Inversion of Control

The presentation demystifies the concept of dependency injection and inversion of control using a sample java application using Spring.

8e46b74e930fdddb93a58e990f0a47b2?s=128

Charles Muchene

February 27, 2019
Tweet

Transcript

  1. Which is which? @charlesmuchene IoC DI !

  2. Agenda • Dependency Injection • Inversion of Control

  3. Dependency Injection “A 25-dollar term for a 5-cent concept”

  4. Dependency Injection “A 25-dollar term for a 5-cent concept” a

    pattern (or process) of providing the dependencies to a component
  5. Task

  6. Task public class User { private String name; private int

    age; public User(String name, int age) { this.name = name; this.age = age; }
  7. Task User Service User Data Access Object

  8. User Service public interface UserService { void save(User user); User

    findOne(long id); }
  9. User DAO public interface UserDao { void save(User user); User

    findOne(long id); }
  10. Main User user = new User("Charles", 24); System.out.println("******************************"); User fetchedUser

    = userService.findOne(-1); System.out.println(fetchedUser); UserService userService = new UserServiceImpl(); userService.save(user);
  11. User Service Impl public class UserServiceImpl implements UserService { @Override

    public void save(User user) { userDao.save(user); } ... } Dependency private UserDao userDao = new UserDaoImpl();
  12. User Service Impl public class UserServiceImpl implements UserService { private

    UserDao userDao; public UserServiceImpl(UserDao userDao) { this.userDao = userDao; } @Override public void save(User user) { userDao.save(user); } ... }
  13. Main User user = new User("Charles", 24); System.out.println("******************************"); User fetchedUser

    = userService.findOne(-1); System.out.println(fetchedUser); UserDao userDao = new UserDaoImpl(); UserService userService = new UserServiceImpl(userDao); userService.save(user);
  14. User Service Impl public class UserServiceImpl implements UserService { private

    UserDao userDao; public UserServiceImpl(UserDao userDao) { this.userDao = userDao; } @Override public void save(User user) { userDao.save(user); } ... }
  15. User Service Impl @Service public class UserServiceImpl implements UserService {

    @Autowired private UserDao userDao; @Override public void save(User user) { userDao.save(user); } ... }
  16. Main User user = new User("Charles", 24); System.out.println("******************************"); User fetchedUser

    = userService.findOne(-1); System.out.println(fetchedUser); UserService userService = new UserServiceImpl(); userService.save(user);
  17. Main: Spring User user = new User("Charles", 24); System.out.println("******************************"); User

    fetchedUser = userService.findOne(-1); System.out.println(fetchedUser); String contextLocation = "context/applicationContext.xml"; ApplicationContext context = new ClassPathXmlApplicationContext(contextLocation); UserService userService = context.getBean(UserService.class); userService.save(user);
  18. IoC Container // Load up config String contextLocation = "context/applicationContext.xml";

    ApplicationContext context = new ClassPathXmlApplicationContext(contextLocation); // Ask IoC for instance (bean) UserService userService = context.getBean(UserService.class); // Use the (wired-up) instance userService.save(user);
  19. IoC Container • Creates • Wires together • Manages (creation

    -> destruction)
  20. Inversion of Control Programming principle principle of delegating the creation,

    configuration and lifecycle management of objects to another component or framework
  21. In Spring, Inversion of Control is achieved through Dependency Injection.

  22. IoC DI !

  23. Resources • https://github.com/charlesmuchene/di-spring

  24. charlesmuchene