49
สสสสส สสสสสสสสสส สสสสสสสสส (Spring Framework Workshop ) By www.spring66.com พพพพพพพพพ 2552, 22 พพพพพพ สสสสสสสสสสสสสสสสสสสส สสส Supported by KT ZMICO Securitie

(Spring Framework Workshop) By 2552, 22 Supported by KT ZMICO Securities

Embed Size (px)

Citation preview

Page 1: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

สปริ�ง เฟริมเวิ�ริ�ค เวิ�ริ�คชอป(Spring Framework Workshop)

By www.spring66.comพฤศจิ�กายน 2552, 22

ชั�ดเจิน “เขี�ยนจาวิาไม�ใช�สปริ�ง บาป”

Supported by KT ZMICO Securities

Page 2: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

@roofimon ขีอ ขีอบค�ณ TA• ไม่�ม่� TA ไม่�ม่�งานน��ครั�บ

– @somkiat– @9tae– @poorprogrammer– @nuboat– @siros_s– @YashimaExteen– @boyone

• TA เป็�นผู้��ทรังค�ณวุ�ฒิ�เรั"#องสป็รั�งท�กท�าน

Page 3: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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)– Transaction with Annotation (@siros_s)– EhCache (Apply AOP)– Spring Web MVC with Web Annotation

Page 4: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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/trunk/

Page 5: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

ที่��มาขีองเน��อหา(ลอกมาจากไหน)• Building Spring2 Enterprise Application• Development J2EE without EJB• Professional Spring Framework• Spring2 in Action• Pro Spring2• Spring Recipe• หน�งส"ออ�างอ�งเยอะม่าก

Page 6: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

ที่#าไมต้�องสปริ�ง เม��อก�อน• Java เตรั�ยม่เครั"#องม่"อไวุ�ให�เยอะไป็หม่ดแต�เวุลาใชั� ? • Design Pattern: ต�องน�#งต�ควุาม่เอาเอง• กรัะบวุนการัรัวุม่ฟี-เจิอรั.ของ SUN โหดรั�ายเก�นไป็• ชั�วุ�ตเรัาถู�กผู้�กต�ดก�บ Application Server, API ม่ากเก�นไป็• อยากเอาฟี-เจิอรั.ม่ารัวุม่ก�นพรั�อม่ท��งใชั� Design Pattern ด�วุย ?• Light Weight

Page 7: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

สม%ยน��

Page 8: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities
Page 9: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

My Basic Requirements for Lightweight Container

• Lifecycle management• Lookup• Configuration• Dependency resolution

Page 10: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

lightweight Container (Value Added)

• Transaction• Thread management• Object pooling• Clustering• Management• Remoting• Exposing remote services• Consuming remote services• Customization and extensibility• AOP

Page 11: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

What actually Spring is?

• Framework that …– make Enterprise Java easier to use– promote good programming practice– enabling a POJO-based programming

model that is applicable in a wide range of environments

• Some said Spring is just a “glue” for connecting all state of the art technologies together via it’s Application Context.

• Heart and Soul of Spring is Dependency Injection and Aspect Oriented Programming.

Page 12: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Main Components

Page 13: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Layer of Application

Page 14: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Dependency Injection หริ�อ

Inversion Of Control

โดยท�#วุไป็ เม่"#อใชั� IOC

• Hollywood Principle: "Don't call me, I'll call you."

Page 15: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Why IoC is Matter? • Removes explicit dependence on container

APIs • Because components don't need to look up

collaborators at runtime, they're much simpler to write and maintain

• Application code is much easier to test • A good IoC implementation preserves strong

typing • Dependencies are explicit• No more Application Server dependent

Page 16: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Bean?

• Bean is a service• Service =

Interface+Implement+Descriptor• Bean อาศ�ยอย��ใน Bean Factory หรั"อ

Application Context• Application Context ค"อห�วุใจิของ Spring• ค�ดไม่�ออกต�องสรั�าง Application Context ข2�นม่า

ก�อน

Page 17: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Configurationส�วินมากจะช��อ applicationContect.xml

Page 18: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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();

Page 19: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

The “PetClinic”

Page 20: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

The “Lightweight Container ” Architecture

Page 21: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Basic Architecture• Spring JDBC• Spring Core• Spring Web MVC• JSP• jQuery• Separate Web Module from Service Module

by using “HTTP Remoting”

Page 22: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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

Page 23: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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”.

Page 24: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Checkout Code from Google Code

• Create – “interface Clinic”

• Run Test (Fail!!!!!)• Create

– applicationContect.xml– Copy content from “Master Project”– “src\test\resources\org\spring66\

training3\test\petclinic\baseline”

• Run Test !!!!!!• We have done creating our first “bean”.

Page 25: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

What we have done?

Page 26: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Why Test First

• Test case is “META-CODE”, code that explains code.

• Feel confident for refactor, move and share.

Page 27: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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;– }

Page 28: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Go more deep into “DataSource”

DB

ConnectionDataSource

HibernateiBatisOpenJPA

PersistenceManager

OracleMsSQLDB2MySQL

Page 29: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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)

Page 30: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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>

Page 31: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Spring JDBC

- The value add provided by the Spring Framewo rk JDBC abstraction is perhaps best shown by t he sequence of actions outlined in the table.

Page 32: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Spring JDBC

Action Spring You

Define connection parameters.   X

Open the connection. X  

Specify the SQL statement.   X

Declare parameters and provide parameter values   X

Prepare and execute the statement. X  

Set up the loop to iterate through the results (if any). X  

Do the work for each iteration.   X

Process any exception. X  

Handle transactions. X  

Close the connection, statement and resultset. X  

Page 33: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Go more deep into Spring JDBC

• JdbcTemplate• NamedParameterJdbcTemplate • SimpleJdbcTemplate • SimpleJdbcInsert and SimpleJdbcCall • RDBMS Objects including MappingSqlQuery,

SqlUpdate and StoredProcedure

Page 34: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Go more deep into Spring JDBC

• DataSource• Prepared Statement• Mapper• Extractor

Page 35: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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

Page 36: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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

Page 37: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Spring DispatcherServlet

Page 38: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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>

Page 39: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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"/>

Page 40: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Context Hierarchy

Page 41: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

Web Layer

• Need Spring Boot Strap

Web Context

SpringContext

Web Context

SpringContext

org.springframework.web.context.ContextLoaderListenerweb.xml

Page 42: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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>

Page 43: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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,…

Page 44: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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";

}

Page 45: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

First Controller

• Named “ClinicController”• And then Copy Content from Main Project • src\main\java\com\spring66\petclinic\web

• ====ClinicController======

• “mvn jetty:run –Dmaven.test.skip=true –Djetty.port=9999”

• http://localhost:9999/

Page 46: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

First Controller, Test First

• Create “VetControllerTests”• And then Copy Content from Web.text• ====TestVetController======

• Wowwww!!! Full of “red” things, so let’s eliminate them.

Page 47: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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

Page 48: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

OwnerController, Test First

• Create “OwnerControllerTests”• And then Copy Content from Web.text• ====TestOwnerController======

• Wowwww!!! Full of “red” things, so let’s eliminate them.

Page 49: (Spring Framework Workshop) By  2552, 22 Supported by KT ZMICO Securities

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