[英]spring service is not autowired in main class
這是我的SourceRepository類,它不會覆蓋自動返回的Iterable的常規通用findAll()。
package com.infostream.repositories;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.infostream.models.Source;
public interface SourceRepositoryImpl extends PagingAndSortingRepository<Source, Long>{
Page<Source> findAll(Pageable pageRequest);
}
這是我的服務班級:
package com.infostream.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import com.infostream.models.Source;
import com.infostream.repositories.SourceRepositoryImpl;
@Component
public class SourcesService {
@Autowired
private SourceRepositoryImpl sourceRepository;
public PageImpl<Source> getPaginatedSources(Pageable pageRequest) {
Page<Source> searchResultPage = sourceRepository.findAll(pageRequest);
return new PageImpl<Source>(searchResultPage.getContent(), pageRequest, searchResultPage.getTotalElements());
}
public Iterable<Source> getAllSources() {
return sourceRepository.findAll();
}
}
這是我作為Java應用程序運行的主類。
package com.infostream.services;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.infostream.consumers.RssArticleConsumer;
import com.infostream.models.Article;
import com.infostream.models.Source;
import com.infostream.producers.RssXmlProducer;
public class HarvestService {
private static BlockingQueue<Article> article_queue = new ArrayBlockingQueue<Article>(10);
@Autowired
private static SourcesService sourcesService;
public static void main(String[] args) throws InterruptedException {
Iterable<Source> sources = sourcesService.getAllSources();
/*
for(Source s : sources) {
System.out.println(s.getUrl());
}
Thread t1 = new Thread(new RssXmlProducer(sources.iterator().next(), article_queue));
Thread t2 = new Thread(new RssArticleConsumer(article_queue));
t1.start();
t2.start();
t1.join();
t2.join();
*/
}
}
sourcesService變量為null,我看到自動裝配不起作用,但我不知道為什么。 是因為我通過右鍵單擊包資源管理器中的文件並單擊“以Java應用程序運行”來將HarvestService文件作為Java應用程序運行嗎?
我也遇到了同樣的問題,@ @Autowired
在主類中不起作用我所做的是獲取對ApplicationContext
的引用,然后使用它來獲取sourcesService
作為bean
重寫了您的課程,如下所示
package com.infostream.services;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// adedd import file
import org.springframework.context.ApplicationContext;
import com.infostream.consumers.RssArticleConsumer;
import com.infostream.models.Article;
import com.infostream.models.Source;
import com.infostream.producers.RssXmlProducer;
@SpringBootApplication // added this here
public class HarvestService
{
private static BlockingQueue<Article> article_queue = new ArrayBlockingQueue<Article>(10);
@Autowired
private static SourcesService sourcesService;
ApplicationContext context; // added this here
public static void main(String[] args) throws InterruptedException {
// added this - get reference to application context
context = SpringApplication.run(HarvestService.class, args);
// added this - get the object via the context as a bean
sourcesService = (SourcesService) context.getBean("sourcesService");
Iterable<Source> sources = sourcesService.getAllSources();
/*
for(Source s : sources) {
System.out.println(s.getUrl());
}
Thread t1 = new Thread(new RssXmlProducer(sources.iterator().next(),article_queue));
Thread t2 = new Thread(new RssArticleConsumer(article_queue));
t1.start();
t2.start();
t1.join();
t2.join();
*/
}
}
您正在使用彈簧靴嗎? 看起來您的HarvestService類需要@SpringBootApplication
並在主函數中添加@SpringBootApplication
SpringApplication.run(HarvestService.class, args);
並確保您在maven / gradle中具有正確的依賴性。 希望能幫助到你
您必須實現CommandLineRunner並將代碼放入方法運行中,因為spring需要加載所有組件,並且使用常規main不能正常工作
@Override
public void run(String... args) throws Exception {
main(args);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.