สปริงเฟรมเวิร์คเวิร์คชอป(Spring Framework Workshop)By www.spring66.comมิถุนายน 2553, 12ชัดเจน “เขียนจาวาไม่ใช้สปริง บาป”
@roofimonขอ ขอบคุณTAไม่มี TA ไม่มีงานนี้ครับ@somkiatพระ @9tae ไปบวชอยู่@poorprogrammer@nuboat@YashimaExteen@boyone@siros_s มาสอน AOP ตอนบ่ายTA เป็นผู้ทรงคุณวุฒิเรื่องสปริงทุกท่าน
AgendaImpossible is nothingWhy Spring?New Features and Enhancements in Spring 3.0Core Container 3.0Persistence with Spring JDBCAOP (@siros_s)EhCache(Apply AOP)Spring Web MVC with Web Annotation
Basic RequirementsJDK 1.6 ++Maven 2.0.xApache DerbyNetbeans, Eclipse, Vi, Whatever IDESkeleton Source fromhttp://code.google.com/p/spring66-training-3/svn/branches/1.0.PREFullhttps://spring66-training-3.googlecode.com/svn/branches/1.9PRE
ที่มาของเนื้อหา(ลอกมาจากไหน)Building Spring2 Enterprise ApplicationDevelopment J2EE without EJBProfessional Spring FrameworkSpring2 in ActionPro Spring2Spring Recipeหนังสืออ้างอิงเยอะมาก
The Spring FrameworkThe 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 ContainerLifecycle managementLookupConfigurationDependency resolution
lightweight Container (Value Added)TransactionThread managementObject poolingClusteringManagementRemotingExposing remote servicesConsuming remote servicesCustomization and extensibilityAOP
ทำไมต้องสปริง เมื่อก่อนJava เตรียมเครื่องมือไว้ให้เยอะไปหมดแต่เวลาใช้ ? Design Pattern: ต้องนั่งตีความเอาเองกระบวนการรวมฟีเจอร์ของ SUN โหดร้ายเกินไปชีวิตเราถูกผูกติดกับ Application Server, API มากเกินไปอยากเอาฟีเจอร์มารวมกันพร้อมทั้งใช้ Design Pattern ด้วย ?Light Weight
สมัยนี้
Main Components
Layer of Application
Bean?Bean is a serviceService = Interface+Implement+DescriptorBean อาศัยอยู่ใน Bean Factory หรือ Application ContextApplication Context คือหัวใจของ Springคิดไม่ออกต้องสร้าง Application Context ขึ้นมาก่อน
Configurationส่วนมากจะชื่อ applicationContect.xml
Initial Context//Load Context Manually From AppConfig.classApplicationContext 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 ArchitectureSpring JDBCSpring CoreSpring Web MVCJSPjQuery
Basic RequirementUse CasesView a list of veterinarians and their specialtiesView information pertaining to a pet ownerUpdate the information pertaining to a pet ownerAdd a new pet owner to the systemView information pertaining to a petUpdate the information pertaining to a petAdd a new pet to the systemView information pertaining to a pet's visitation historyAdd information pertaining to a visit to the pet's visitation historyBusiness RulesAn owner may not have multiple pets with the same case-insensitive name
Checkout Code from Google CodeCreate Test Case Call “isClinicServiceReady” @Autowired protected Clinic clinic; @Testpublic void isCliniceReady() {    assertNotNull(clinic);}We have done creating our first “bean”.
Checkout Code from Google CodeCreate “interface Clinic”Run Test (Fail!!!!!)Create applicationContect.xmlCopy content from “Master Project”“src\test\resources\org\spring66\training3\test\petclinic\baseline”Run Test !!!!!!We have done creating our first “bean”.
What we have done?
Why Test FirstTest case is “META-CODE”, code that explains code.Feel confident for refactor, move and share.
Checkout Code from Google CodeCreate “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”DBPersistenceManagerOracleMsSQLDB2MySQLHibernateiBatisOpenJPAConnectionDataSource
Go more deep into “DataSource”Basically we must have this information for datasourceDriver ClassDriver Class NameConnection String (URL)UsernamePasswordPlus some optional parameters depends on Driver Manager (Pool Size, Wait Time, bla bla bla)
Go more deep into DataSourceBasically 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 JDBCThe 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 JDBCJdbcTemplateNamedParameterJdbcTemplate SimpleJdbcTemplate SimpleJdbcInsert and SimpleJdbcCall RDBMS Objects including MappingSqlQuery, SqlUpdate and StoredProcedure
Go more deep into Spring JDBCDataSourcePrepared StatementMapperExtractor
Move up to Service LayerThe most important part of our Application.All business logics are located, hereTest it carefully, change very often.ControllerServiceEntity AEntity BEntity C
Spring Web MVC FlowClear separation of rolesPowerful and straightforward configuration of both framework and application classes as JavaBeans. Adaptability, non-intrusiveness, and flexibility.Customizable binding and validationCustomizable handler mapping and view resolutionFlexible model transferBeans 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 LayerWeb ContextSpringContextNeed Spring Boot Strap org.springframework.web.context.ContextLoaderListenerweb.xmlWeb ContextSpringContext
Bootstrapping web applicationsSpring 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.xmlNeed 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@Controllerpublic class ClinicController {    private final Clinic clinic;    @Autowired    public ClinicController(Clinic clinic) {        this.clinic = clinic;    }@RequestMapping("/")    public String welcomeHandler() {        return "welcome";    }
First ControllerNamed “ClinicController”
And then Copy Content from Main Project
src\main\java\com\spring66\petclinic\web
====ClinicController======

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