Spring MVC Project with maven, mongodb, spring-data
This will contain only snippets of code. There are small dependencies that are not listed in these blocks of code.
I am running mongoDB in a remote Centos 6.2 Server.
Main Database is a PostgreSQL. Upper layers in MVC Service classes will try to fetch data from mongoDB. If is not available, will use SQL DB and update mongoDB. My application uses mongo for caching purposes and writes denormalized relationships in mongoDB.
pom.xml
1 2 3 4 5 6 7 8 9 10 11 |
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>${spring-data.mongodb.version}</version> </dependency> |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Bikeshop</display-name> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- MVC SERVLET --> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/pages/index.jsp</welcome-file> </welcome-file-list> </web-app> |
applicationContext.xml
1 2 3 4 5 6 7 |
<mongo:mongo id="mongo" host="${mongo.host}" port="${mongo.port}"/> <mongo:repositories base-package="com.jadebaboon.nucleus.shared.persistence.repository.mongo"/> <bean id="mongoTemplate"> <constructor-arg name="mongo" ref="mongo"/> <constructor-arg name="databaseName" value="${mongo.database}"/> </bean> |
A very simple entity:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
@Document (collection = "brands") public class BrandCache { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "BrandCache [id=" + id + ", name=" + name + "]"; } } |
A ultra simple Dao Repository:
1 2 3 4 |
@Repository public interface MongoBrandCacheRepository extends MongoRepository<BrandCache, Integer> { } |
Service class for Repository (contains JPA code for data transfer from JPA Repository to Mongo Repository, can be ignored):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
@Service public class BrandCacheServiceImpl implements BrandCacheService { @Autowired private MongoBrandCacheRepository mongoBrandCacheRepository; @Autowired private JpaBrandRepository jpaBrandRepository; @Autowired private MongoTemplate mongoTemplate; @Override public List<BrandCache> readAll() throws RecordNotFoundException { List<BrandCache> brandCacheList = mongoBrandCacheRepository.findAll(); if (brandCacheList.size() == 0) throw new RecordNotFoundException("BrandCache Database is empty"); return brandCacheList; } @Override public BrandCache create(BrandCache brandCache) { mongoBrandCacheRepository.save(brandCache); return brandCache; } @Override public void refreshRepository() throws RecordNotFoundException { List<BrandEntity> result = jpaBrandRepository.findAll(); if (result.size() == 0) throw new RecordNotFoundException("Brand Database is empty"); mongoTemplate.dropCollection("brands"); for (BrandEntity brandEntity : result) { System.out.println("INSERTING: " + brandEntity.getId() + " " + brandEntity.getName()); BrandCache brandCache = new BrandCache(); brandCache.setId(brandEntity.getId()); brandCache.setName(brandEntity.getName()); mongoBrandCacheRepository.save(brandCache); } } @Override public BrandCache update(BrandCache brandCache) { //not implemented return null; } @Override public void delete(BrandCache brandCache) { //not implemented } } |
Unit test for this implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
@ContextConfiguration (locations = {"classpath:test-mvc-servlet.xml"}) public class MongoBrandCacheServiceTest extends AbstractTestNGSpringContextTests { @Autowired (required = true) private BrandCacheService brandCacheService; @Test public void readAllTest() { try { List<BrandCache> brandCacheList = brandCacheService.readAll(); for (BrandCache brandCache : brandCacheList) System.out.println(brandCache.getName()); assertNotEquals(brandCacheList.size(), 0, "returned 0 objects"); } catch (Exception ex) { fail(ex.getMessage()); } } @Test public void refreshRepositoryTest() { try { brandCacheService.refreshRepository(); } catch (Exception ex) { fail(ex.getMessage()); } } } |