для начала анотируется подключаемый класс анотацией например
@Component
чтобы твой класс был в связке со спрингом можно запускать его как консольную подрпрограмму для этого имлементируется CommandLineRunner в основном приложении
и получаем метод run вот пример как я внедряю свой класс BashParser и его метод
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import ru.syn.quotes.services.BashParser;
@SpringBootApplication
public class QuotesApplication implements CommandLineRunner {
@Autowired
BashParser parser;
public static void main(String[] args) {
SpringApplication.run(QuotesApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
//используем метод из класса в связке со спрингом
parser.getIndex();
}
}
например как выглядит подключенный метод из класса
package ru.syn.quotes.services;
import org.apache.commons.text.StringEscapeUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class BashParser {
public Map<Integer,String> getIndex() {
Map<Integer, String> quotes = new HashMap<>();
try {
Document doc = Jsoup.connect("http://ibash.org.ru/").get();
log(doc.title());
Elements sourceQuote = doc.select("html body div.quote");
for (Element sQ : sourceQuote) {
int idQ = Integer.parseInt(sQ.select("div.quothead span a b").text().substring(1));
//создаём строку без спец символов т.е. кодируем кавычки и прочее чтобы сохранять в базу данных безопасно
String quote = StringEscapeUtils.escapeHtml4(sQ.select("div.quotbody").text());
log("%s\n\t%s", idQ, quote);
quotes.put(idQ, quote);
}
} catch (
Exception e) {
log("Error: %s", e.getMessage());
}
return quotes;
}
public static void log(String message, Object... args) {
System.out.println(String.format(message, args));
}
}
не забываем про настройки подключения к базе чтобы не словить ошибку
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured
пример подключения
application.properties
spring.application.name=Quotes
spring.datasource.url=jdbc:postgresql://localhost:5432/somedb
spring.datasource.username=someuser
spring.datasource.password=somepass
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.jpa.hibernate.ddl-auto=update