• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
สปริงเฟรมเวิร์ค4.1
 

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

on

  • 1,856 views

 

Statistics

Views

Total Views
1,856
Views on SlideShare
1,784
Embed Views
72

Actions

Likes
0
Downloads
31
Comments
0

2 Embeds 72

http://spring66.com 69
http://www.slideshare.net 3

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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

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