Showing posts with label mongodb. Show all posts
Showing posts with label mongodb. Show all posts

Saturday, January 24, 2015

Depedencies Injection In Play-Framework 2.2.x Version Using Spring

Introduction:

Hello Friends, today we discuss about "How we use Dependencies Injection With Play-Framework Using JAVA". There are so many languages who have its own framework for developing web application easily and effectively like PHP have Cake-PHP, Zend etc or Python have its DJango etc. These web framework build a good web application easily and quickly, because the major thing is that there is not need to configure application manually, these frameworks are help the developers to create a basic web application struture with couple of seconds and easily customizable. These are Open-Source framework. But still java had not any open-source web framework for build reliable and scalable web-application. 

Explanation:

Now the day's Typesafe launches a Play-Framework for build a "Reactive" application using Java or Scala. Play 1.x build using Java but 2.x build with Scala, The Scala is a hybrid lanugage which have so features and Play uses these features for better performance. For more information about Play click on this link.
Now the day's Reactive Programming is the part of developement. Play is a reactive and follow reacive principles as below: 
  • Responsive
  • Resilient
  • Elastic
  • Message Driven
For basics understanding of reactive go to Reactive Manifesto

Play-Framework have so many features for build a reliable, scalable web applications easily, But the main limitation i feel in the Play-Framework is, the Play does not have its own dependency injection management. Most of the Java developers are habitual with Dependencies Injection because using new in our code is a bad practices. Play-Framework is a scalable, we easily scale our web application with play framework and for dependencies injection we are using third party plug-ins for Dependency Management like Spring, Google Guice etc.

We are trying to integrate Spring Dependency Injection with Play-Framework as below. Our requirements as below:                                                       


  • Download Play Framework
  • Install Java 1.6 or above
  • Eclipse IDE or other Rich Editor

Step 1:

Download the project from Github . Still Play have support for IDE, but some of the Java web developers found the different way for deploy application with play framework. Because for spring we are using tomcat and integrate tomcat with IDE easily and run the application. Play have its own server and we are using command line for run the server and create the application. With IDE we are write our code with rich environment and also debug the application. But still there is dependency of command line. 

NOTE: We are using Play-Framework 2.2 version and we hope in Play-Framework 3.x have its own dependency injection management module.

Step 2:

Download spring depedencies as below: 

 "org.springframework" % "spring-context" % "4.1.3.RELEASE"

NOTE: Play using Scala-Built Tool for build the application and dependencies management. 

Step 3:

For add spring dependencies configuration, we need to create a Global class at the root package and Inherit GlobalSettings class for override some default configurations settings for application. The Global class define some global configurations for application.

NOTE: One application have one Global Class. If we change the class name Global class to another name or change the Package of Global class, the application pick its default configuration because application search global settings class with Global name and on root package.



public class Global extends GlobalSettings{

	private Logger logger = null;	
	private final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
	
	public Global() {
		logger = Logger.getLogger("GlobalConfiguration");
		logger.setLevel(Level.ALL);
	}
	
	@Override 
	public void onStart(Application app) {
		super.onStart(app);
		
		logger.info("ON START");
		applicationContext.scan("com.harmeetsingh13");
		applicationContext.refresh();
		
		// This will construct the beans and call any construction lifecycle methods e.g. @PostConstruct
		applicationContext.start();
	}
	
	@Override
	public void onStop(Application app) {
		applicationContext.close();
		
		super.onStop(app);
	}
	
	@Override
	public  A getControllerInstance(Class clazz) throws Exception {
		logger.info("getControllerInstance");
		return applicationContext.getBean(clazz);
	}
}

Saturday, August 17, 2013

Perform CRUD Operation's MongoDB with Java.


MongoDB :

Data in MongoDB has a flexible schemaCollections do not enforce document structure. This means that:

  • documents in the same collection do not need to have the same set of fields or structure, and
  • common fields in a collection’s documents may hold different types of data.

Each document only needs to contain relevant fields to the entity or object that the document represents. In practice, most documents in a collection share a similar structure. Schema flexibility means that you can model your documents in MongoDB so that they can closely resemble and reflect application-level objects. For more information Click on that link.

Requirements : 

  1. Download MongoDB from here.
  2. Install MongoDB and set the PATH variable . 
  3. Install JDK 7 (The following code is Tested On JDK 7.

Note: Before Running the code, Need to start MongoDB server .


Following is the CODE to perform CRUD operation's of MongoDB with JAVA.

import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Date;

import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.QueryBuilder;
import com.mongodb.ServerAddress;

public class MongoDBCRUDOperationInJava {
 public static void main(String[] args) throws UnknownHostException {
  MongoClient client = new MongoClient(new ServerAddress("localhost", 27017));
  DB courseDB = client.getDB("students");
  DBCollection collection = courseDB.getCollection("grades");
  //collection.drop();
  //insertDocument(collection);
  //findOne(collection);
  //findAll(collection);
  //count(collection);
  //findWithCriteriaUsingDBObject(collection);
  findWithCriteriaUsingQueryBuilder(collection);
}

public static void insertDocument(DBCollection collection) {
 DBObject doc = new BasicDBObject("_id", new ObjectId());
 doc.put("name", "Harmeet");
 doc.put("age", 24);
 doc.put("birthday", new Date(12312312313l));
 doc.put("programmer", true);
 doc.put("language", Arrays.asList("Java", "Scala", "Python"));
 doc.put("address", new BasicDBObject("street", "5").append("area", "moga"));
 DBObject doc2 = new BasicDBObject().append("name", "Jimmy");
System.out.println("Before Inserting the Document in Collection : "+doc);
 collection.insert(Arrays.asList(doc, doc2));
 System.out.println("After Inserting the Document in Collection : "+doc);
}
public static void findOne(DBCollection collection) {
 DBObject doc = collection.findOne();
 System.out.println(doc);
}
public static void findAll(DBCollection collection) {
 DBCursor cur = collection.find();
 try{
   while(cur.hasNext()){
   DBObject doc = cur.next();
   System.out.println(doc);
  }
 }finally{
 cur.close();
 }
}
public static void count(DBCollection collection) {
 System.out.println("COunt of collection Documents : "+collection.count());
}
public static void findWithCriteriaUsingDBObject(DBCollection collection) {
 DBObject criteria = new BasicDBObject("type", "homework").append("student_id", 9);
 DBCursor cur = collection.find(criteria);
 try{
   while(cur.hasNext()){
   DBObject doc = cur.next();
   System.out.println(doc);
  }
 }finally{
  cur.close();
 }
}
public static void findWithCriteriaUsingQueryBuilder(DBCollection collection) {
 QueryBuilder builer =  QueryBuilder.start("type").is("homework").and("student_id").is(9).and("score").greaterThan(60);
 DBCursor cur = collection.find(builer.get(), new  BasicDBObject("student_id" , true).append("_id", false));
 try{
   while(cur.hasNext()){
   DBObject doc = cur.next();
   System.out.println(doc);
  }
 }finally{
  cur.close();
 }
}
}

Download the CODE from here.