简体   繁体   中英

Spring Boot Data console application

I'm gonna create a Java console application for accessesing a database (MySQL). I'm gonna use Spring Boot/Spring Data JPA. What is the correct way to create a console application using Spring Boot?

I found a few ways to do this:

  • spring.main.web-application-type=NONE (in application.properties)
  • spring.main.web-environment = false (in application.properties)
  • using the Spring Shell project
  • implementing the CommandLineRunner interface

I suppose that some of them may be obsolete, have pros and cons. Could you please explain how to create a plain console application using Spring Boot/Spring Data?

Recently, I have done a console application, as you require now. I did that by implementing CommandLineRunner interface. When spring boot starts the application, it will invoke the run(String... args) method of CommandLineRunner interface. So, you can autowire(or using constructor injection) spring data repositories in this implemention class( eg AppRunner) & invoke database operations.

Instead of MySql database, I have you used MongoDB along with some caching operations.

Example:

AppRunner.java

package com.cache.caching;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class AppRunner implements CommandLineRunner {

    Logger logger = LoggerFactory.getLogger(AppRunner.class);

    BookRepository bookRepository;

    public AppRunner(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }

    @Override
    public void run(String... args) throws Exception {
        logger.info("articles fetching..."+bookRepository.getArticles());
        logger.info("articles fetching..."+bookRepository.getArticles());
        logger.info("articles fetching..."+bookRepository.getArticles());
        logger.info("articles fetching..."+bookRepository.getArticles());
    }
}

BookRepository.java

package com.cache.caching;


import java.net.UnknownHostException;
import java.util.List;

public interface BookRepository {
    List<Article> getArticles() throws UnknownHostException;
}

BookRepositoryImpl.java

package com.cache.caching;

import com.mongodb.*;
import org.bson.codecs.pojo.annotations.BsonId;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.MongoCollectionUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@Component
public class BookRepositoryImpl implements BookRepository{

    @Override
    @Cacheable("articles")
    public List<Article> getArticles() throws UnknownHostException {
       
        MongoClient mongoClient
                = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
        DB db = mongoClient.getDB("Mart");
        DBCollection collection =  db.getCollection("articles");
        DBCursor cursor = collection.find();
        List<Article> list= new ArrayList<>();
        while (cursor.hasNext()) {
           Article article = new Article();
           DBObject dbObject = cursor.next();
           article.setId((Double) dbObject.get("_id"));
           article.setSubject((String) dbObject.get("subject"));
           list.add(article);
        }
        return list;
    }
    
}

In your case, you can provide MySQL database connection details here in application.yml / application.properties file.

CachingApplication.java - application starts here, this SpringApplication.run(CachingApplication.class, args); invokes the run(String... args) method of CommandLineRunner interface.

package com.cache.caching;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class CachingApplication {

    public static void main(String[] args) {
        SpringApplication.run(CachingApplication.class, args);
    }

}

Sample: Sample Full Example Here

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