dependency injection when you only have one dependency javazone johannes brodwall, recovering spring...
TRANSCRIPT
![Page 1: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/1.jpg)
Dependency injection when you only have one dependency
JavaZone
Johannes Brodwall, Recovering Spring User
Steria Norway
![Page 2: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/2.jpg)
Let’s get rid ofdogmatic dependency
injection
![Page 3: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/3.jpg)
Does this look familiar?
![Page 4: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/4.jpg)
Person-Controller
Person-Controller-
Impl
Person-Service
Person-ServiceImpl
Person-Repository
Person-Repository
Impl
PersonDao
PersonDaoImpl
Session-Factory
![Page 5: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/5.jpg)
![Page 6: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/6.jpg)
Customer
Invoice
Order
Product
![Page 7: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/7.jpg)
![Page 8: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/8.jpg)
No more:
this.personService =new PersonServiceImpl(sessionFactory)
![Page 9: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/9.jpg)
Instead:
<bean id="personDao" class=“…springmadness.dao.impl.PersonDaoImpl"> <property name="sessionFactory" …/></bean>
![Page 10: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/10.jpg)
(Or)
@Autowired private PersonService personService;
![Page 11: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/11.jpg)
Why?
![Page 12: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/12.jpg)
Why?
(dear God, why?!)
![Page 13: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/13.jpg)
Testing
![Page 14: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/14.jpg)
Multiple implementations
![Page 15: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/15.jpg)
Multiple implementations
(Really?)
![Page 16: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/16.jpg)
Configuration
![Page 17: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/17.jpg)
Configuration
(Often one)
![Page 18: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/18.jpg)
Ordnung muss sein!
![Page 19: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/19.jpg)
(Ordnung muss sein!)
Hobgoblin of little minds
- Ralph Waldo Emerson
![Page 20: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/20.jpg)
Alternative
![Page 21: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/21.jpg)
Session-Factory
PersonController
ServiceRepository
![Page 22: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/22.jpg)
public class PersonController { private PersonService personService;
@Autowired public PersonController(SessionFactory sf) { this.personService = new PersonServiceImpl(sf); }
![Page 23: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/23.jpg)
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); }
![Page 24: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/24.jpg)
public class PersonController { private PersonService personService;
@Autowired public PersonController(SessionFactory sf) { this.personService = new PersonServiceImpl(sf); } public PersonControllerImpl(PersonService ps) { this.personService = ps; }
![Page 25: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/25.jpg)
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
![Page 26: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/26.jpg)
SPRING!
Session-Factory
PersonController
ServiceRepository
InvoiceController
Repository
FooServiceImpl
ReportsController
FooController
![Page 27: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/27.jpg)
Look ma!No Spring!
![Page 28: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/28.jpg)
Session-Factory
PersonServlet
Locator(singleton O_O)
”Injected” by servlet
![Page 29: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/29.jpg)
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); }
![Page 30: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/30.jpg)
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!
![Page 31: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/31.jpg)
Session-Factory
PersonController
ServiceRepository
InvoiceController
Repository
FooServiceImpl
ReportsController
FooController
<<locator injection>>
<<locator injection>>
<<locator injection>>
<<locator in
jection>>
![Page 32: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/32.jpg)
Bonus: Generic Repository
![Page 33: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/33.jpg)
public interface Repository {
<T> T retrieve(Class<T> type,Serializable id);
Serializable save(Object object);
<T> List<T> find(Specification<T> spec);
Transaction beginTransaction();}
![Page 34: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/34.jpg)
![Page 35: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/35.jpg)
![Page 36: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/36.jpg)
@RunWith(RepositoryTestRunner.class)public class PersonControllerTest { private Repository repository; private PersonController personController; public PersonControllerTest(Repository repo) { this.repository = repo; this.personController = new PersonController(repo); }
![Page 37: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/37.jpg)
@Testpublic 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"));}
![Page 38: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/38.jpg)
public class RepoTestRunner extends Suite { public RepoTestRunner(Class<?> testClass) { super(testClass, createRunners(testClass)); } static List<Runner> createRunners(Class<?> testClass) { List<Runner> runners = new ArrayList<Runner>();
runners.add(testRunner(testClass, new HashMapRepository())); if (!isRunningInInfinitest()) {
runners.add(testRunner(testClass, hibernateRepository())); } return runners; }
![Page 39: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/39.jpg)
![Page 40: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/40.jpg)
What Spring taught me
![Page 41: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/41.jpg)
Be aware of dependencies!
![Page 42: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/42.jpg)
Avoid differences between test and prod
![Page 43: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/43.jpg)
Stay the heck away from frameworks!
![Page 44: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/44.jpg)
Collapse service chains
![Page 45: Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway](https://reader036.vdocuments.us/reader036/viewer/2022062417/551be7ea550346b4588b6172/html5/thumbnails/45.jpg)
Takk for [email protected]
http://johannesbrodwall.com
http://sterkblanding.no
http://twitter.com/jhannes