【问题标题】:Spring unable to autowire Mongo RepoSpring无法自动装配Mongo Repo
【发布时间】:2017-05-14 03:23:06
【问题描述】:

我在 Mongo 中使用 Spring。这是我的 mongo 存储库接口,它位于 com.stock.repo 包中。

public interface StockRepository extends PagingAndSortingRepository<Stock,Long> {

}

这是我的 applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mongo="http://www.springframework.org/schema/data/mongo"
       xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <mongo:mongo host="127.0.0.1" port="27017" />
    <mongo:db-factory dbname="testdb" />

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    </bean>

    <mongo:repositories base-package="com.stock.repo"/>
    <context:component-scan base-package="com.kiroule.example.restwebapp" />
</beans>

这是我的嵌入式码头初始化程序

public class ServerInitializer {
    public static void main(String[] args) throws Exception{

        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation("/");

        Server server = new Server(8080);

        ServletContextHandler contextHandler = new ServletContextHandler();
        ServletHolder serHol = contextHandler.addServlet(ServletContainer.class, "/api/*");
        serHol.setInitOrder(1);
        serHol.setInitParameter("jersey.config.server.provider.packages",
                "com.stock.service");
        contextHandler.setContextPath("/");
        contextHandler.addEventListener(new ContextLoaderListener(context));
        contextHandler.setResourceBase(new ClassPathResource("").getURI().toString());
        server.setHandler(contextHandler);


        try {
            server.start();
            server.join();
        } catch (Exception ex) {
            ex.printStackTrace();
            server.stop();
            server.destroy();
        }
    }

这就是我自动装配的地方:

@Path("/broker")
public class BrokerService {

    @Autowired
    StockRepository stockRepository;

    @GET
    @Path("/stocks")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Stock> getAllStocks(){
        List<Stock> stocks = new ArrayList<Stock>();//((int)stockRepository.count());
        stockRepository.findAll().forEach(stocks::add);
        return stocks;
    }

}

但我收到一个错误提示

May 14, 2017 8:33:10 AM org.glassfish.jersey.server.spring.AutowiredInjectResolver getBeanFromSpringContext
WARNING: No beans found. Resolution failed for type interface com.stock.repo.StockRepository.

我是 spring 新手,所以我可能会犯一些基本错误。这里出了什么问题,我该如何解决?

【问题讨论】:

  • 假设你引用了这个example
  • @RajithPemabandu 什么?
  • 我分享了错误的链接。你试过这个example

标签: spring mongodb jersey jetty


【解决方案1】:

尝试注释您的存储库

@Repository
public interface StockRepository extends PagingAndSortingRepository<Stock,Long> {

}

【讨论】:

猜你喜欢
  • 2017-12-18
  • 2016-09-27
  • 2013-01-29
  • 2023-04-08
  • 2012-12-04
  • 2013-11-12
  • 2019-04-29
  • 2016-03-29
  • 2014-09-07
相关资源
最近更新 更多