弹簧服务不在主类中自动装配

时间:2017-11-23 02:04:31

标签: java spring spring-boot spring-data-jpa autowired

这是我的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应用程序运行?

3 个答案:

答案 0 :(得分:1)

我也遇到了同样的问题,@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();
        */      
    }
}

答案 1 :(得分:0)

你正在使用弹簧靴吗? 看起来你的HarvestService类需要@SpringBootApplication并在main函数中添加thid

SpringApplication.run(HarvestService.class, args);

并确保您在maven / gradle中具有正确的依赖关系。 希望它有所帮助

答案 2 :(得分:0)

你必须实现CommandLineRunner并将你的代码放在方法运行中,因为spring需要加载所有组件并且使用普通main来运行它不会起作用

@Override
    public void run(String... args) throws Exception {
        main(args);
    }

SpringApplication.run main method