+ All Categories
Home > Technology > Spring, web service, web server, eclipse by a introduction sandesh sharma

Spring, web service, web server, eclipse by a introduction sandesh sharma

Date post: 21-Jan-2017
Category:
Upload: sandesh-sharma
View: 162 times
Download: 2 times
Share this document with a friend
30
J2EE – Advance Java By Sandesh Sharma
Transcript
Page 1: Spring, web service, web server, eclipse by a introduction sandesh sharma

J2EE – Advance Java

By Sandesh Sharma

Page 2: Spring, web service, web server, eclipse by a introduction sandesh sharma

Contents

Spring

– DI– Framework– IoC– Configuration

Web Service

– Rest WebService– Spring Rest– Annotation– Configruation

Web Server

– Setup– Project Configuration

Eclipse

Page 3: Spring, web service, web server, eclipse by a introduction sandesh sharma

3

What does Spring offer? Dependency Injection

Also known as IoC (Inversion of Control) Aspect Oriented Programming

Runtime injection-based Portable Service Abstractions

The rest of spring ORM, DAO, Web MVC, Web, etc. Allows access to these without knowing how

they actually work

Page 4: Spring, web service, web server, eclipse by a introduction sandesh sharma

4

Dependency Injection defined

Method to create needed dependencies or look them up somehow without doing it in the dependent code Often called Inversion of Control (IoC)

IoC injects needed dependencies into the object instead Setters or Contructor

Primary goal is reduction of dependencies in code an excellent goal in any case This is the central part of Spring

URL: http://en.wikipedia.org/wiki/Inversion_of_Control

Page 5: Spring, web service, web server, eclipse by a introduction sandesh sharma

5

What is a bean?

Typical java bean with a unique id In spring there are basically two types

Singleton One instance of the bean created and

referenced each time it is requested Prototype (non-singleton)

New bean created each time Same as new ClassName()

Beans are normally created by Spring as late as possible

Page 6: Spring, web service, web server, eclipse by a introduction sandesh sharma

6

Sample bean definition<bean id="exampleBean" class=”org.example.ExampleBean"> <property name="beanOne"><ref bean="anotherExampleBean"/></property> <property name="beanTwo"><ref bean="yetAnotherBean"/></property> <property name="integerProperty"><value>1</value></property> </bean>

public class ExampleBean { private AnotherBean beanOne; private YetAnotherBean beanTwo; private int i; public void setBeanOne(AnotherBean beanOne) {

this.beanOne = beanOne; } public void setBeanTwo(YetAnotherBean beanTwo) {

this.beanTwo = beanTwo; } public void setIntegerProperty(int i) {

this.i = i; }…

}

Page 7: Spring, web service, web server, eclipse by a introduction sandesh sharma

7

What is a bean factory?

Often seen as an ApplicationContext BeanFactory is not used directly often ApplicationContext is a complete superset of bean

factory methods Same interface implemented Offers a richer set of features

Spring uses a BeanFactory to create, manage and locate “beans” which are basically instances of a class Typical usage is an XML bean factory which allows

configuration via XML files

Page 8: Spring, web service, web server, eclipse by a introduction sandesh sharma

8

• Beans are created in order based on the dependency graph

– Often they are created when the factory loads the definitions– Can override this behavior in bean

<bean class=“className” lazy-init=“true” />– You can also override this in the factory or context but this is

not recommended

• Spring will instantiate beans in the order required by their dependencies

1. app scope singleton - eagerly instantiated at container startup2. lazy dependency - created when dependent bean created3. VERY lazy dependency - created when accessed in code

How are beans created?

Page 9: Spring, web service, web server, eclipse by a introduction sandesh sharma

9

How are beans injected? A dependency graph is constructed based on the

various bean definitions Beans are created using constructors (mostly no-

arg) or factory methods Dependencies that were not injected via constructor

are then injected using setters Any dependency that has not been created is

created as needed

Page 10: Spring, web service, web server, eclipse by a introduction sandesh sharma

10

@Autowired

Uses Constructor, Field, Method

Declares a constructor, field, setter

method, or configuration method

to be autowired by type. Items

annotated with @Autowired do not

have to be public.

Page 11: Spring, web service, web server, eclipse by a introduction sandesh sharma

11

@Component

Use Type

Generic stereotype annotation for any Spring-managed

Component.

Page 12: Spring, web service, web server, eclipse by a introduction sandesh sharma

12

Rest Webservice

A web service is just a web page meant for a computer to request and process

More precisely, a Web service is a Web page that’s meant to be consumed by an autonomous program as opposed to a Web browser or similar UI tool

Page 13: Spring, web service, web server, eclipse by a introduction sandesh sharma

Nouns

URIs are the equivalent of a noun Most words in English are nouns, from cat to antidisestablishmentarianism

The REST language has trillions of nouns for all the concepts in all the heads and files of all the people in the world

Page 14: Spring, web service, web server, eclipse by a introduction sandesh sharma

Verbs

Verbs (loosely) describe actions that are applicable to nouns

Using different verbs for every noun would make widespread communication impossible

In programming we call this “polymorphism” Some verbs only apply to a few nouns In REST we use universal verbs only

Page 15: Spring, web service, web server, eclipse by a introduction sandesh sharma

GET: fetch information

To fetch a web page, the browser does a GET on some URI and retrieves a representation (HTML, plain text, JPEG, or whatever) of the resource identified by that URI

GET is fundamental to browsers because mostly they just browse

REST requires a few more verbs to allow taking actions

Page 16: Spring, web service, web server, eclipse by a introduction sandesh sharma

Four verbs for every noun

GET to retrieve information POST to add new information, showing its relation to

old information PUT to update information DELETE to discard information

Page 17: Spring, web service, web server, eclipse by a introduction sandesh sharma

Not such a big deal

The Web already supports machine-to-machine integration

What's not machine-processable about the current Web isn't the protocol, it's the content

Page 18: Spring, web service, web server, eclipse by a introduction sandesh sharma

18

@Controller

Uses Type

Stereotypes a component as a Spring MVC controller.

Page 19: Spring, web service, web server, eclipse by a introduction sandesh sharma

19

@Repository

Uses Type

Stereotypes a component as a repository. Also

indicates that SQLExceptions thrown from the

component’s methods should be translated into Spring

DataAccessExceptions.

Page 20: Spring, web service, web server, eclipse by a introduction sandesh sharma

20

@Service

Uses Type

Stereotypes a component as a service.

Page 21: Spring, web service, web server, eclipse by a introduction sandesh sharma

21

@RequestMapping

Uses Method, Type

Maps a URL pattern and/or HTTP method to a method or controller type.

Page 22: Spring, web service, web server, eclipse by a introduction sandesh sharma

22

@RequestParam

Uses Parameter

Binds a request parameter to a method parameter

Page 23: Spring, web service, web server, eclipse by a introduction sandesh sharma

23

@Transactional

Uses Method, Type

Declares transactional boundaries and rules on a bean and/or its methods.

Page 24: Spring, web service, web server, eclipse by a introduction sandesh sharma

24

Configuration<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans">

<context:component-scan base-package="com.hiyp.service.user.controller" /><mvc:annotation-driven />

<bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/hiyp_service" /><property name="username" value="root" /><property name="password" value="hiyp12#" /></bean>

<bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="annotatedClasses"><list><value>com.hiyp.services.model.User</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop></props></property></bean>

<bean id="txManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean>

<bean id="persistenceExceptionTranslationPostProcessor"class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<bean id="userDao" class="com.hiyp.services.user.dao.UserDaoImpl"></bean><bean id="userServices" class="com.hiyp.services.user.UserServicesImpl"></bean></beans>

Page 25: Spring, web service, web server, eclipse by a introduction sandesh sharma

25

Web Server - Tomcate

The Apache Jakarta Project “creates and maintains open source solutions on the Java platform for distribution to the public at no charge”Apache Jakarta Tomcat--or just “Tomcat”--is one of those projectsTomcat is a container for servletsTomcat can act as a simple standalone server for Web applications that use HTML, servlets, and JSPApache is an industrial-strength, highly optimized server that can be extended with Tomcat

Page 26: Spring, web service, web server, eclipse by a introduction sandesh sharma

26

Tomcat - Directories

To create servlets, you really should have two directory structures:A development directory, in which you can write and partially debug your codeA deployment directory, in which you put “live” codeTomcat requires a particular set of directories for your web applicationIt is extremely picky about having everything in the right place!

Page 27: Spring, web service, web server, eclipse by a introduction sandesh sharma

27

Eclipse

Page 28: Spring, web service, web server, eclipse by a introduction sandesh sharma

28

Eclipse – Building & Running

Page 29: Spring, web service, web server, eclipse by a introduction sandesh sharma

29

Eclipse - Shortcuts

Any thing to search – Cltr + Shift + L http://www.shortcutworld.com/en/win/Eclipse.html http://www.vogella.com/tutorials/EclipseShortcuts/

article.html

Page 30: Spring, web service, web server, eclipse by a introduction sandesh sharma

thAnks

Question can be sent sandesh.sharma [at]jabong.com


Recommended