如何将对象列表保存到mongodb?

时间:2016-12-12 21:48:09

标签: java mongodb spring-boot

回购

x_test.sort(axis=0)

Repo Impl

public interface AppPortRepository {

AppPortModel save(AppPortModel appPortModel);
void remove (AppPortModel appPortModel);
List<AppPortModel> findAll();
}

POJO的

@Repository
public class AppPortRepositoryImpl implements AppPortRepository {

    ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");

    MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");

    @Override
    public AppPortModel save(AppPortModel appPortModel) {
        mongoOperation.save(appPortModel);
        return appPortModel;
    }

    @Override
    public void remove(AppPortModel appPortModel) {
        mongoOperation.remove(appPortModel);

    }
    @Override
    public List<AppPortModel> findAll() {
        return mongoOperation.findAll(AppPortModel.class);
    }

}

类SNippet

@Document(collection = "apData")
public class AppPortModel implements Comparable {
    @Id
    private String id;
    private String status;
    private String host;
    private String portName;
    private String plCount;

    public AppPortModel () {}

    public AppPortModel (String status, String host, String portName, String plCount) {
        this.status = status;
        this.host = host;
        this.portName = portName;
        this.plCount = plCount;
    }

    //getters and setters

    @Override
    public int compareTo(Object o) {
        // TODO Auto-generated method stub
        return this.getHost().compareTo(((AppPortModel) o).getHost());
        //return (int) (this.getMemoryUtilization()-(((AppServerCpuDisk)o).getMemoryUtilization()));
    }

     @Override
     public String toString() {
      return "Employee [Status=" + status + ", Host=" + host + ", Port Name="
        + portName + ", Port Listening Count=" + plCount + "]";



     }
}

运行线程的类

 public class ApplicationPort implements Runnable {
    @Autowired
    private AppPortRepositoryImpl appPortRepositoryImpl;

 /*code that gives me values of Status, apServer, n, and line
 ***********************************************
  removed code here to minimize clutter/code overwhelm.
 ***********************************************
   n means port Name, line means Count.  */  

    apData.add(new AppPortModel(Status,apServer,n,line));
    //apData stores all the data into a list

    //I tried to mock the method above to insert these values into MongoDB.
    AppPortModel appPortModel = new AppPortModel(Status,apServer,n,line);
                    appPortRepositoryImpl.save(appPortModel);

我正在尝试将这些变量(PoJO)存储到MONGODB中。目前,这些值存储在List中。 &#34;列表&lt; AppPortModel&GT;&#34 ;.我之间删除了所有代码,因为它只是我如何获取我的值的方法。我遵循了一些教程,这就是他们将数据保存到MONGO DB的方式。我可以将所有数据保存到List中,但我可以将数据保存到Mongo Db

错误消息

 @Component
    public class AppPortList {      

    @Value("#{'${APP_SERVERS_PORT_MONITORING}'.split('@!')}")
    private String[] apServerArray;
    @Value("#{'${APP_SERVER_PORT_MONITORING_PORTS}'.split('@!')}")
    private String[] appServerPortsList;    

    @PostConstruct
    public List<AppPortModel> getAppPortList() {

    final int MYTHREADS = 80;
    ExecutorService executor = Executors.newFixedThreadPool(MYTHREADS);

    //for loop
    //catch  and shut down executor
    return ApplicationPort.getData();

此错误指向&#34; appPortRepositoryImpl.save(appPortModel);&#34;

SpringConfig.xml示例

java.lang.NullPointerException
    at com.spring.web.server.ApplicationPort.run(ApplicationPort.java:134)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

0 个答案:

没有答案