สปริงเฟรมเวิร์ค4.1

2,250 views

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,250
On SlideShare
0
From Embeds
0
Number of Embeds
74
Actions
Shares
0
Downloads
63
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

สปริงเฟรมเวิร์ค4.1

  1. 1. สปริงเฟรมเวิร์คเวิร์คชอป(Spring Framework Workshop)<br />By www.spring66.com<br />มิถุนายน 2553, 12<br />ชัดเจน “เขียนจาวาไม่ใช้สปริง บาป”<br />
  2. 2. @roofimonขอ ขอบคุณTA<br />ไม่มี TA ไม่มีงานนี้ครับ<br />@somkiat<br />พระ @9tae ไปบวชอยู่<br />@poorprogrammer<br />@nuboat<br />@YashimaExteen<br />@boyone<br />@siros_s มาสอน AOP ตอนบ่าย<br />TA เป็นผู้ทรงคุณวุฒิเรื่องสปริงทุกท่าน<br />
  3. 3. Agenda<br />Impossible is nothing<br />Why Spring?<br />New Features and Enhancements in Spring 3.0<br />Core Container 3.0<br />Persistence with Spring JDBC<br />AOP (@siros_s)<br />EhCache(Apply AOP)<br />Spring Web MVC with Web Annotation<br />
  4. 4. Basic Requirements<br />JDK 1.6 ++<br />Maven 2.0.x<br />Apache Derby<br />Netbeans, Eclipse, Vi, Whatever IDE<br />Skeleton Source from<br />http://code.google.com/p/spring66-training-3/svn/branches/1.0.PRE<br />Full<br />https://spring66-training-3.googlecode.com/svn/branches/1.9PRE<br />
  5. 5. ที่มาของเนื้อหา(ลอกมาจากไหน)<br />Building Spring2 Enterprise Application<br />Development J2EE without EJB<br />Professional Spring Framework<br />Spring2 in Action<br />Pro Spring2<br />Spring Recipe<br />หนังสืออ้างอิงเยอะมาก<br />
  6. 6. The Spring Framework<br /><ul><li>The Spring Framework is an open source application framework for the Java platform
  7. 7. Spring provides comprehensive infrastructure support for developing Java applications
  8. 8. Infrastructure??????</li></li></ul><li> My Basic Requirements for Lightweight Container<br />Lifecycle management<br />Lookup<br />Configuration<br />Dependency resolution<br />
  9. 9. lightweight Container (Value Added)<br />Transaction<br />Thread management<br />Object pooling<br />Clustering<br />Management<br />Remoting<br />Exposing remote services<br />Consuming remote services<br />Customization and extensibility<br />AOP<br />
  10. 10. ทำไมต้องสปริง เมื่อก่อน<br />Java เตรียมเครื่องมือไว้ให้เยอะไปหมดแต่เวลาใช้ ? <br />Design Pattern: ต้องนั่งตีความเอาเอง<br />กระบวนการรวมฟีเจอร์ของ SUN โหดร้ายเกินไป<br />ชีวิตเราถูกผูกติดกับ Application Server, API มากเกินไป<br />อยากเอาฟีเจอร์มารวมกันพร้อมทั้งใช้ Design Pattern ด้วย ?<br />Light Weight<br />
  11. 11. สมัยนี้<br />
  12. 12.
  13. 13. Main Components<br />
  14. 14. Layer of Application<br />
  15. 15. Bean?<br />Bean is a service<br />Service = Interface+Implement+Descriptor<br />Bean อาศัยอยู่ใน Bean Factory หรือ Application Context<br />Application Context คือหัวใจของ Spring<br />คิดไม่ออกต้องสร้าง Application Context ขึ้นมาก่อน<br />
  16. 16. Configuration<br />ส่วนมากจะชื่อ applicationContect.xml<br />
  17. 17. Initial Context<br />//Load Context Manually From AppConfig.class<br />ApplicationContext context = <br /> new FileSystemXmlApplicationContext(<br /> "classpath:/applicationContext.xml");<br />//Lookup Bean named <br />Clinic clinic = (Clinic)context.getBean(“clinic”);<br />Collection<Vet> vets = clinic.getVets();<br />
  18. 18. The “PetClinic”<br />
  19. 19. The “Lightweight Container” Architecture<br />
  20. 20. Basic Architecture<br />Spring JDBC<br />Spring Core<br />Spring Web MVC<br />JSP<br />jQuery<br />
  21. 21. Basic Requirement<br />Use Cases<br />View a list of veterinarians and their specialties<br />View information pertaining to a pet owner<br />Update the information pertaining to a pet owner<br />Add a new pet owner to the system<br />View information pertaining to a pet<br />Update the information pertaining to a pet<br />Add a new pet to the system<br />View information pertaining to a pet's visitation history<br />Add information pertaining to a visit to the pet's visitation history<br />Business Rules<br />An owner may not have multiple pets with the same case-insensitive name<br />
  22. 22. Checkout Code from Google Code<br />Create Test Case Call “isClinicServiceReady”<br /> @Autowired<br /> protected Clinic clinic;<br /> @Test<br />public void isCliniceReady() {<br /> assertNotNull(clinic);<br />}<br />We have done creating our first “bean”.<br />
  23. 23. Checkout Code from Google Code<br />Create <br />“interface Clinic”<br />Run Test (Fail!!!!!)<br />Create <br />applicationContect.xml<br />Copy content from “Master Project”<br />“src est esourcesorgspring66 raining3 estpetclinicaseline”<br />Run Test !!!!!!<br />We have done creating our first “bean”.<br />
  24. 24. What we have done?<br />
  25. 25. Why Test First<br />Test case is “META-CODE”, code that explains code.<br />Feel confident for refactor, move and share.<br />
  26. 26. Checkout Code from Google Code<br />Create “interface ListDataService”<br />Create “class ListDataServiceImpl”<br />@Override<br /> public List getElementsList() {<br /> List list = new ArrayList();<br /> list.add("Tom");<br /> list.add("Henri");<br /> list.add("Jim");<br /> return list;<br /> }<br />
  27. 27. Go more deep into “DataSource”<br />DB<br />Persistence<br />Manager<br />Oracle<br />MsSQL<br />DB2<br />MySQL<br />Hibernate<br />iBatis<br />OpenJPA<br />Connection<br />Data<br />Source<br />
  28. 28. Go more deep into “DataSource”<br />Basically we must have this information for datasource<br />Driver Class<br />Driver Class Name<br />Connection String (URL)<br />Username<br />Password<br />Plus some optional parameters depends on Driver Manager (Pool Size, Wait Time, bla bla bla)<br />
  29. 29. Go more deep into DataSource<br />Basically we must have this information<br /><bean id="dataSource" class=“$Driver"><br /> <!-- Connection Info --><br /> <property name="driverClassName" <br /> value="${jdbc.driverClassName}" /><br /> <property name="url" value="${jdbc.url}" /><br /> <property name="username" value="${jdbc.username}" /><br /> <property name="password" value="${jdbc.password}" /><br /> <!-- Connection Pooling DBCP --><br /> <property name="initialSize" value="5"/><br /> <property name="maxActive" value="100" /><br /> <property name="maxIdle" value="30" /><br /> <property name="maxWait" value="1000" /><br /> <property name="poolPreparedStatements" value="true" /><br /> <property name="defaultAutoCommit" value="false" /><br /> </bean><br />
  30. 30. Spring JDBC<br />The value-add provided by the Spring Framework JDBC abstraction is perhaps best shown by the sequence of actions outlined in the table.<br />
  31. 31. Spring JDBC<br />
  32. 32. Go more deep into Spring JDBC<br />JdbcTemplate<br />NamedParameterJdbcTemplate <br />SimpleJdbcTemplate <br />SimpleJdbcInsert and SimpleJdbcCall <br />RDBMS Objects including MappingSqlQuery, SqlUpdate and StoredProcedure<br />
  33. 33. Go more deep into Spring JDBC<br />DataSource<br />Prepared Statement<br />Mapper<br />Extractor <br />
  34. 34. Move up to Service Layer<br />The most important part of our Application.<br />All business logics are located, here<br />Test it carefully, change very often.<br />Controller<br />Service<br />Entity A<br />Entity B<br />Entity C<br />
  35. 35. Spring Web MVC Flow<br />Clear separation of roles<br />Powerful and straightforward configuration of both framework and application classes as JavaBeans. <br />Adaptability, non-intrusiveness, and flexibility.<br />Customizable binding and validation<br />Customizable handler mapping and view resolution<br />Flexible model transfer<br />Beans whose lifecycle is scoped to the current HTTP request or HTTP Session<br />
  36. 36. Spring DispatcherServlet<br />
  37. 37. Dispatcher Servlet<br /><web-app> <br /> <servlet> <br /> <servlet-name>example</servlet-name> <br /> <servlet-class><br /> org.springframework.web.servlet.DispatcherServlet<br /> </servlet-class><br /> <load-on-startup>1</load-on-startup><br /> </servlet> <br /> <servlet-mapping> <br /> <servlet-name>example</servlet-name> <br /> <url-pattern>*.form</url-pattern> <br /> </servlet-mapping> <br /> </web-app> <br />
  38. 38. Example-servlet.xml<br /><context:component-scan base-package="com.spring66.petclinic.web"/><br /><bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="0"/><br /><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/"<br /> p:suffix=".jsp" p:order="1"/><br /><bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"<br /> p:basename="messages"/><br />
  39. 39. Context Hierarchy<br />
  40. 40. Web Layer<br />Web <br />Context<br />Spring<br />Context<br /><ul><li>Need Spring Boot Strap </li></ul>org.springframework.web.context.ContextLoaderListener<br />web.xml<br />Web <br />Context<br />Spring<br />Context<br />
  41. 41. Bootstrapping web applications<br /><ul><li>Spring allows for seamlessly bootstrapping @Configuration classes within your servlet container's web.xml deployment descriptor</li></ul><listener><br /> <listener-class><br /> org.springframework.web.context.ContextLoaderListener<br /> </listener-class><br /></listener><br /><context-param><br /> <param-name>contextConfigLocation</param-name><br /> <param-value>/WEB-INF/applicationContext.xml</param-value><br /></context-param><br />
  42. 42. Still have xxx-servlet.xml<br /><ul><li>Need to create abc-servlet.xml for mapping request from dispatcher servlet
  43. 43. <context:component-scan </li></ul> base-package="com.spring66.petclinic.web"/><br /><ul><li>Also can config other parameters like binding, viewResolver,…</li></li></ul><li>Still have xxx-servlet.xml<br />@Controller<br />public class ClinicController {<br /> private final Clinic clinic;<br /> @Autowired<br /> public ClinicController(Clinic clinic) {<br /> this.clinic = clinic;<br /> }<br />@RequestMapping("/")<br /> public String welcomeHandler() {<br /> return "welcome";<br /> }<br />
  44. 44. First Controller<br /><ul><li>Named “ClinicController”
  45. 45. And then Copy Content from Main Project
  46. 46. srcmainjavacomspring66petclinicweb
  47. 47. ====ClinicController======
  48. 48. “mvn jetty:run –Dmaven.test.skip=true –Djetty.port=9999”
  49. 49. http://localhost:9999/</li></li></ul><li>First Controller, Test First<br /><ul><li>Create “VetControllerTests”
  50. 50. And then Copy Content from Web.text
  51. 51. ====TestVetController======
  52. 52. Wowwww!!! Full of “red” things, so let’s eliminate them.</li></li></ul><li>Create VetController<br /><ul><li>List All Vet
  53. 53. Create “VetController”
  54. 54. Copy content from “Web.txt”
  55. 55. Part “VetController”
  56. 56. There are two interesting parts:
  57. 57. Inject “clinic” service into controller
  58. 58. @Autowired
  59. 59. And Request Mapping
  60. 60. @Request over “public Collection<Vet> list()”
  61. 61. It will handles {WebContext}/main/vet/list</li></li></ul><li>OwnerController, Test First<br /><ul><li>Create “OwnerControllerTests”
  62. 62. And then Copy Content from Web.text
  63. 63. ====TestOwnerController======
  64. 64. Wowwww!!! Full of “red” things, so let’s eliminate them.</li></li></ul><li>Let’s do more action, Test First<br /><ul><li>Create “OwnerController”
  65. 65. Copy content from “Web.txt”
  66. 66. Part “SearchOwnerController”
  67. 67. If it works so move forward
  68. 68. Do some magic by adding new Owner
  69. 69. Copy content from “Web.txt”
  70. 70. Part “AddOwnerController”
  71. 71. Create “ViewUtils”
  72. 72. Copy content from “Web.txt”
  73. 73. Part “ViewUtils”
  74. 74. Create “PageType”.. Good u can guest, next</li>

×