Upload
sandesh-sharma
View
163
Download
2
Embed Size (px)
Citation preview
J2EE – Advance Java
By Sandesh Sharma
Contents
Spring
– DI– Framework– IoC– Configuration
Web Service
– Rest WebService– Spring Rest– Annotation– Configruation
Web Server
– Setup– Project Configuration
Eclipse
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 startup2. lazy dependency - created when dependent bean created3. 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 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
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!
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.html
thAnks
Question can be sent sandesh.sharma [at]jabong.com