【问题标题】:Spring Boot Data console applicationSpring Boot Data 控制台应用程序
【发布时间】:2020-09-04 12:43:34
【问题描述】:

我将创建一个用于访问数据库 (MySQL) 的 Java 控制台应用程序。我将使用 Spring Boot/Spring Data JPA。使用 Spring Boot 创建控制台应用程序的正确方法是什么?

我找到了几种方法来做到这一点:

  • spring.main.web-application-type=NONE(在 application.properties 中)
  • spring.main.web-environment = false(在 application.properties 中)
  • 使用 Spring Shell 项目
  • 实现CommandLineRunner 接口

我想其中一些可能已经过时,有利有弊。您能否解释一下如何使用 Spring Boot/Spring Data 创建一个普通的控制台应用程序?

【问题讨论】:

  • 控制台应用程序是什么意思,它在日志中为您提供执行结果?
  • @Rebai Ahmed 我目前有一个普通的 Java 应用程序(public static void main(String[] args) {})。这是一个简单的控制台应用程序,文本用户界面。我需要以某种方式将 Spring Boot/Spring Data 添加到此应用程序中。

标签: java spring spring-boot spring-data console-application


【解决方案1】:

最近,我做了一个控制台应用程序,正如您现在需要的那样。我通过实现CommandLineRunner 接口来做到这一点。当spring boot启动应用程序时,会调用CommandLineRunner接口的run(String... args)方法。 因此,您可以在此实现类(例如 AppRunner)中自动装配(或使用构造函数注入)spring 数据存储库并调用数据库操作。

我让你使用了 MongoDB 以及一些缓存操作,而不是 MySql 数据库。

示例:

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;
    }
    
}

在您的情况下,您可以在 application.yml / application.properties 文件中提供 MySQL 数据库连接详细信息。

CachingApplication.java - 应用程序从这里开始,这个SpringApplication.run(CachingApplication.class, args); 调用CommandLineRunner 接口的run(String... args) 方法。

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 Full Example Here

【讨论】:

    猜你喜欢
    • 2014-06-09
    • 2018-12-06
    • 2015-03-27
    • 1970-01-01
    • 2018-07-28
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多