gpt4 book ai didi

java - Spring 服务未在主类中 Autowiring

转载 作者:行者123 更新时间:2023-12-02 02:29:00 26 4
gpt4 key购买 nike

这是我的 SourceRepository 类,它不会覆盖自动生成的通用 findAll() ,它返回 Iterable

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 变量为空,我看到 Autowiring 不起作用,但我不知道为什么。是因为我通过右键单击包资源管理器中的文件并单击“作为 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();
*/
}
}

关于java - Spring 服务未在主类中 Autowiring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47446504/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com