• What is mean by Dependency Injection? Or What do you mean by Inversion of Control?
Dependency injection (DI) is a programming design pattern and architectural model, sometimes also referred to as inversion of control or IOC, although technically speaking, dependency injection specifically refers to an implementation of a particular form of IOC.
Dependancy Injection describes the situation where one object uses a second object to provide a particular capacity. For example, being passed a database connection as an argument to the constructor instead of creating one internally. The term "Dependency injection" is a misnomer, since it is not a dependency that is injected, rather it is a provider of some capability or resource that is injected. There are three common forms of dependency injection: setter-, constructor- and interface-based injection.
Dependency injection is a way to achieve loose coupling. Inversion of control (IOC) relates to the way in which an object obtains references to its dependencies. This is often done by a lookup method. The advantage of inversion of control is that it decouples objects from specific lookup mechanisms and implementations of the objects it depends on. As a result, more flexibility is obtained for production applications as well as for testing.
  • What are the modules in Spring?
  1. The core container:
    The core container provides the fundamental functionality of the Spring framework. In this module primary component is the BeanFactory, an implementation of the Factory pattern. The BeanFactoryapplies the Inversion of Control (IOC) pattern to separate an application's configuration and dependency specification from the actual application code.
  2. Spring context module :
    TThe Spring context is a configuration file that provides context information to the Spring framework. The Spring context includes enterprise services such as e-mail, JNDI, EJB, internalization, validation, scheduling and applications lifecycle events. Also included is support for the integration with templating frameworks such as velocity.
  3. Spring AOP module:
    The Spring AOP module allows a software component to be decorated with additional behavior, through its configuration management feature. As a result you can easily AOP-enable any object managed by the Spring framework. The Spring AOP module provides transaction management services for objects in any Spring-based application. With Spring AOP you can incorporate declarative transaction management into your applications without relying on EJB components.
  4. Spring DAO module:
    The Spring DAO module provides a JDBC-abstraction layer that reduces the need to do tedious JDBC coding and parsing of database-vendor specific error codes. Also, the JDBC package provides a way to do programmatic as well as declarative transaction management, not only for classes implementing special interfaces, but for all your POJOs (plain old Java objects).
  5. Spring ORM module:
    : Spring provides integration with OR mapping tools like Hibernate, JDO and iBATIS. Spring transaction management supports each of these ORM frameworks as well as JDBC.
  6. Spring Web module:
    The Web context module provides basic web-oriented integration features builds on top of the application context module, providing contexts for Web-based applications. As a result, the Spring framework supports integration with Jakarta Struts. The Web module also eases the tasks of handling multi-part requests and binding request parameters to domain objects.
  7. Spring MVC framework module:
    Spring provides a pluggable MVC architecture. The users have a choice to use the web framework or continue to use their existing web framework. Spring separates the roles of the controller; the model object, the dispatcher and the handler object which makes it easier to customize them. Spring web framework is view agnostic and does not push the user to use only JSPs for the view. The user has the flexibility to use JSPs, XSLT, velocity templates etc to provide the view.
  • What is a BeanFactory and XMLBeanFactory?
Bean factory is a container. It configures, instantiates and manages a set of beans. These beans are collaborated with one another and have dependencies among themselves. The reflection of these dependencies are used in configuring data that is used by BeanFactory.
XMLBeanFactory is a bean factory that is loaded its beans from an XML file.
  • What are Inner Beans?
A bean inside another bean is known as Inner Bean. They are created and used on the fly, and can not be used outside the enclosing beans. The Id and scope attributes for inner beans are of no use.
  • What is DataAccessException?
DataAccessException is an unchecked RuntimeException. These type of exceptions are unforced by users to handle. This exception is used to handle the errors occurring when the details of the database access API in use, such as JDBC.
Technorati Tags:

The Java Message Service is a Java API that allows applications to create, send, receive, and read messages. Designed by Sun and several partner companies, the JMS API defines a common set of interfaces and associated semantics that allow programs written in the Java programming language to communicate with other messaging implementations.

The JMS API minimizes the set of concepts a programmer must learn to use messaging products but provides enough features to support sophisticated messaging applications. It also strives to maximize the portability of JMS applications across JMS providers in the same messaging domain.

The JMS API enables communication that is not only loosely coupled but also

  • Asynchronous. A JMS provider can deliver messages to a client as they arrive; a client does not have to request messages in order to receive them.
  • Reliable. The JMS API can ensure that a message is delivered once and only once. Lower levels of reliability are available for applications that can afford to miss messages or to receive duplicate messages.

The JMS Specification was first published in August 1998. The latest version of the JMS Specification is Version 1.0.2b, which was released in August 2001. You can download a copy of the Specification from the JMS Web site, http://java.sun.com/products/jms/.

Technorati Tags: ,,
LiveJournal Tags: ,,

This Article presents two important new features of EJB 3.0 which is bundeled with Java EE 5.0 specification, which are Annotations and Dependency Injection. The solid reason for this artice is , it will give us an idea how these features are used, so on their encounter in other articles of EJB3.0, we can understand their functionality. So we can say learning this article will give us a smooth drive in later other EJB 3.0 core concepts. So now we are going to start to see how Annotations and Dependency Injection has made the life of a developer easier and code more simple and managable.

2.Annotatons in EJB 3.0

Annotations came into existence with Java 5.0. Annotations essentially allow us to attatch some additional information to a Java class, interface,methods and variables. The additional information that we supply can be used by development environments like Eclipse, Java compiler, a deployment tool, a persistence provider like Hibernate or a runtime environment like the Java EE container. So, annotations can be called custom java modifiers (in addition to public final, static etc.) that can be used by anything handling java source code (like java compiler) or byte code(like java runtime environment, j2ee container ).

In fact, annotations are used to affect the way programs are treated mostly by tools. These tools use theseannotations applied in program to produce derived files.

  • Tools are- compiler, IDE (Integreated Development Environment), Runtime tools like(J2ee container etc.).
  • Derived files are :- New Java code, deployment descriptor, class files.

In EJB 3.0 annotations are used to give our components "configuration metadata" that "configuration metadata" in earlier version EJB 2.0 we used to give by way of Deployment Descriptor. So, use of annotations eliminates the need of XML based Deployment Descriptor (exceptions are there like Default Listener as we will see can be configured only through Deployment Descriptor not by using annotation). Annotations provide us or benifit us by

  • Declarative Programming
  • Less coding since tool will generate the boliler plate code from annotations in the source code

Simple Example

	
///Annotation Example on Java 5.0 to understand what is it and how it can be utilised...
//annotation interface
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)

public @interface ObjColor
{
String minecolor() default "Red";
}
.......
// plain interface
public interface Bounceable
{
public void bounce();
}
.....
// Class using plain interface and annotation
@ObjColor(minecolor="Yellow")
public class Ball implements Bounceable
{
public void bounce(){
String ballColor=Ball.class.getAnnotation(ObjColor.class).minecolor();//getting the value of minecolor element of ObjColor annotation
System.out.println("Hey, I am a Ball , I implement Bounceable interface "+
"so now I can bounce...Can you see some information has been attached to me"+
" which is neither extended nor implemented , and "+
"for this no new field or mathod has been added"+
"professional calls it Annotation and says i can use it ..."+
"and color of mine is "+ballColor);
}
public static void main(String[] args)
{
Ball b=new Ball();
b.bounce();
}
}


Explanation of Simple Example



Lets understand this simple code...




  • Here @ObjColor is an annotation applied on the class (or we can say attached to the class). It tells the tools , which will make use of it that the color of ball is Red that may be used by anybody. More important this extra information is attached to the class without extending , implementing or adding extra method or variable. this information can be used when required inside...like we have got value of color of ball...( String ballColor=Ball.class.getAnnotation(ObjColor.class).minecolor();//getting the value of minecolor element of ObjColor annotation)


  • As annotation is an special kind of interface. It must be imported from where it is defined. As we can see that this interface has @Target(ElementType.TYPE) it means this @ObjColor annotation can be applied to Class, interface (including annotation type), or enum declaration. and it has @Retention(RetentionPolicy.RUNTIME), which means this extra details attached is available upto runtime( Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively).



3.Replacing Deployment Descriptor with Annotations in EJB 3.0



In container managed environment , components need many services (Like. Transaction, Security, Remotability etc.) which are provided by the container. For this purpose service configuration is required to be given to component which is used by container. Service configuration using Java Metadata annotations is easily the most important change in EJB 3.0. As we will see in other articles that annotations simplify the EJB programming model, eliminate the need for verbose(detailed) Deployment Descriptor. lets have a quick refresh about DD(Deployment Descriptor).



3.1.What is Deployment Descriptor?


A Deployment Descriptor is simply an XML based file that contains application confguration information. Every Deployment unit in Java EE can have a Deployment Descriptor that describes its contents and environment. Some example of deployment units are the Enterprise Archieve (EAR),Web Application Archieve (WAR), and the EJB module(ejb-jar). Deployment Descriptor file example as web.xml in Web Application Archieve and ejb-jar.xml in EJB module. One who has used EJB 2.0 , knows that how verbose the XML (ejb-jar.xml) descriptor was. EJB 3.0 makes the use of DD optional. We can use metadata annotations instead of descriptor entries.



Notes : Both Annotations and DD can be used togetherand Deployment Descriptor entries overrides configuration values hard coded in EJB components. The most obvious way of mixing and matching annotation and XML -Metadata is to use for deployment specific configurations while using annotations for everything else.



3.2.Weakness of Annotations

It isn't always a good idea to mix and match configuration with source code such as annotations. This means that you would have to change source code each time you made a configuration change to something like a database connection resource or deployment descriptor environment entry.

3.3.Common Annotations on EJB 3.0


Here is a list of Metadata annotations introduced on Java EE. Although primarily geared towards EJB 3.0, thseseannotations apply to Java EE components such as Servlets and JSF managed beans as well as application clients. So we will see this list in relevence like usage of annotation and the Java EEcomponents which can use it. Annotationsdefined in the javax.annotation.* package are defined by the common Metadata Annotations API (JSR-250).




  • javax.annotation.Resource :- Its usage is in Dependency injection of resources such as Data Source, JMS Objects etc. Components that can use this annotation are as : EJB, Web Components, application client.


  • javax.annotation.PostConstruct :- Its usage is in declaring a method, a life cycle method. Components that can use this annotation are as : EJB, Web Components.


  • javax.annotation.PreDestroy :- Its usage is in declaring a method , a life cycle method. Components that can use this annotation are as : EJB, Web Components.


  • javax.annotation.security.RunAs :- Its usage is in security related coding. Components that can use this annotation are as : EJB, Web Components.


  • javax.annotation.security.RolesAllowed :- Its usage is in security related coding. Components that can use this annotation are as : EJB.


  • javax.annotation.security.PermitAll :- Its usage is in security related coding. Components that can use this annotation are as : EJB.


  • javax.annotation.security.DenyAll :- Its usage is in security related coding. Components that can use this annotation are as : EJB.


  • javax.annotation.security.DeclareRoles :- Its usage is in security related coding. Components that can use this annotation are as : EJB, Web Components.


  • javax.ejb.EJB :- Its usage is in Dependency injection of Session Bean. Components that can use this annotation are as : EJB, Web Components, application client.


  • javax.jws.WebServiceRef :- Its usage is in Dependency injection of Web Services. Components thact can use this annotation are as : EJB, Web Components, application client.


  • javax.persistence.PersistenceContext :- Its usage is in Dependency injection of Container managed EntityManager. Components that can use this annotation are as : EJB, Web Components.


  • javax.persistence.PersistenceUnit :- Its usage is in Dependency injection of EntityManagerFactory. Components that can use this annotation are as : EJB, Web Components.



Here we have listed a few annotations. There are many other annotations on the J2ee plateform which we will be using it in all the future articles of EJB 3.0. Here the sole reason of listing some annotations and giving an example of annotation on Java 5.0 plateform is to explain the functionality of annotations.





iBatis is an object-relational mapping tool (ORM) that simplifies access to database. This article details the steps needed for integrating iBatis with Spring. Through such an integration, objects that are specific to iBatis can utilise all the benefits given by Spring's IOC Container. This is not an introductory article for both Spring and iBatis Frameworks. First-time readers are encouraged to read the Introductory article for Spring in javabeat Introduction to Spring Web Framework to know the preliminary concepts related to Spring.

2) Step-by-Step Procedure for Integration

2.1) Introduction

We are going to create a sample table in the MySql Database and going to access the data within it using Spring-iBatis Integration. The required bundles needed to build and run the sample program are listed below.

  • Spring Distribution
  • MySql Database
  • MySql Database Driver
2.2) Creating tables

Create a table called Jsr which contains relevant information for holding information like name, id, description andspecification lead for a Java Specification Request (JSR). Issue the following command in the MySql Client command prompt to create the table,


create table Jsr (JsrId varchar(10), JsrName varchar(50), JsrDescription, varchar(500), SpecLead varchar(100));


2.3) Creating the Java Equivalent


Now let us create a equivalent Java class for the Jsr table. This class, will contain properties that will map to the column names in the Jsr table. Given here is the complete code listing for the Jsr Java class,



Jsr.java




package javabeat.net.articles.spring.ibatis;

public class Jsr
{
private String id;
private String name;
private String description;
private String specLead;

public String getId()
{
return id;
}

public void setId(String id)
{
this.id = id;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public String getDescription()
{
return description;
}

public void setDescription(String description)
{
this.description = description;
}

public String getSpecLead()
{
return specLead;
}

public void setSpecLead(String specLead)
{
this.specLead = specLead;
}

public String toString()
{
return "Id = " + id + ", Name = " + name +
", Description = " + description + ", Lead = " + specLead;
}
}


2.4) JsrDao Class


Then, we need to get into the client-facing Dao interface design. This is the interface that clients will be depending on, to perform various database operations like selection of rows, insertion, deletion, updating data etc.



JsrDao.java




package javabeat.net.articles.spring.ibatis;

import java.util.List;

public interface JsrDao
{

public List<Jsr> selectAllJsrs();
public Jsr selectJsrById(String jsrID);

public void insertJsr(Jsr insertJsr);
public void deleteJsr(String jsrId);
public void updateJsr(Jsr jsrWithNewValues);

}


2.5) iBatis Mapping File


Jsr.xml




<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap>

<typeAlias type = "javabeat.net.articles.spring.ibatis.Jsr" alias = "jsr"/>

<resultMap class = "jsr" id = "result">
<result property = "id" column = "JsrId"/>
<result property = "name" column = "JsrName"/>
<result property = "description" column = "JsrDescription"/>
<result property = "specLead" column = "SpecLead"/>
</resultMap>

<select id = "selectAllJsrs" resultMap = "result">
select * from Jsr
</select>

<select id = "selectJsrById" resultMap = "result" parameterClass = "string">
select * from Jsr where JsrId = #value#
</select>

<insert id = "insertJsr" parameterClass="jsr">
insert into Jsr (JsrId, JsrName, JsrDescription, SpecLead) values (#id#, #name#, #description#, #specLead#)
</insert>

<delete id = "deleteJsr" parameterClass="string">
delete from Jsr where JsrId = #value#
</delete>

<update id = "updateJsr" parameterClass="jsr">
update Jsr set JsrName = #name#, JsrDescription = #description#, SpecLead = #specLead#
where JsrId = #id#
</update>

</sqlMap>


iBatis mapping file contains the mapping information between a Java class and its corresponding table in the database. Not only does it contain this mapping information, but also it contains many definitions for Named Queries. A Named Query is just a query defined with some name so that it can be reused across multiple modules.



The above Xml file starts with an element called 'typeAlias' which is just a short-name for'javabeat.net.articles.spring.ibatis.Jsr'. Instead of referencing the fully-qualified name of the Jsr class, now it can be shortly referred as 'jsr' in the other sections of the Xml file. Next comes the mapping information specified in the form of 'resultMap' element where the associations between the Java properties for the corresponding column names are made.



Then, the Named Queries section follows. A query called 'selectAllJsrs' has been defined which is actually a select query. The query string value is manifested in the form of 'select * from Jsr'. By having such a query definition, it can be used elsewhere in the Application just by referring the query identifier. Now, let us choose a query definition that illustrates passing parameters to it. The query identifier 'selectJsrById' needs the JsrId as a parameter using which it can filter the number of rows fetched. This can be represented by using the attribute'parameterClass'. Here 'string' stands for java.lang.String which means that the parameter is of type String. Similarly there are values like 'int', 'float', etc for java.lang.Integer and java.lang.Float respectively. Inside the query definition, we have the following query string,




select * from Jsr where JsrId = #value#


In the above query string, we have defined a new symbol called 'value'. This is the default symbol name for the parameter and since we have only one parameter it would not cause any problem. The expression '#value#' will be substituted with the values specified at the run-time. (Later we will see how the value gets substituted to the above expression).



Now, let us see a query definition that accepts multiple parameters. In the query definition 'insertJsr', we want the jsr id, jsr name, jsr description and spec lead values to get inserted and we have defined the query string as follows,




insert into Jsr (JsrId, JsrName, JsrDescription, SpecLead) values (
#id#, #name#, #description#, #specLead#)


In the query definition, the value of the parameter value is pointing to 'jsr', which means that during run-time the query string will get translated as follows,




insert into Jsr (JsrId, JsrName, JsrDescription, SpecLead) values (
jsr.getId(), jsr.getName(),jsr.getDescription(), jsr.getSpecLead())



Technorati Tags: ,



LiveJournal Tags: ,

1) What is Spring?

Spring is a lightweight inversion of control and aspect-oriented container framework.

2) Explain Spring?

  • Lightweight : Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 1MB. And the processing overhead is also very negligible.
  • Inversion of control (IoC) : Loose coupling is achieved in spring using the technique Inversion of Control. The objects give their dependencies instead of creating or looking for dependent objects.
  • Aspect oriented (AOP) : Spring supports Aspect oriented programming and enables cohesive development by separating application business logic from system services.
  • Container : Spring contains and manages the life cycle and configuration of application objects.
  • Framework : Spring provides most of the intra functionality leaving rest of the coding to the developer.

3) What are the different modules in Spring framework?

  • The Core container module
  • Application context module
  • AOP module (Aspect Oriented Programming)
  • JDBC abstraction and DAO module
  • O/R mapping integration module (Object/Relational)
  • Web module
  • MVC framework module

4) What is the structure of Spring framework?


5) What is the Core container module?

This module is provides the fundamental functionality of the spring framework. In this module BeanFactory is the heart of any spring-based application. The entire framework was built on the top of this module. This module makes the Spring container.

6) What is Application context module?

The Application context module makes spring a framework. This module extends the concept of BeanFactory, providing support for internationalization (I18N) messages, application lifecycle events, and validation. This module also supplies many enterprise services such JNDI access, EJB integration, remoting, and scheduling. It also provides support to other framework.

7) What is AOP module?

The AOP module is used for developing aspects for our Spring-enabled application. Much of the support has been provided by the AOP Alliance in order to ensure the interoperability between Spring and other AOP frameworks. This module also introduces metadata programming to Spring. Using Spring’s metadata support, we will be able to addannotations to our source code that instruct Spring on where and how to apply aspects.

8) What is JDBC abstraction and DAO module?

Using this module we can keep up the database code clean and simple, and prevent problems that result from a failure to close database resources. A new layer of meaningful exceptions on top of the error messages given by several database servers is bought in this module. In addition, this module uses Spring’s AOP module to provide transaction management services for objects in a Spring application.

9) What are object/relational mapping integration module?

Spring also supports for using of an object/relational mapping (ORM) tool over straight JDBC by providing the ORM module. Spring provide support to tie into several popular ORM frameworks, including Hibernate, JDO, and iBATIS SQL Maps. Spring’s transaction management supports each of these ORM frameworks as well as JDBC.

10) What is web module?

This module is built on the application context module, providing a context that is appropriate for web-based applications. This module also contains support for several web-oriented tasks such as transparently handling multipart requests for file uploads and programmatic binding of request parameters to your business objects. It also contains integration support with Jakarta Struts.

LiveJournal Tags: ,

Technorati Tags: ,

The goal of the Geronimo project is to produce a server runtime framework that pulls together the best Open Source alternatives to create runtimes that meet the needs of developers and system administrators. Our most popular distribution is a fully certified Java EE 5 application server runtime.

Some of our guiding principles are:

  • Easy to use.
  • Build servers that are distributed under the Apache Software License.
  • Provide runtimes that meet the needs of developers, administrators and system integrators.
  • Integrate with the best open source tooling available like Eclipse.
  • Provide frequent releases of our software so users can experience the newest features and have access to the latest bug fixes.
  • Build a community that incorporates multiple disciplines required to create complex runtime and toolable infrastructure.
IceRocket Tags: ,
+