Download presentation
Presentation is loading. Please wait.
Published byTrevion Alleyne Modified over 10 years ago
1
Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway
2
Let’s get rid of dogmatic dependency injection
3
Does this look familiar?
4
Person- Controller Person- Controller- Impl Person- Service Person- ServiceImpl Person- Repository Impl Person- Repository Impl PersonDao Impl PersonDao Impl Session- Factory
6
Customer Invoice Order Product
8
No more: this.personService = new PersonServiceImpl(sessionFactory)
9
Instead:
10
(Or) @Autowired private PersonService personService;
11
Why?
12
(dear God, why?!)
13
Testing
14
Multiple implementations
15
(Really?)
16
Configuration
17
(Often one)
18
Ordnung muss sein!
19
(Ordnung muss sein!) Hobgoblin of little minds - Ralph Waldo Emerson
20
Alternative
21
Session- Factory PersonController Service Repo sitory
22
public class PersonController { private PersonService personService; @Autowired public PersonController(SessionFactory sf) { this.personService = new PersonServiceImpl(sf); }
23
public class PersonController { private PersonService personService; @Autowired public PersonController(SessionFactory sf) { this.personService = new PersonServiceImpl(sf); } public class PersonServiceImpl implements … { private PersonRepository personRepo; public PersonServiceImpl(SessionFactory sf) { this.personRepo = new PersonRepositoryImpl(sf); }
24
public class PersonController { private PersonService personService; @Autowired public PersonController(SessionFactory sf) { this.personService = new PersonServiceImpl(sf); } public PersonControllerImpl(PersonService ps) { this.personService = ps; }
25
public class PersonController { private PersonService personService; @Autowired public PersonController(SessionFactory sf) { this.personService = new PersonServiceImpl(sf); } public PersonControllerImpl(PersonService ps) { this.personService = ps; } For Spring For mocking
26
SPRING! Session- Factory PersonController Service Repo sitory InvoiceController Repository FooServiceImpl ReportsController FooController
27
Look ma! No Spring!
28
Session- Factory PersonServlet Locator (singleton O_O) Locator (singleton O_O) ”Injected” by servlet
29
public class PersonController extends HttpServlet { private PersonService personService; public PersonController() { } public PersonController(PersonService personService) { this.personService = personService; } @Override public void init() throws ServletException { SessionFactory sf = HibernateLookup.getInstance(getServletContext()); this.personService = new PersonServiceImpl(sf); }
30
public class PersonController extends HttpServlet { private PersonService personService; public PersonController() { } public PersonController(PersonService personService) { this.personService = personService; } @Override public void init() throws ServletException { SessionFactory sf = HibernateLookup.getInstance(getServletContext()); this.personService = new PersonServiceImpl(sf); } For mocking Look, ma! No Spring!
31
Session- Factory PersonController Service Repo sitory InvoiceController Repository FooServiceImpl ReportsController FooController >
32
Bonus: Generic Repository
33
public interface Repository { T retrieve(Class type,Serializable id); Serializable save(Object object); List find(Specification spec); Transaction beginTransaction(); }
36
@RunWith(RepositoryTestRunner.class) public class PersonControllerTest { private Repository repository; private PersonController personController; public PersonControllerTest(Repository repo) { this.repository = repo; this.personController = new PersonController(repo); }
37
@Test public void should_show_person() { Person person = new Person(); Long id = (Long) repository.save(person); ModelAndView show = personController.show(id); assertEquals(person, show.getModel().get("person")); }
38
public class RepoTestRunner extends Suite { public RepoTestRunner(Class testClass) { super(testClass, createRunners(testClass)); } static List createRunners(Class testClass) { List runners = new ArrayList (); runners.add(testRunner(testClass, new HashMapRepository())); if (!isRunningInInfinitest()) { runners.add(testRunner(testClass, hibernateRepository())); } return runners; }
40
What Spring taught me
41
Be aware of dependencies!
42
Avoid differences between test and prod
43
Stay the heck away from frameworks!
44
Collapse service chains
45
Takk for meg! johannes.brodwall@steria.no http://johannesbrodwall.com http://sterkblanding.no http://twitter.com/jhannes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.