Slide 1

Slide 1 text

Which is which? @charlesmuchene IoC DI !

Slide 2

Slide 2 text

Agenda • Dependency Injection • Inversion of Control

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Dependency Injection “A 25-dollar term for a 5-cent concept” a pattern (or process) of providing the dependencies to a component

Slide 5

Slide 5 text

Task

Slide 6

Slide 6 text

Task public class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; }

Slide 7

Slide 7 text

Task User Service User Data Access Object

Slide 8

Slide 8 text

User Service public interface UserService { void save(User user); User findOne(long id); }

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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);

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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); } ... }

Slide 13

Slide 13 text

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);

Slide 14

Slide 14 text

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); } ... }

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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);

Slide 17

Slide 17 text

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);

Slide 18

Slide 18 text

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);

Slide 19

Slide 19 text

IoC Container • Creates • Wires together • Manages (creation -> destruction)

Slide 20

Slide 20 text

Inversion of Control Programming principle principle of delegating the creation, configuration and lifecycle management of objects to another component or framework

Slide 21

Slide 21 text

In Spring, Inversion of Control is achieved through Dependency Injection.

Slide 22

Slide 22 text

IoC DI !

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

charlesmuchene