Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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.

Charles Muchene

February 27, 2019
Tweet

More Decks by Charles Muchene

Other Decks in Technology

Transcript

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

    pattern (or process) of providing the dependencies to a component
  2. Task public class User { private String name; private int

    age; public User(String name, int age) { this.name = name; this.age = age; }
  3. 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);
  4. User Service Impl public class UserServiceImpl implements UserService { @Override

    public void save(User user) { userDao.save(user); } ... } Dependency private UserDao userDao = new UserDaoImpl();
  5. 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); } ... }
  6. 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);
  7. 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); } ... }
  8. User Service Impl @Service public class UserServiceImpl implements UserService {

    @Autowired private UserDao userDao; @Override public void save(User user) { userDao.save(user); } ... }
  9. 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);
  10. 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);
  11. 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);
  12. Inversion of Control Programming principle principle of delegating the creation,

    configuration and lifecycle management of objects to another component or framework