Spring Framework 2.0 - An Overview - PowerPoint PPT Presentation

Loading...

PPT – Spring Framework 2.0 - An Overview PowerPoint presentation | free to view - id: 5b8c8d-YzkzN



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Spring Framework 2.0 - An Overview

Description:

Spring Framework 2.0 - An Overview (try) July 21,2007 (_at_ Java User Group) http://www.try.idv.tw/ try_at_csie.org About Me Education Ph.D. Candidate, CSIE ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 62
Provided by: 6649938
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Spring Framework 2.0 - An Overview


1
Spring Framework 2.0 - An Overview
  • ??? (try)
  • July 21,2007 (_at_ Java User Group)
  • http//www.try.idv.tw/
  • try_at_csie.org

2
About Me
  • Education
  • Ph.D. Candidate, CSIE ,National Taiwan Univ.
  • Research interests Middleware and Software
    Architecture for the smart living spaces.
  • Professional Services
  • Lecturer, SL-750 (Spring Framework 2.0), Learning
    Services, Sun Microsystems Taiwan, Inc.
  • Columnist (Java EE), RUN! PC Magazine
  • Exam Developer, OOAD, 104??????????
  • Exam Developer, UML 2.0, 104??????????
  • Reviewer, Core JavaServer Faces???

3
??
  • Spring Framework ??
  • Spring 2.0 ????
  • ??

4
Spring Framework???
  • Spring?? R.Johnson
  • ?????????
  • ?????(Musicology)?????
  • JSR-154(Servlet 2.4)????
  • ????
  • 2002???????J2EE Design and Development??10????????
    ????????
  • 2003?10???????????1.0??
  • ?TSS???Introduction to Spring Framework????????Jav
    a?????

5
Spring Framework????
6
Spring Framework ?????
  • ??????Spring??????
  • ???????????Spring Framework
  • ??????
  • ????Spring Framework ???
  • ??????
  • ????? (Inversion of Control)
  • ????? (Dependency Injection)

7
Template Method
  • ????
  • ????????
  • ?????????????

public abstract class HttpServlet
protected service() .. if(..HTTP
Get..) doGet(..) else if (..HTTP Post..)
doPost(..) .. public abstract
void doGet(..) public abstract void
doPost(..)
8
Template Method?Hook Method
HttpServlet
Service() if(isHttpGet()) doGet() else
if(isHttpPost()) doPost()
9
??????(Application Framework)
  • ??
  • ???????,????,?????????
  • ??
  • ????????????
  • ??????
  • ????????????????
  • ?Template Method?,?????????????
  • ???????????????
  • ????,??????????????,????????,???????????

10
Inversion Of Control (IoC)
11
???(Dependency)
  • ??????????????????????????,????????? (depends-on)
    ??????

public class MySessionBean implements
javax.ejb.SessionBean private
javax.ejb.SessionContext ctx public void
ejbCreate() public void ejbRemove()
public void ejbActivate() public void
ejbPassivate() public void
setSessionContext(SessionContext ctx)
this.ctx ctx (?)
12
??????????????
  • ???(Dependency)?????????????????
  • ?????,??????????
  • Liskov??????????????????
  • ?Client????????????,???Client???????????

public MovieLister () movieFinder new
MovieFinderImpl()
13
??????????
public MovieFinder create() return
new MovieFinderImpl()
MovieFinderFactory
14
Dependency Injection
IoC Container
15
??????IoC
  • ??????????????????????
  • ?????????IoC Container?
  • ????????IoC??????????IoC????
  • ?????IoC????????????
  • ?IoC Container?IoC?????????????
  • M.Fowler????????(Dependency Injection)????????????
    ???,?????????

16
??
  • Template Method
  • Application Framework
  • Inversion of Control
  • Dependency
  • Dependency Injection

17
??
  • Spring Framework??
  • Spring 2.0????
  • Bean???Bean???
  • ???????? (AOP)
  • ?????
  • Message-Driven POJO
  • ??

18
Bean ??????
???
Bean ??
Bean ???
POJO
19
Bean????????
  • Spring?Bean?????????
  • XML????
  • ??????
  • Bean????
  • DTD????

lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http//www.springframework.org/dtd/spring-beans-2
.0.dtd"gt ltbeansgt lt/beansgt
20
(Spring 2.0) XML Schema?????
  • lt? xml version"1.0" encoding"UTF-8"?gt
  • ltbeans xmlns"http//www.springframework.org/schem
    a/beans
  • xmlnsxsi"http//www.w3.org/2001/XML
    Schema-instance"
  • xsischemaLocation
  • http//www.springframework.org/schema/b
    eans
  • http//www.springframework.org/schema/b
    eans/spring-beans-2.0.xsd"gt
  • ltbean id"" class""gt
  • ltproperty name"" value""/gt
  • lt/beangt
  • ltbean id"" class""gt
  • lt/beangt
  • lt/beansgt

?????XML Schema?????
21
??Bean??
  • class ProductViewer
  • private Product product
  • public void setProduct(Product product)
  • this.product product

ltbean id"productViewer" class"ProductViewer"gt
ltproperty name"product"gt ltref
bean"myProduct"/gt lt/propertygt lt/beangt ltbean
id"myProduct" class"Product"gt ltproperty
name"productId" value"10001"/gt lt/beangt
22
Setter Injection (Spring 2.0)
  • class ProductViewer
  • private Product product
  • public void setProduct(Product product)
  • this.product product

ltbean id"productViewer" class"ProductViewer"gt
ltproperty name"product ref"myProduct
/gt lt/beangt ltbean id"myProduct"
class"Product"gt ltproperty name"productId"
value"10001"/gt lt/beangt
23
(Spring 2.0) p-namespace?????
??????xmlnsphttp//www.springframework.org/schem
a/p? ???? p-namespace?????
ltbean id"myProduct" class"Product"gt
ltproperty name"productId value"10001"/gt
ltproperty namemyName" refanotherBean"/gt lt/bean
gt
ltbean id"myProduct" class"Product"
pproductId"10001"
pmyName-refanotherBean" /gt
24
??Bean?? (Code)
  • ?????
  • ApplicationContext context
  • new
    ClassPathXmlApplicationContext(

  • beans-config.xml
  • )
  • Product b (Product) context.getBean(productB)
  • ????
  • String beanConfigs
  • new String"bean-config1.xml"
    , "bean-config2.xml"
  • ApplicationContext context
  • new
    ClassPathXmlApplicationContext(
  • beanConfigs )

25
??
  • Spring Framework??
  • Spring 2.0????
  • Bean???Bean???
  • ???????? (AOP)
  • ?????
  • Message-Driven POJO
  • ??

26
???????????????
  • ??????????????????????????????(Cross-Cutting
    Concerns)
  • ??(Transaction)
  • ???
  • ????
  • ???
  • ???????,?????????????????????,????????

?
27
???(Aspect)????
  • ????(Reuse)???,???????????????????????Aspect?????
  • ???????????????????????

?????
??????
28
Pointcuts?Join Points?Advices
Advices
Join Points
Pointcuts
29
??Advice??
  • public class LogBeforeAdvice
  • public void before(JoinPoint jointPoint)
  • (??????)

30
(Spring 2.0) AOP???????
  • lt? xml version"1.0" encoding"UTF-8" ?gt
  • ltbeans xmlns"http//www.springframework.org/schem
    a/beans"
  • xmlnsxsi"http//www.w3.org/2001/XMLSchema
    -instance"
  • xmlnsaop"http//www.springframework.org/s
    chema/aop"
  • xsischemaLocation
  • "http//www.springframework.org/schema/
    beans
  • http//www.springframework.org/schema/
    beans/
  • spring-beans-2.0.xsd
  • http//www.springframework.org/schema/
    aop
  • http//www.springframework.org/schema/
    aop/
  • spring-aop-2.0.xsd"gt
  • lt/beansgt

31
(Spring 2.0) AOP??
execution( modifiers-pattern?
ret-type-pattern declaring-type-pattern?
name-pattern(param-pattern) throws-pattern? )
  • ltbean id"logBeforeAdvice"
  • class"mypackage.LogBeforeAdvice"/gt
  • ltbean id"product" class"mypackage.Product"/gt
  • ltaopconfiggt
  • ltaopaspect id"logging" ref"logBeforeAdvice"
    gt
  • ltaopbefore
  • pointcut"execution (
    mypackage.Product.(..))"
  • method"before"/gt
  • lt/aopaspectgt
  • lt/aopconfiggt

32
(Spring 2.0) AOP Annotations
  • ltbean id"logBeforeAdvice"
  • class"mypackage.LogBeforeAdvice"/gt
  • ltbean id"product" class"mypackage.Product"/gt
  • ltaopaspectj-autoproxy/gt

33
(Spring 2.0) ??Advice??
  • _at_Aspect
  • public class LogBeforeAdvice
  • _at_Before("execution( mypackage.Product.(..))"
    )
  • public void before(JoinPoint jointPoint)
  • (??????)

34
??
  • Spring Framework??
  • Spring 2.0????
  • Bean???Bean???
  • ???????? (AOP)
  • ????????
  • Message-Driven POJO
  • ??

35
(Spring 2.0) ??tx?????????
36
??tx?????????
  • lttxadvice id"txAdvice" transaction-manager"txMa
    nager"gt
  • lttxattributesgt
  • lttxmethod name"get" read-only"true" /gt
  • lttxmethod name""
  • propagation"REQUIRED"
  • isolation"DEFAULT"/gt
  • lt/txattributesgt
  • lt/txadvicegt
  • ltaopconfiggt
  • ltaopadvisor pointcut"execution(
    .ProductDao.(..))"
  • advice-ref"txAdvice" /gt
  • lt/aopconfiggt

37
(Spring 2.0) ??_at_Transactional???????
  • lttxannotation-driven transaction-manager"txManag
    er" /gt

// ?????????????
public class ProductDaoJdbcImpl
extends JdbcDaoSupport implements
ProductDao _at_Transactional(readOnly true)
public List getProducts()
(????) _at_Transactional(readOnly
false,
propagation Propagation.REQUIRED,
isolation Isolation.DEFAULT)
public void save(Product product)
(????)
38
Hibernate?JPA
???????????JPA?????????,????Hibernate
API????????,????????????!
Java Persistence API
Hibernate API
Hibernate Annotation (Hibernate) Hibernate XML ???
Hibernate Annotation (JPA) Hibernate
EntityManager
Hibernate Core
39
Spring?Hibernate???
hibernate.cfg.xml
persist() save() update() saveOrUpdate()
delete()
Transaction
40
(Spring 2.0) ??JPA Annotations
  • _at_Entity
  • _at_Table(name "TB_PRODUCT")
  • public class Product
  • _at_Id
  • _at_GeneratedValue(strategyGenerationType.TABLE)
  • _at_Column(name "id")
  • private int productId
  • _at_Column(name "name")
  • private String name
  • _at_Column(name "price")
  • private int price
  • (?????????getter methods?setter methods)

TB_PRODUCT
?? ?? ??
id Yes Integer
name No varchar
price No Integer
41
Spring-Hibernate-DAO????
?????
HibernateDaoSupport
ProductDao
ProductDaoHibernateImpl
??(implements)
??(extends)
42
Spring-Hibernate??
  • public class ProductDaoHibernateImpl extends
    HibernateDaoSupport

  • implements ProductDao
  • public void save(Product product)
  • getHibernateTemplate().save(product)
  • public List getProducts() throws
    DataAccessException
  • return (List) getHibernateTemplate().ex
    ecute( new HibernateCallback()
  • public Object
    doInHibernate(Session session) throws
    HibernateException
  • Criteria criteria
    session.createCriteria(Product.class)
  • criteria.addOrder(Order.asc(
    "price"))
  • return criteria.list()
  • )
  • // end getProducts

43
SessionFactoryBean (Annotation)
  • ltbean id"sessionFactory" class"org.springframewo
    rk.orm.hibernate3.

  • annotation.AnnotationSessionFactoryBean"gt
  • ltproperty name"dataSource"
    ref"dataSource" /gt
  • ltproperty name"annotatedClasses"gt
  • ltlistgt
  • ltvaluegtProductlt/valuegt
  • lt/listgt
  • lt/propertygt
  • ltproperty name"hibernateProperties"gt
  • ltvaluegt
  • hibernate.dialectorg.hibernate.dial
    ect.MySQLDialect
  • hibernate.show_sqltrue
  • lt/valuegt
  • lt/propertygt
  • lt/beangt

44
Spring-Hibernate??
  • ?SessionFactory??ProductDaoHibernateImpl
  • ltbean id"productDao class"ProductDaoHibernateI
    mpl"gt
  • ltproperty name"sessionFactory"
  • ref"sessionFactory" /gt
  • lt/beangt

ProductDaoHibernateImpl
SessionFactoryBean
DataSource
??
??
45
??
  • Spring Framework??
  • Spring 2.0????
  • Bean???Bean???
  • ???????? (AOP)
  • ?????
  • Message-Driven POJO
  • ??

46
(Spring 2.0) Message-Driven POJO
  • public class MyMessageDrivenPojo implements
    MessageListener
  • public void onMessage(Message message)
  • // ??????????
  • ???
  • public static void main(String args)
  • DefaultMessageListenerContainer container

  • (DefaultMessageListenerContainer)

  • context.getBean("listenerContainer")
  • container.start()

47
Message-Driven POJO (MDP)
  • ltbean id"myMDP" class"MyMessageDrivenPojo" /gt
  • ltbean id"listenerContainer
  • class"org.springframework.jms.listener
    .
  • DefaultMessageListenerContai
    ner"gt
  • ltproperty name"concurrentConsumers" value"2"
    /gt
  • ltproperty name"connectionFactory"
    ref"jmsFactory" /gt
  • ltproperty name"destination" ref"destination"
    /gt
  • ltproperty name"messageListener" ref"myMDP" /gt
  • lt/beangt

48
??????
  • ltbean id"txManager" class"org.springframework.jm
    s.connection.

  • JmsTransactionManager"gt
  • ltproperty name"connectionFactory"
    ref"connectionFactory" /gt
  • lt/beangt
  • ltbean id"listenerContainer class"org.springfra
    mework.jms.listener.

  • DefaultMessageListenerContainer"gt
  • ltproperty name"concurrentConsumers"
    value"2" /gt
  • ltproperty name"connectionFactory"
    ref"jmsFactory" /gt
  • ltproperty name"destination"
    ref"destination" /gt
  • ltproperty name"messageListener" ref"myMDP"
    /gt
  • ltproperty name"transactionManager"
    reftxManager" /gt
  • lt/beangt

49
??
  • Spring 2.0 ?????????????,???????
  • ?????
  • ??????(Dependency Injection)?????(No
    intrusive)???
  • ??AOP(Aspect-oriented programming)??
  • ??????(Persistence)???(Transaction)???
  • ??MVC Web?????
  • ??????????API?????????

50
Backup
51
?? Spring Framework?????
  • ??????????(Programming by interface)
  • ???Java???? (POJO-based Programming)
  • ????
  • ????????

52
???????
  • ????
  • ?setter???????Setter Injection
  • ????(Constructor)???Constructor Injection
  • ????(Interface)???Interface injection
  • Spring Framework??Setter Injection?Constructor
    Injection
  • ??????????????????,??Spring?????

53
??????(Glue Code)
  • ???????????????IoC Container

MovieService
MovieServlet
IoC Container
54
??Glue Code
MovieService
IoCManagerServlet
MovieServlet
IoC Container
55
AOP?????
  • Aspect
  • ????Cross-Cutting Concerns
  • ?Pointcut?Advice????????
  • Advice?Aspect???????
  • Join Points
  • Advice??????????????Join Point
  • ??????,??Advice?????????????
  • Spring????????Join Points
  • Spring???Field???Join Points,Spring????????Field?J
    oin Points?????????

56
AOP?????(2)
  • Pointcut
  • Pointcut??????Join Points
  • ????????Pointcut????,??????Advice?????(Weaving)

57
Hibernate??????
  • ??????
  • ltbean id"txManager"
  • class"org.springframework.jdbc.datasou
    rce.
  • DataSourceTransactionManage
    r"gt
  • ltproperty name"dataSource" ref"myDataSource"
    /gt
  • lt/beangt
  • Hibernate?????
  • ltbean id"txManager"
  • class"org.springframework.orm.hibernate3.
  • HibernateTransactionManager"gt
  • ltproperty name"sessionFactory"
    ref"sessionFactory" /gt
  • lt/beangt

58
NamedParameterJdbcTemplate
  • String sql "INSERT INTO t_user (name,age)
  • VALUES(userName,
    userAge)"
  • Map namedParameters new HashMap()
  • namedParameters.put("userName", name)
  • namedParameters.put("userAge", age)
  • NamedParameterJdbcTemplate jdbcTemplate
  • new NamedParameterJdbcTemplate(dat
    aSource)
  • List rows jdbcTemplate.queryForList(sql,
    namedParameters)

59
SimpleJdbcTemplate
  • JDK 5.0????,????
  • String sql "SELECT FROM user WHERE id?"
  • ParameterizedRowMapperltUsergt mapper
  • new ParameterizedRowMapperltUsergt()
  • public User mapRow(ResultSet rs, int
    rowNum)

  • throws SQLException
  • return user
  • SimpleJdbcTemplate jdbcTemplate new
    SimpleJdbcTemplate(dataSource)
  • jdbcTemplate.queryForObject(sql, mapper, id)

60
MessageListenerAdapter
  • ??????MyMessageDrivenPojo ???????
  • ????javax.jms.MessageListener??
  • MessageListenerAdapter
  • ????MDP?JMS?????
  • ?????????????????
  • ?????Adapter????????
  • public interface MyMessageHandler
  • void processMessage(String message)
  • Public class MyMessageHandlerImpl
  • implements MyMessageHandler

?????MessageListener!
??
61
MessageListenerAdapter ???
  • ltbean id"messageListenerAdapter"
    class"org.springframework.jms.listener.


  • adapter.MessageListenerAdapter"
    gt
  • ltconstructor-arggt
  • ltbean class"MyMessageHandlerImpl"/gt
  • lt/constructor-arggt
  • ltproperty name"defaultListenerMethod
    value"processMessage"/gt
  • lt/beangt
  • ltbean id"listenerContainer class"org.springfram
    ework.jms.listener.

  • DefaultMessageListenerContainer"gt
  • ltproperty name"concurrentConsumers"
    value"2" /gt
  • ltproperty name"connectionFactory"
    ref"jmsFactory" /gt
  • ltproperty name"destination"
    ref"destination" /gt
  • ltproperty name"messageListener"
    ref"messageListenerAdapter " /gt
  • lt/beangt
About PowerShow.com