Application implementation of Spring with MongoDB
The following are the steps are for the implementation of the Spring4MongoDB_Chapter1 application:
- Create a web-based Maven project with the name 
Spring4MongoDB_Chapter1. - Import the project into Eclipse for the implementation. I have used Eclipse Juno.
 
We need to create the controller to map the requests.
The controller request is mapped to the GET and POST methods, as shown in the following table:
Following is the implementation of ProductController.java. We have used the @Controller annotation to indicate that the ProductController.java class is a controller class. The @Autowired annotation ties the ProductRepository class with the ProductController.java file.
The property 
productList is a list of type Product that holds the products that are to be displayed on screen. The @PostConstruct annotation will call the method decorated by it. Once the constructor of the class is called and all properties are set, and before any business methods are called, it's worthy to note as it's only called once.
The Product.java file has an @Document annotation and an @ID annotation, which is identified as a MongoDB collection that maps the Product entity to product collection in MongoDB.
The ProducRepository.java file has @Repository annotation. This is the persistence layer, and tells spring that this class performs operations on the database. The connection to Mongo is set up in Mongo template.
ProductRepository.java
The .jsp file displays the products available and allows the user to perform CRUD operations on the 
Product bean. The following screenshot is the output of editing product information using the product ObjectId stored in MongoDB.
Product.jsp file
This file serves as a view layer to the user. This has the product creation form and includes a file that lists all the products stored in MongoDB.
If all goes well, you should see the following screen, where you can play around with products. The following screenshot is the output of the Register Product and list Product functionality using Spring and MongoDB.
The following dispatcher-servlet.xml file shows the configuration for component scan and MongoDB template. It also shows the MongoDB database name configuration.
dispatcher-servlet.xml
You can see that the mongoDbFactory bean has been configured with MongoDB database details. You will also observe that mongoTemplate has also been configured. The property of the mongoTemplate bean is mongoDbFactory bean, and so when the template is called the connection gets established.
Just run the following commands in the MongoDB database in order to test the Order use case:
db.order.find()db.order.remove()Tip
RoboMongo is a free tool like Toad to access the MongoDB database.