[JWAP-2] DI & Spring

Download [JWAP-2] DI & Spring

Post on 27-Aug-2014

431 views

Category:

Software

8 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

<ul><li> Java </li> <li> Web </li> <li> Programming </li> <li> Advanced </li> <li> Week02 </li> <li> Spring Framework </li> <li> snatcher@nhn.com </li> <li> https://github.com/eternity-next/score-spring.git </li> <li> score-client-01 </li> <li> class Lecture Class StandardGrader Class PassFailGrader interface Grader Dependency Use Client Dependency Use Create Dependency inject Dependency </li> <li> class Lecture Class StandardGrader Class PassFailGrader interface GraderUse Client Use Create inject class ScoreRepositoryUse </li> <li> class Lecture interface Grader class ScoreRepository Score Server 2) HTTP 1) score 3) JSON 4) score </li> <li> public class ScoreRepository { private String url; private HttpClient client; public ScoreRepository() { this.url = http://localhost:3000/scores this.client = new HttpClient( new MultiThreadedHttpConnectionManager()); } public int [] getScores() { GetMethod get = new GetMethod(url); try { if (client.executeMethod(get) == HttpStatus.SC_OK) { return new Gson().fromJson(get.getResponseBodyAsString(), int [].class); } else { return new int [0]; } } catch (Exception e) { throw new RuntimeException(e); } finally { get.releaseConnection(); } } } ScoreRepository </li> <li> public class Lecture { private ScoreRepository scoreRepository; private Grader grader; public Lecture(Grader grader) { this.grader = grader; this.scoreRepository = new ScoreRepository(); } public String grade() { return grader.grade(scoreRepository.getScores()); } } Lecture </li> <li> Problem Testability </li> <li> Transitive Dependency </li> <li> Hidden Dependency </li> <li> Transitive Dependency </li> <li> B CA Object A </li> <li> Object C </li> <li> Object B </li> <li> Object A </li> <li> Object B </li> <li> depends on </li> <li> Object C </li> <li> Object B </li> <li> depends on </li> <li> Object A </li> <li> Object C </li> <li> depends on </li> <li> class Lecture interface Grader class ScoreRepository Score Server LectureTest Transitive Dependency </li> <li> LectureTest </li> <li> Score Server </li> <li> depends on </li> <li> class Lecture interface Grader class ScoreRepository Score Server LectureTest Server Down </li> <li> LectureTest </li> <li> Score Server </li> <li> depends on </li> <li> public class Lecture { private ScoreRepository scoreRepository; private Grader grader; public Lecture(Grader grader) { this.grader = grader; this.scoreRepository = new ScoreRepository(); } public String grade() { return grader.grade(scoreRepository.getScores()); } } Lecture </li> <li> Tight Coupling </li> <li> class Lecture interface Grader class ScoreRepository Score Server LectureTest Test Fail </li> <li> LectureTest </li> <li> Score Server </li> <li> depends on </li> <li> public class Lecture { private ScoreRepository scoreRepository; private Grader grader; public Lecture(Grader grader) { this.grader = grader; this.scoreRepository = new ScoreRepository(); } public String grade() { return grader.grade(scoreRepository.getScores()); } } Hidden Dependency </li> <li> Hidden ScoreRepository </li> <li> @Test public void starndardGrade() { Lecture lecture = new Lecture(new StandardGrader()); String grade = lecture.grade(); assertEquals("A:1 B:1 C:1 D:0 F:2 ", grade); } assertEquals </li> <li> A:1 B:1 C:1 D:0 F:2 ? </li> <li> Solution Dependency Injection </li> <li> class Lecture interface Grader class ScoreRepository Score Server LectureTest Transitive Dependency </li> <li> LectureTest </li> <li> Score Server </li> <li> depends on </li> <li> class Lecture interface Grader class HttpScore Repository Score Server LectureTest Dependency Breaking </li> <li> LectureTest </li> <li> Score Server </li> <li> depends on </li> <li> interface ScoreRepository </li> <li> class Lecture interface Grader class HttpScore Repository Score Server LectureTest Dependency Breaking </li> <li> LectureTest </li> <li> Score Server </li> <li> depends on </li> <li> interface ScoreRepository </li> <li> class Lecture interface Grader class HttpScore Repository Score Server LectureTest Testable </li> <li> LectureTest </li> <li> Score Server </li> <li> depends on </li> <li> interface ScoreRepository </li> <li> public class Lecture { private ScoreRepository scoreRepository; private Grader grader; public Lecture(Grader grader) { this.grader = grader; this.scoreRepository = new ScoreRepository(); } public String grade() { return grader.grade(scoreRepository.getScores()); } } Tight Coupling &amp; </li> <li> Hidden Dependency </li> <li> public class Lecture { private ScoreRepository scoreRepository; private Grader grader; public Lecture(Grader grader, ScoreRepository scoreRepository) { this.grader = grader; this.scoreRepository = scoreRepository; } Loose Coupling &amp; </li> <li> Explicit Dependency </li> <li> Mock </li> <li> {50,20,70,80,90} =&gt;A:1 B:1 C:1 D:0 F:2 </li> <li> @RunWith(MockitoJUnitRunner.class) public class LectureTest { @Mock private ScoreRepository repository; @Test public void starndardGrade() { when(repository.getScores()) .thenReturn(new int [] {50,20,70,80,90}); Lecture lecture = new Lecture(new StandardGrader(), repository); String grade = lecture.grade(); assertEquals("A:1 B:1 C:1 D:0 F:2 ", grade); } </li> <li> score-client-02 </li> <li> lecture = new Lecture(new StandardGrader(), new HttpScoreRepository()); lecture = new Lecture(new PassFailGrader(), new HttpScoreRepository()); Loose Coupling =&gt; Complex Object Graph </li> <li> Registry </li> <li> LectureRegistry object :Lecture object :Standard Grader object :PassFail Grader object :HttpScore Repository object :Lecture cache </li> <li> get Standard Grade Lecture get Pass Fail Grade Lecture Container </li> <li> public class Score { private static LectureRegistry lectureFactory = new LectureRegistry(); public static void main(String [] args) { Lecture lecture = null; Scanner scanner = new Scanner(System.in); try { System.out.print("grader? "); switch(scanner.next()) { case "st": lecture = lectureFactory.getStandardGradeLecture(); break; case "pf": lecture = lectureFactory.getPassFailGradeLecture(); break; default: return; } System.out.println(lecture.grade()); } finally { scanner.close(); } } } Score class </li> <li> Complex Object Graph, Simple API </li> <li> lecture = lectureFactory.getStandardGradeLecture(); lecture = lectureFactory.getPassFailGradeLecture(); Dependency Lookup </li> <li> score-client-03 </li> <li> Registry? </li> <li> new? </li> <li> Dependency Injection? </li> <li> ? </li> <li> Spring Registry Spring </li> <li> Registry = Application Context Dependency Injection Framework IoC Container </li> <li> class Lecture class Standard Grader class PassFail Grader class HttpScore Repository class Lecture class Lecture class Standard Grader class HttpScore Repository class PassFail Grader Class Configuration Metadata object :Lecture object :Standard Grader object :PassFail Grader object :HttpScore Repository object :Lecture Registry </li> <li> class Lecture class Standard Grader class PassFail Grader class HttpScore Repository class Lecture class Lecture class Standard Grader class HttpScore Repository class PassFail Grader Class Configuration Metadata object :Lecture object :Standard Grader object :PassFail Grader object :HttpScore Repository object :Lecture Application Context Registry </li> <li> Refactoring </li> <li> class Lecture class Standard Grader class PassFail Grader class HttpScore Repository class Lecture Configuration Metadata object :Lecture object :Standard Grader object :PassFail Grader object :HttpScore Repository object :Lecture Application Context XML Spring GenericXml ApplicationContext </li> <li> class Lecture class Standard Grader class PassFail Grader class HttpScore Repository class Lecture Configuration Metadata XML </li> <li> Scores.xml </li> <li> Application Context Application Context </li> <li> Scores.xml </li> <li> Application Context Application Context object :HttpScore Repository httpScore Repository </li> <li> Scores.xml </li> <li> Application Context Application Context object :Standard Grader object :PassFail Grader object :HttpScore Repository httpScore Repository passFail Grader standard Grader </li> <li> Scores.xml </li> <li> Application Context Application Context object :Lecture object :Standard Grader object :PassFail Grader object :HttpScore Repository httpScore Repository passFail Grader standard Grader standard Grade Lecture Dependency Injection </li> <li> Sores.xml </li> <li> Application Context Application Context object :Lecture object :Standard Grader object :PassFail Grader object :HttpScore Repository object :Lecture httpScore Repository passFail Grader standard Grader standard Grade Lecture passFail Grade Lecture Dependency Injection </li> <li> Load Application Context GenericXmlApplicationContext context = new GenericXmlApplicationContext("classpath:scores.xml); Application Context object :Lecture object :Standard Grader o...</li></ul>