在Spring数据中创建存储库实例

在Spring数据中创建存储库实例,spring,spring-data,Spring,Spring Data,我对Spring数据和Spring都是新手,所以不要对我太苛刻 我找不到实例化存储库的方法。我阅读了文件: 它描述了声明存储库的不同方式(xml、过滤器等),但没有说明如何在代码中获取它的实例 以下是我的配置xml文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3

我对Spring数据和Spring都是新手,所以不要对我太苛刻

我找不到实例化存储库的方法。我阅读了文件:

它描述了声明存储库的不同方式(xml、过滤器等),但没有说明如何在代码中获取它的实例

以下是我的配置xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:solr="http://www.springframework.org/schema/data/solr"
       xsi:schemaLocation="http://www.springframework.org/schema/data/solr http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <solr:repositories base-package="spring" />
    <solr:solr-server id="solrServer" url="http://localhost:8983/solr" />

    <bean id="taskRepo" class="spring.SolrTaskRepository">
    </bean>

    <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
        <constructor-arg index="0" ref="solrServer"/>
    </bean>
</beans>

和SolrTaskRepository:

public interface SolrTaskRepository<T, ID extends Serializable> extends SolrCrudRepository<T, ID> {
    Page<T> findByOrigin(String origin, Pageable page);
}
公共接口SolrTaskRepository扩展了SolrCrudRepository{
PageFindByOrigin(字符串原点,可分页页面);
}

有人能帮我吗?

如果您想在上下文之外的某个地方使用repo(或任何SpringBean):

ApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");

MyRepo obj = (MyRepo) context.getBean("myRepo");
<bean id="someService" class="com.org.core.SomeService">
        <property name="myRepo" ref="myRepo" />
</bean>
 <bean id="myRepo" class="com.org.core.MyRepo">
 </bean>
如果您在spring管理的其他bean(某些服务)中使用repo,您可以自动连接它

@Autowire
private MyRepo myRepo;// + setter
或者将其注入上下文:

ApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");

MyRepo obj = (MyRepo) context.getBean("myRepo");
<bean id="someService" class="com.org.core.SomeService">
        <property name="myRepo" ref="myRepo" />
</bean>
 <bean id="myRepo" class="com.org.core.MyRepo">
 </bean>

对于这两种方式,您都需要在上下文中定义bean:

ApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");

MyRepo obj = (MyRepo) context.getBean("myRepo");
<bean id="someService" class="com.org.core.SomeService">
        <property name="myRepo" ref="myRepo" />
</bean>
 <bean id="myRepo" class="com.org.core.MyRepo">
 </bean>

示例上下文文件:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="myRepo" class="com.org.core.MyRepo">
     </bean>

</beans>


如果使用
ClassPathXmlApplicationContext
加载上下文,则需要类路径中的文件。

如果要在上下文之外的某个地方使用repo(或任何SpringBean):

ApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");

MyRepo obj = (MyRepo) context.getBean("myRepo");
<bean id="someService" class="com.org.core.SomeService">
        <property name="myRepo" ref="myRepo" />
</bean>
 <bean id="myRepo" class="com.org.core.MyRepo">
 </bean>
如果您在spring管理的其他bean(某些服务)中使用repo,您可以自动连接它

@Autowire
private MyRepo myRepo;// + setter
或者将其注入上下文:

ApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");

MyRepo obj = (MyRepo) context.getBean("myRepo");
<bean id="someService" class="com.org.core.SomeService">
        <property name="myRepo" ref="myRepo" />
</bean>
 <bean id="myRepo" class="com.org.core.MyRepo">
 </bean>

对于这两种方式,您都需要在上下文中定义bean:

ApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");

MyRepo obj = (MyRepo) context.getBean("myRepo");
<bean id="someService" class="com.org.core.SomeService">
        <property name="myRepo" ref="myRepo" />
</bean>
 <bean id="myRepo" class="com.org.core.MyRepo">
 </bean>

示例上下文文件:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="myRepo" class="com.org.core.MyRepo">
     </bean>

</beans>


如果您使用
ClassPathXmlApplicationContext
加载上下文,您需要类路径中的文件。

我可以在JavaSE中使用它还是必须使用EE?谢谢,您还可以给出applicationContext.xml文件的示例吗?我更新了答案。还可以看一下(Spring选项卡下的教程)。我还更新了我的问题。我几乎明白了,但现在我得到了一个运行时异常,说
无法实例化bean类[spring.SolrTaskRepository]:指定的类是一个接口
。这是正确的,因为它是一个接口,指定了Spring应该为我生成的其他方法。你知道我怎么说Spring从指定的接口生成实例吗?接口不能被实例化。您可以实例化实现接口的类。我可以在JavaSE中使用它吗?还是必须使用EE?谢谢,您还可以给出applicationContext.xml文件的示例吗?我更新了答案。还可以看一下(Spring选项卡下的教程)。我还更新了我的问题。我几乎明白了,但现在我得到了一个运行时异常,说
无法实例化bean类[spring.SolrTaskRepository]:指定的类是一个接口
。这是正确的,因为它是一个接口,指定了Spring应该为我生成的其他方法。你知道我怎么说Spring从指定的接口生成实例吗?接口不能被实例化。可以实例化实现接口的类。