03.egovframe runtime environment training book

38
eGovFrame Training Book Runtime Environment eGovFrame Center 2012

Upload: chuong-nguyen

Post on 19-May-2015

754 views

Category:

Documents


3 download

DESCRIPTION

eGovFrame - Framework for e-government Run-time Environment

TRANSCRIPT

Page 1: 03.egovFrame Runtime Environment Training Book

eGovFrame Training Book Runtime Environment

eGovFrame Center

2012

Page 2: 03.egovFrame Runtime Environment Training Book

Table of contents

Overview

Foundation Layer

Persistence Layer

Presentation Layer

Page l 2

I

II

III

IV

Page 3: 03.egovFrame Runtime Environment Training Book

Page l 3

Runtime Environment Composition Overview

Runtime Environment is the foundation of software applications and provides the basic

functionality required to run software

eGovFrame Runtime Environment

Foundation Layer

Service Group

Presentation

Layer

Business Logic

Layer

Persistence

Layer

Integration

Layer

UX

Layer

Page 4: 03.egovFrame Runtime Environment Training Book

Page l 4

Runtime Environment Composition of eGovFrame

Runtime Environment

Presentation

Layer

Persistent Layer

Foundation Layer

Integration Layer

Application Program

DB Client Screen

UX Layer

Biz Logic Layer

Runtime Environment is the foundation of software applications and provides the basic

functionality required to run an enterprise application

Page 5: 03.egovFrame Runtime Environment Training Book

Page l 5

Composition of the runtime environment

The runtime environment consists of 6 service groups, 38 services. Support

MVC, DI, AOP, etc patterns, based on Spring framework

Composition of eGovFrame

Runtime Environment

Foundation Layer

AOP

FTP

Object Pooling

XML Manipulation

Cache

Hot-Deploy

Marshalling/Unmarshalling

Compress/Decompress

ID Generation

Property

Encryption/Decryption

IoC Container

Resource

Excel

Logging

Scheduling

File Handling

Mail

String Util

Integration

Layer

Persistence Layer Biz Logic

Layer

TBD

TBD

DataSource

Transaction

Biz. Process

Management

Exception Handling

Message Service

Naming Service

Web Service

Data Access

ORM

Presentation Layer

Ajax Support

MVC

UI Adaptor

Internationalization

Security

File Upload/Download

UX Layer

UX/UI Controller Component

HTML5

JavaScript Module App Framework

CSS3

Page 6: 03.egovFrame Runtime Environment Training Book

Page l 6

Composition of the runtime environment

The runtime environment consists of 6 service groups, 38 services. Support

MVC, DI, AOP, etc patterns, based on Spring framework

Composition of eGovFrame

Runtime Environment

Foundation Layer

AOP

FTP

Object Pooling

XML Manipulation

Cache

Hot-Deploy

Marshalling/Unmarshalling

Compress/Decompress

ID Generation

Property

Encryption/Decryption

IoC Container

Resource

Excel

Logging

Scheduling

File Handling

Mail

String Util

Integration

Layer

Persistence Layer Biz Logic

Layer

TBD

TBD

DataSource

Transaction

Biz. Process

Management

Exception Handling

Message Service

Naming Service

Web Service

Data Access

ORM

Presentation Layer

Ajax Support

MVC

UI Adaptor

Internationalization

Security

File Upload/Download

UX Layer

UX/UI Controller Component

HTML5

JavaScript Module App Framework

CSS3

Page 7: 03.egovFrame Runtime Environment Training Book

Page l 7

Runtime Environment OSS Overview

Runtime Environment consists of a variety of open source software

Open Source S/W Version Applied Service Reference URL

Spring 3.0.5 IoC Container, AOP, Property, Resource http://www.springsource.org/

Spring Security 2.0.4 Server Security http://static.springsource.org/spring-security/site/

Log4j 1.3 Logging http://logging.apache.org/

EHCache 2.4.1 Cache http://ehcache.sourceforge.net/

Commons Compress 1.1 Compress/Decompress http://commons.apache.org/compress/

Commons VFS 1.0 File Handling http://commons.apache.org/vfs/

Commons FileUpload 1.2.2 File Upload/Download http://commons.apache.org/fileupload

Commons Net 3.0.1 FTP http://commons.apache.org/net/

Common Email 1.1 Mail http://commons.apache.org/email/

Commons Pool 1.5.6 Object Pooling http://commons.apache.org/pool/

Jakarta Regexp 1.5 String Util http://jakarta.apache.org/regexp/

Apache Xerces 2 2.2.9 XML Manipulation

http://xerces.apache.org/xerces2-j/

JDOM 1.1 http://www.jdom.org/

java simplified encryption (jasypt) 1.7 Encryption/Decryption http://www.jasypt.org/

Apache POI 3.2 Excel

http://poi.apache.org/

jXLS 0.9.9 http://jxls.sourceforge.net/

Castor 1.2 Marshalling/Unmarshalling

http://www.castor.org/

Apache XML Beans 2.4 http://xmlbeans.apache.org/

Quartz 1.8.5 Scheduling http://www.opensymphony.com/quartz/

Page 8: 03.egovFrame Runtime Environment Training Book

Page l 8

Features & Effects Overview

Features

• Adopt proven open source SW, optimized for e-government projects

• Utilize Spring framework as a core foundation that is a pervasive lightweight framework

• Apply DI, AOP, MVC, etc for SW architecture and implementation

• Provide a common interface for integrating solutions as defining associated standard interface

Effects

• Improve development productivity

• Improve e-government system reusability

• Improve interoperability of e-government system

• Standardize e-government application software

• Promote Open Source Software

• Enhance SMEs competitiveness

Page 9: 03.egovFrame Runtime Environment Training Book

Page l 9

RE*) Open Source - Spring Framework Overview

Overview

• Open source framework supports Java based enterprise applications

• POJO based lightweight container (very loosely coupled)

• Developed by Rod Johnson (Based on “Expert one-on-one J2EE Design and Development”)

Spring Mission and Goals

• Spring should be fun and easier than J2EE in use.

• Spring should not depend on the API.

• Spring integrates with existing good solutions rather than competing.

EJB Problems

• Heavy weight remote model

• High complexity + Portability, Difficult to ensure performance and scalability

Foundation Layer

· RE*) : Runtime Environment

Page 10: 03.egovFrame Runtime Environment Training Book

Page l 10

Spring Framework Composition

Spring Framework consists of DAO, ORM, AOP, JEE, Web, based on the core that governs the

Bean object life cycle

Foundation Layer

Page 11: 03.egovFrame Runtime Environment Training Book

Page l 11

IoC Container

Overview

• Manage creating objects and object dependencies by external setting, rather than hard coding

within source code

• IoC is an abbreviation of the “Inversion of Control” or referred to “Reverse control”

• Improve flexibility and scalability

Key Features

• Dependency Injection

- Inject dependencies among objects in the external setting

- Framework determines dependencies(objects) and relationships which will be used at run time

• Bean Lifecycle Management

- Manage the life cycle of object creation, destruction, etc

Foundation Layer

Page 12: 03.egovFrame Runtime Environment Training Book

Page l 12

IoC Container

Inversion of Control (IoC) is an object-oriented programming practice where the object coupling

is bound at run time by an assembler object and is typically not known at compile

time using static analysis.

In traditional programming, the flow of the business logic is determined by objects that are statically assigned to one another.

With IoC, the flow depends on the object graph that is instantiated by the assembler and is made possible by object

interactions being defined through abstractions. The binding process is achieved through dependency injection.

Inversion of control as a design guideline serves the following purposes:

• There is a decoupling of the execution of a certain task from implementation.

• Every module can focus on what it is designed for.

• Replacing modules has no side effect on other modules.

IoC Container

The container is an object that implements the concept of Inversion of Control. It manages the object lifecycle from

creation to destruction and takes control for all objects.

Dependency Injection (DI)

DI is a software design pattern that allows a choice of component to be made at run-time rather than compile time. It is a

style of object configuration in which an objects fields and collaborators are set by an external entity, such as bean

definition.

Foundation Layer

Page 13: 03.egovFrame Runtime Environment Training Book

Page l 13 IoC Container

Key Features

• DI (Dependency Injection)

Foundation Layer

[Module Call Conceptual View – Non DI

[Module Call Conceptual View –Use DI

• Bean Lifecycle Management

A B

C

Configuration

Page 14: 03.egovFrame Runtime Environment Training Book

Page l 14

IoC Container

Non-IoC/DI vs IoC/DI

Non-IoC/DI

Foo

Bar

Baz

package x.y;

public class Foo {

public void process() {

Bar bar = new Bar();

Baz baz = new Baz();

// ...

}

}

IoC/DI

Foo

Bar

Baz

package x.y; public class Foo { public Foo(Bar bar, Baz baz) { // ... } }

vs

Foundation Layer

<beans> <bean name="foo" class="x.y.Foo"> <constructor-arg> <bean class="x.y.Bar"/> </constructor-arg> <constructor-arg> <bean class="x.y.Baz"/> </constructor-arg> </bean> </beans>

Page 15: 03.egovFrame Runtime Environment Training Book

Page l 15

IoC Container Foundation Layer

Container

• creates objects and take a role as an

assembler to inject dependency between

objects

Configuration Metadata

• represents how the Spring container

instantiates, configures, and assembles the

objects in an application. It is traditionally

supplied in a simple and intuitive XML

format.

Bean

• is an object that created and managed by

Spring IoC Container

Fully configured system

Ready for use

The Spring

Container

Business Objects

(POJOs)

produces

Configuration

Metadata

Page 16: 03.egovFrame Runtime Environment Training Book

Page l 16

IoC Container Foundation Layer

In Spring, the Bean Factory is an IoC container to control beans and inject dependency

among them. The Application Context is spring's more advanced container(include

Bean Factory) and it adds more enterprise-specific functionality. Normally, the

Application Context is used over the BeanFactory.

BeanFactory

• The Bean Factory is the actual container which instantiates, configures, and manages a

number of beans. (ex: dependency injection, bean lifecyclemanagement)

• It is represented by the interface org.springframework.beans.factory.BeanFactory, for

which there are multiple implementations.

Application Context

• Extend BeanFactory interface and add functionality such as Spring AOP, message

resource, internationalization, event processing, etc to develop and run an enterprise

application

• As the ApplicationContext includes all functionality of the BeanFactory, it is generally

recommended that it be used over the BeanFactory

Page 17: 03.egovFrame Runtime Environment Training Book

Page l 17 IoC Container

package x.y;

public class Foo {

public void setBar(Bar bar) {

// ...

}

public void setBaz(Baz baz) {

// ...

}

}

<beans>

<bean name="foo" class="x.y.Foo">

<property name="bar">

<bean class="x.y.Bar" />

</property>

<property name="baz">

<bean class="x.y.Baz" />

</property>

</bean>

</beans>

Injection Type

• Constructor Injection : Configuring dependency through an argument constructor

• Setter Injection : Configuring dependency through setters on the object.

Foundation Layer

Page 18: 03.egovFrame Runtime Environment Training Book

Page l 18

IoC Container

XML configuration file

• Bean configuration file has <beans/> as root, and provides related Namespace and Schema

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="..." class="...">

<!-- collaborators and configuration for this bean go here -->

</bean>

<bean id=“xmlEmpDAO” class=“egovframework.example.service.impl.XmlEmpDAO“ />

<bean id=“xmlEmpService” class=“egovframework.example.service.impl.XmlEmpServiceImpl” >

<property name=“xmlDAO” ref=“xmlEmpDAO” />

</bean>

<bean name=“otherExampleBean” class=“egovframework.example.OtherExampleBean“ />

</beans>

Foundation Layer

Page 19: 03.egovFrame Runtime Environment Training Book

Page l 19

Annotation

• Available to use Java annotation instead of XML configuration files (Java 5 or higher)

• As using annotation, simplify configuration files, and mapping between view pages and objects or methods

can be clearly defined.

IoC Container Foundation Layer

Page 20: 03.egovFrame Runtime Environment Training Book

Page l 20 IoC Container

Annotation Type

• Using Java Annotation, Spring Bean definitions can be set, then need to add namespace

and element

Foundation Layer

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config/>

</beans>

Page 21: 03.egovFrame Runtime Environment Training Book

Page l 21 IoC Container

Auto Scan, based on Annotation

• Normally declaring all the beans or components in XML bean configuration file, so that Spring

container can detect and register beans or component.

• Spring is also able to auto scan, detect and instantiate beans from pre-defined project package.

• ‘base-package’ attribute is the basic package to find components, can be assigned multiple package

with ‘,’ separation mark.

• <context:exclude-filter> tag and <context:include-filter> tag are used to state what component is

included or excluded to the scanning

Foundation Layer

<?xml version="1.0" encoding="UTF-8"?>

<beans ··· >

<context:component-scan base-package=“egovframework“/>

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>

</context:component-scan>

</beans>

Page 22: 03.egovFrame Runtime Environment Training Book

Page l 22 IoC Container

Annotation type

• @Required : Applied to setter method, mark a property as being 'required-to-be-set' (i.e. an annotated

(setter) method of a class must be configured to be dependency injected with a value) ethods

Foundation Layer

public class SimpleMovieLister {

// the SimpleMovieLister has a dependency on the MovieFinder

private MovieFinder movieFinder;

// a setter method so that the Spring container can 'inject' a MovieFinder

@Required

public void setMovieFinder(MovieFinder movieFinder) {

this.movieFinder = movieFinder;

}

// business logic that actually 'uses' the injected MovieFinder is omitted...

}

• @Autowired : auto wire a bean on the setter method, constructor or a field. It can autowire property

in a particular bean

• @Resource : marks a resource that is needed by the application. It finds the target bean with name

• @PostConstruct, @PreDestroy : Use to assign Instantiation callback, Destruction callback methods

@Service public UserServiceImpl implements UserService {

@Resource (name="userDAO")

private UserDAO userDAO;

}

Page 23: 03.egovFrame Runtime Environment Training Book

Page l 23

@Resource @Resource annotation의 name 값으로 대상 bean을 찾을 수 있다. @Resource annotation은 field 또는

메소드에 사용할 수 있다.

IoC Container Foundation Layer

public class SimpleMovieLister {

private MovieFinder movieFinder;

@Resource(name="myMovieFinder")

public void setMovieFinder(MovieFinder movieFinder) {

this.movieFinder = movieFinder;

}

}

Page 24: 03.egovFrame Runtime Environment Training Book

Page l 24 IoC Container

Auto-detecting components

• Set Name- Set bean name with @Component, @Repository, @Service, @Controller annotation’s

name value

Foundation Layer

@Service("myMovieLister")

public class SimpleMovieLister {

// ...

}

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

@Repository

public class MovieFinderImpl implements MovieFinder {

// ...

}

ApplicationContext for web application

• Spring provides classes for easy WebApplicationContext setting

• Add following settings in web.xml for ApplicationContext setting with the Listener

(Servlet 2.4 or later)

Page 25: 03.egovFrame Runtime Environment Training Book

Page l 25 AOP

Overview

• Aspect-oriented programming (AOP) is a programming paradigm which aims to

increase modularity by allowing the separation of cross-cutting concerns

• AOP is programming techniques to support the processing of modularizing Logging, security,

transactions, and common functions without changing existing business logic

OOP OOP + AOP

AOP

Applied

Configuration

File

Foundation Layer

Page 26: 03.egovFrame Runtime Environment Training Book

Page l 26

AOP

Main Concept

• Advice, JoinPoint, Pointcut, Weaving, Aspect

Benefits

• Remove duplicate code

• Improve readability of the business logic

• Improve productivity

• Improve reusability

• Increase ease of change

Advice

Weaving JoinPoint

Pointcut

Foundation Layer

Page 27: 03.egovFrame Runtime Environment Training Book

Page l 27

AOP

Relations for AOP components

Aspect

Advice

소스코드

Join point is the time

which a method is called in Spring

A set of Join point (condition: select*)

Processing what you want to apply

Joinpoint

EmployeeService

insertEmployee()

updateEmployee()

selectEmployeeList()

selectEmployee()

deleteEmployttList()

Pointcut

Source Code

Foundation Layer

Page 28: 03.egovFrame Runtime Environment Training Book

Page l 28

AOP

Develop functions of cross-cutting concerns (Create Advice)

• Advice, JoinPoint, Pointcut

Apply AOP (Create setting file)

• Remove duplicated code

public class AdviceUsingXML {

public void beforeTargetMethod(JoinPoint thisJoinPoint) {

Class clazz = thisJoinPoint.getTarget().getClass();

String className = thisJoinPoint.getTarget().getClass().getSimpleName();

String methodName = thisJoinPoint.getSignature().getName();

// Obtain the target method Logger and log the current class and method status information

Log logger = LogFactory.getLog(clazz);

logger.debug(className + "." + methodName + " executed.");

}

}

<aop:config>

<aop:pointcut id="targetMethod"

expression="execution(* egovframework.rte.fdl.aop.sample.*Sample.*(..))" />

<aop:aspect ref="adviceUsingXML">

<aop:before pointcut-ref="targetMethod" method="beforeTargetMethod" />

</aop:aspect>

</aop:config>

Foundation Layer

Page 29: 03.egovFrame Runtime Environment Training Book

Page l 29

Data Source

Overview

• Service for providing database connection

• Remove dependencies about business logic and database connection

Key Features

• Service to connect database

- Create database connection, using JDBC driver

Persistence Layer

<bean id="dataSource”

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="${driver}" />

<property name="url" value="${dburl}" />

<property name="username" value="${username}" />

<property name="password" value="${password}" />

</bean> • driverClassName : JDBC driver class name

• url : Database access JDBC URL

• username : Database access username

• password : Database access password

Page 30: 03.egovFrame Runtime Environment Training Book

Page l 30

Data Source

Key Features

• DBCP DataSource

- Database connection module, using JDBC driver. This is database connection pool, called Commons DBCP

in Jakarta projects.

<bean id="dataSource” class="org.apache.commons.dbcp.BasicDataSource” destroy-method="close">

<property name="driverClassName" value="${driver}" />

<property name="url" value="${dburl}" />

<property name="username" value="${username}" />

<property name="password" value="${password}" />

<property name="defaultAutoCommit" value="false" />

<property name="poolPreparedStatements" value="true" />

</bean>

• driverClassName : jdbc driver class name

• url : DataBase url

• username : DataBase access user name

• password : DataBase access password

• defaultAutoCommit : configure auto-commit function about connection

• poolPreparedStatements : PreparedStatement configuration (user or not)

Persistence Layer

Page 31: 03.egovFrame Runtime Environment Training Book

Page l 31

Data Access

Overview

• Provide a service to respond in a consistent manner for a variety of database solutions and database

access technologies

• Improve business efficiency by providing abstract data access method

• SQL statement can be mapped easily to JavaBeans(or Map), using iBATIS which apply simple XML

technology rather than java code to access a relational database

Key features

• Provide an abstraction access method for JDBC (Do not directly call JDBC related API)

• Support removing SQL statement from Java source code (Ensure ease of management /

maintenance / tuning)

• Support various binding/mapping for input/output objects of Query execution

• Support dynamic query change through dynamic SQL

• Support a variety of DB processing (Batch SQL, Paging, Callable statement, BLOB/CLOB, etc)

Persistence Layer

Page 32: 03.egovFrame Runtime Environment Training Book

Page l 32

Data Access

Features

• iBATIS is a persistence framework that emphasize on simplicity and minimize the

repetitive and complex DB query using SQL Map

- Support Stored Procedure with XML or mapping between SQL statements and Java objects, as

emphasis on simplicity, one of iBATIS main ideas

- Persistence framework, developed by Clinton Begin(Apache Software Foundation) in 2001

Elements iBATIS Hibernate comparison

Round Trip Delay Time Short Long

Hibernate takes more time due to

features such as automatic generation

of query

Flexibility Good Poor

Learning Curve Short Long iBATIS is much more similar to JDBC

SQL knowledge Should be higher Do not required much

Persistence Layer

Page 33: 03.egovFrame Runtime Environment Training Book

Page l 33

Spring with iBatis Persistence Layer

Spring and iBATIS

• In order to use iBATIS in Spring, define the sqlMapClient of iBATIS like below

• Bean definition (context-sqlMap.xml)

- id and class name are fixed values

- dataSource : make Spring manages DataSource as using DataSource bean id which configured in

Spring

- configLocation : configure the location of SqlMap Configuration file다

<beans . . .> <!-- SqlMap setup for iBATIS Database Layer --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" p:dataSource-ref="dataSource" p:configLocation-ref="classpath:/egovframework/sqlmap/rte/sql-map-config.xml" /> </beans>

Page 34: 03.egovFrame Runtime Environment Training Book

Page l 34 Spring with iBatis Persistence Layer

Persistence Layer development sequence, using iBATIS

• Define SqlMapClient Bean (context-sqlMap.xml)

• Define the location of SQL Map XML in SQL Map Config XML

• Configure SQL related information in SQL Map XML files

• Create a DAO class

Extends EgovAbstractDAO class which inherits SqlMapClientDaoSupport to make DB query and run,

using SQL Map XML query id

Page 35: 03.egovFrame Runtime Environment Training Book

Page l 35

MVC

Overview

• MVC(Model-View-Controller) pattern separates code by

Model, View, Controller 3 factors, depending on functions

- Model : objects include application data

and business logic

- View : display the information of model to users

- Controller : intermediary of Model and View

(Reflect the status of model change, based on the user request and select view for a response)

• MVC pattern reduces dependency by separating the UI code and business code, increases reusability

and can be easier to change

• Though ‘MVC pattern’ is not used only for Web Framework, ‘MVC service’ in eGovFrame means

Web MVC Framework utilizing MVC pattern

Presentation Layer

Page 36: 03.egovFrame Runtime Environment Training Book

Page l 36

MVC

Relationship and flow between Spring MVC components

• When client’s request comes in, the DispatcherServlet first receives the request.

• The DispatcherServlet consults the HandlerMapping and invokes the Controller associated with the request.

• The Controller process the request by calling the appropriate service methods and returns a ModeAndView object to

the DispatcherServlet. The ModeAndView object contains the model data and the view name.

• The DispatcherServlet sends the view name to a ViewResolver to find the actual View to invoke.

• Now the DispatcherServlet will pass the model object to the View to render the result.

• The View with the help of the model data will render the result back to the user.

Presentation Layer

Page 37: 03.egovFrame Runtime Environment Training Book

Page l 37

MVC

Spring MVC core components

• DispatcherServlet

- The front controller of Spring MVC Framework. Supervise life cycle of web request and

respond.

• HandlerMapping

- Decide which controller will process the target URL when web request comes in.

• Controller

- Perform the business logic and the result data is reflected in the ModelAndView.

• ModelAndView

- Consist of model data object which reflects controller’s result and target page information (or

view object).

• ViewResolver

- Provide a mapping between view names and actual views.

• View

- Display the model object which is result data.

Presentation Layer

Page 38: 03.egovFrame Runtime Environment Training Book

Page l 38