春天无法自动装载Mongo Repo

时间:2017-05-14 03:23:07

标签: spring mongodb jersey jetty

我正在使用带有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.

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

1 个答案:

答案 0 :(得分:0)

尝试注释您的存储库

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

}
相关问题