Daniel
Daniel

Reputation: 315

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:

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?

Upvotes: 2

Views: 1980

Answers (1)

Dinesh Dontha
Dinesh Dontha

Reputation: 579

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(e.g. 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

Upvotes: 1

Related Questions