4

Spring JNDI

Embed Size (px)

DESCRIPTION

spring

Citation preview

Page 1: Spring JNDI
Page 2: Spring JNDI

1

Spring JNDI: Template class/method automates certain work by internally performing set of operations in a sequence to complete the work.

EX: Normal Task Completion

Task1 : a()

b()

c()

d()

Template method:

public void xyz()

{

a();

b();

c();

d();

While working with plain Jndi we need to perform the entire programming of Jndi operations manually. While working with Spring JNDI we get abstraction layer on plain Jndi with simplifies the JNDI operations. Spring Jndi provides org.sf.jndiTemplate class providing abstraction layer in plain JNDI and simplify JNDI operations.

Plain JNDI programming:

1. Prepare JNDI properties 2. Prepare InitialContext Object 3. Perfrom JNDI operations (bind, list, lookup----) 4. Close InitialContext object.

Spring Jndi programming:

1. Get JndiTemplate class object through dependency injection 2. Perform Jndi operations using that JndiTemplate class object

Spring Jndi: Which allows to pass JNDI properties from Spring configuration file (which gives the flexibility of modification)

Page 3: Spring JNDI

2

1. While working with Spring JNDI it internally uses plain JNDI but as a programmer there is no need of dealing with Plain JNDI API.

2. The class that is acting as client application in Spring Bean is one concrete class we can take that class as Spring Bean and we can also perform dependency injections on the properties that class

3. main(-) is a static method so to use any variables directly in the main(-) they should be taken as static variables.

For complete please refer JNDI\Spring JNDI\NetBeans7.1 projects\SpringJNDI(Weblogic)\src\java\ListOperationJndiCallbackInterface.java

4. In the Plain JNDI programmer can use the constants of javax.naming.ContextInterface file or its values. While working with Spring JNDI we must specify only their values as JNDI properties.

CONSTANT NAME Its Value CONTEXT_FACTORY PROVIDER_URL

Factory.initialFactory Java.naming.provider.url

Callback methods and interfaces: The methods which are called by the Container automatically are called as Callback methods. The inrefaces which contains callback method is called as Callback interface. Life cycle methods are also Callback methods Spring provides lots of Abstraction layer to make programmer implementing certain functionalities underlying plain technology when that functionality it not directly available in Spring environment.

EX: In Spring JNDI environment list operation is not possible directly with JNID template class to make that operation possible and to gie chance to programmer to implement that functionality manually by using Plain JNDI API. The Spring JDNI provides Callback interface called org.spring framework.jndi.JndiCallback this interface gives one callback method called doInContext(Context ctx) represents the current context represented by JndiTemplate class obj. JndiTemplate class gives execute(-) to execute the logics related to Callback interface implementation classes.

Example Application on Spring JNDI to implement list operation by using JndiCallback interface.

For Complete program please refer JNDI\Spring JNDI\NetBeans7.1 projects\SpringJNDI(Weblogic)\src\java\ListOperationJndiCallbackInterface.java program

Page 4: Spring JNDI

3

Example Application of Spring JNDI to create sub context having bindings using JndiCallback Interface support:

For Complete Program please refer JNDI\Spring JNDI\NetBeans7.1 projects\SpringJNDI(Weblogic)\src\java\SubContextHavingBindings.java program

Keep domain server in running mode.

1. With respect to code creation of one anonymous inner class implementing JndiCallback interface.

2. In this anonymous inner class doInContext method is implemented having the logic of JNDI.

3. Object for anonymous inner class is created. 4. execute method on template class is created having the above created Anonymous inner

class object as argument value

We should not use DriverManager class of Spring API for Connection Pooling in real time projects because it does not contain actually pool connection more over creates new connection object for every getConnection method call.

To overcome this problem we an use either third party Connection Pools or Server managed Connection pools in Spring applications.

Q) What is the JDBC Connection Pool that we have in your Spring project?

A) If Spring project is Standalone project running outside the server environment then use third party pools like C3PO, Apache DBCP. If Spring project is deployable application in the Server like web application then use Server managed Connection Pool.