J2EE – Advance Java
By Sandesh Sharma
Contents

Spring
– DI
– Framework
– IoC
– Configuration

Web Service
– Rest WebService
– Spring Rest
– Annotation
– Configruation

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
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
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
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; }
…
}
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
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 startup
2. lazy dependency - created when dependent bean created
3. VERY lazy dependency - created when accessed in code
How are beans created?
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
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.
11
@Component

Use
− Type
Generic stereotype annotation for any Spring-managed
Component.
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
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
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
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
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
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
18
@Controller

Uses
− Type
Stereotypes a component as a Spring MVC controller.
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.
20
@Service

Uses
− Type

Stereotypes a component as a service.
21
@RequestMapping

Uses
− Method, Type
Maps a URL pattern and/or HTTP method to a method or controller type.
22
@RequestParam

Uses
− Parameter
Binds a request parameter to a method parameter
23
@Transactional

Uses
− Method, Type
Declares transactional boundaries and rules on a bean
and/or its methods.
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>
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 projects
Tomcat is a container for servlets
Tomcat can act as a simple standalone server for Web
applications that use HTML, servlets, and JSP
Apache is an industrial-strength, highly optimized server that
can be extended with Tomcat
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 code
A deployment directory, in which you put “live” code
Tomcat requires a particular set of directories for
your web application
It is extremely picky about having everything in the right
place!
27
Eclipse
28
Eclipse – Building & Running
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.ht
ml
thAnks
Question can be sent sandesh.sharma [at]jabong.com

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

  • 1.
    J2EE – AdvanceJava By Sandesh Sharma
  • 2.
    Contents  Spring – DI – Framework –IoC – Configuration  Web Service – Rest WebService – Spring Rest – Annotation – Configruation 
  • 3.
    3 What does Springoffer?  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
  • 4.
    4 Dependency Injection defined  Methodto 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
  • 5.
    5 What is abean?  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
  • 6.
    6 Sample bean definition <beanid="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; } … }
  • 7.
    7 What is abean 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
  • 8.
    8 • Beans arecreated 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 startup 2. lazy dependency - created when dependent bean created 3. VERY lazy dependency - created when accessed in code How are beans created?
  • 9.
    9 How are beansinjected?  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
  • 10.
    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.
  • 11.
    11 @Component  Use − Type Generic stereotypeannotation for any Spring-managed Component.
  • 12.
    12 Rest Webservice  A webservice 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
  • 13.
    Nouns  URIs are theequivalent 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
  • 14.
    Verbs  Verbs (loosely) describeactions 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
  • 15.
    GET: fetch information  Tofetch 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
  • 16.
    Four verbs forevery noun  GET to retrieve information  POST to add new information, showing its relation to old information  PUT to update information  DELETE to discard information
  • 17.
    Not such abig 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
  • 18.
    18 @Controller  Uses − Type Stereotypes acomponent as a Spring MVC controller.
  • 19.
    19 @Repository  Uses − Type Stereotypes acomponent as a repository. Also indicates that SQLExceptions thrown from the component’s methods should be translated into Spring DataAccessExceptions.
  • 20.
  • 21.
    21 @RequestMapping  Uses − Method, Type Mapsa URL pattern and/or HTTP method to a method or controller type.
  • 22.
    22 @RequestParam  Uses − Parameter Binds arequest parameter to a method parameter
  • 23.
    23 @Transactional  Uses − Method, Type Declarestransactional boundaries and rules on a bean and/or its methods.
  • 24.
    24 Configuration <?xml version="1.0" encoding="UTF-8"?> <beansxmlns="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>
  • 25.
    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 projects Tomcat is a container for servlets Tomcat can act as a simple standalone server for Web applications that use HTML, servlets, and JSP Apache is an industrial-strength, highly optimized server that can be extended with Tomcat
  • 26.
    26 Tomcat - Directories Tocreate servlets, you really should have two directory structures: A development directory, in which you can write and partially debug your code A deployment directory, in which you put “live” code Tomcat requires a particular set of directories for your web application It is extremely picky about having everything in the right place!
  • 27.
  • 28.
  • 29.
    29 Eclipse - Shortcuts  Anything to search – Cltr + Shift + L  http://www.shortcutworld.com/en/win/Eclipse.html  http://www.vogella.com/tutorials/EclipseShortcuts/article.ht ml
  • 30.
    thAnks Question can besent sandesh.sharma [at]jabong.com