Review
In the previous section, we have implemented the Java classes and organized them in various layers. In this section, we will write the configuration files, which are mainly XML documents.Table of Contents
Part 1: Introduction and Functional SpecsPart 2: MongoDB setup
Part 3: Java classes
Part 4: XML configuration
Part 5: HTML Files (with AJAX)
Part 6: Running the Application
Configuration
To enable MongoDB repository, including template support, we have to declare the following beans:- a Mongo repository
- a Mongo host
- a Mongo template
- Optionally, we declared an InitMongoService to automatically populate our database with sample data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:p="http://www.springframework.org/schema/p" | |
xmlns:c="http://www.springframework.org/schema/c" | |
xmlns:tx="http://www.springframework.org/schema/tx" | |
xmlns:context="http://www.springframework.org/schema/context" | |
xmlns:mongo="http://www.springframework.org/schema/data/mongo" | |
xmlns:util="http://www.springframework.org/schema/util" | |
xsi:schemaLocation=" | |
http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd | |
http://www.springframework.org/schema/tx | |
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd | |
http://www.springframework.org/schema/context | |
http://www.springframework.org/schema/context/spring-context-3.1.xsd | |
http://www.springframework.org/schema/data/mongo | |
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd | |
http://www.springframework.org/schema/util | |
http://www.springframework.org/schema/util/spring-util-3.1.xsd"> | |
<context:property-placeholder properties-ref="deployProperties" /> | |
<!-- Activate Spring Data MongoDB repository support --> | |
<mongo:repositories base-package="org.krams.repository" /> | |
<!-- MongoDB host --> | |
<mongo:mongo host="${mongo.host.name}" port="${mongo.host.port}"/> | |
<!-- Template for performing MongoDB operations --> | |
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate" | |
c:mongo-ref="mongo" c:databaseName="${mongo.db.name}"/> | |
<!-- Service for initializing MongoDB with sample data using MongoTemplate --> | |
<bean id="initMongoService" class="org.krams.service.InitMongoService" init-method="init"/> | |
</beans> |
For MongoDB connection, we've extracted the database properties in a separate file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# database properties | |
mongo.db.name=spring_mongodb_tutorial | |
mongo.host.name=localhost | |
mongo.host.port=27017 |
Finally, here's our applicationContext.xml file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:context="http://www.springframework.org/schema/context" | |
xmlns:p="http://www.springframework.org/schema/p" | |
xmlns:mvc="http://www.springframework.org/schema/mvc" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd | |
http://www.springframework.org/schema/context | |
http://www.springframework.org/schema/context/spring-context-3.1.xsd | |
http://www.springframework.org/schema/mvc | |
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> | |
<context:property-placeholder properties-ref="deployProperties" /> | |
<!-- Activates various annotations to be detected in bean classes --> | |
<context:annotation-config /> | |
<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans. | |
For example @Controller and @Service. Make sure to set the correct base-package --> | |
<context:component-scan base-package="org.krams" /> | |
<!-- Configures the annotation-driven Spring MVC Controller programming model. | |
Note that, with Spring 3.0, this tag works in Servlet MVC only! --> | |
<mvc:annotation-driven /> | |
<mvc:resources mapping="/resources/**" location="/resources/" /> | |
<!-- Imports logging configuration --> | |
<import resource="trace-context.xml"/> | |
<!-- Imports datasource configuration --> | |
<import resource="spring-data.xml"/> | |
<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean" | |
p:location="/WEB-INF/spring.properties" /> | |
</beans> |
Next
In the next section, we will create the HTML and JavaScript files. Click here to proceed.
Share the joy:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |


Great tutorial..i read 3-4 tutorial..same architecture of all helped a lot..
ReplyDeleteI have read your blog its very attractive and impressive. I like it your blog.
ReplyDeleteSpring online training Spring online training Spring Hibernate online training Spring Hibernate online training Java online training
spring training in chennai spring hibernate training in chennai
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletecore java training in Electronic City
Hibernate Training in electronic city
spring training in electronic city
java j2ee training in electronic city
ReplyDeleteTo keep current, you need to find trustworthy technology news sources that will provide you with up-to-date information.
Xfinity
Green Dinosaur Pokemon
pubfilm