简体   繁体   中英

Spring Repository

We have some set of json data files stored in the File system. At present I want to load those data files in Spring during application start up.Convert these json files to Pojos and and store it in the application Context.

In future we will have the same json coming from the Mongo Db.So will be loading the Json files from the Mongo Db too and Convert these json files to Pojos and and store it in the application Context.

Then we will be removing the code to load from File System.

So is there any way to abstract this with Spring Repository so that there will be two implementation. One to get it from the File System and the other to get it from the DataBase.

Pointers to some sample example will be useful

Just refer to the repo from interface and create implementations of it.

public class FileRepo implements MyRepo{
    ...
}

public class MongoRepo implements MyRepo{
    ...
}

and use it:

public class MyServiceImpl implements MyService{
    private MyRepo repo;
}


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="repo" class="com.company.repo.FileRepo">//you will change this to MongoRepo 
    </bean>

    <bean id="service" class="com.company.service.MyServiceImpl">
              <property name="repo" value="repo" />
    </bean>

</beans>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM