【问题标题】:How to autowire an instance of implementation of CrudRepository如何自动装配 CrudRepository 的实现实例
【发布时间】:2016-02-15 18:05:42
【问题描述】:

我使用 Spring Boot 构建了一个应用程序。主类是这样的

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class TheApplication {

    public static void main(String[] args) {
        SpringApplication.run(TheApplication.class, args);
    }
}

然后我有一个定义存储库的接口。

package com.application.repositories

@Repository
public interface InstrumentRepository extends CrudRepository<Instrument, String> {

    public List<Instrument> findByPartnerAndUid(Partner partner, String uid);
}

在 REST 控制器中,我定义了类:

@RestController
@RequestMapping("/instrument")
public class InstrumentRestController {

    @Autowired
    private InstrumentRepository instrumentRepository;

    @RequestMapping(value = "/find_all", method = RequestMethod.GET)
    Collection<Instrument> findAllInstrument () {
        return (Collection<Instrument>) this.instrumentRepository.findAll();
    }

    ...
}

从这里开始,如果我运行应用程序并访问http://localhost:8080/instrument/find_all,则会创建 InstrumentRepository 的 bean。


而且,这就是我遇到的问题。 有一个定义 bean 的配置:

<context:annotation-config />
<bean id="service"
        class="com.application.AccountService"></bean>
<jpa:repositories base-package="com.application.repositories"></jpa:repositories>

AccountService 类:

public class AccountService {
    @Autowired
    private InstrumentRepository instrumentRepository;

    Collection<Instrument> getAllAccountInstrument(accountId) {
        this. instrumentRepository.findAllInstrumentByAccountId(accountId);
    }
}

和 REST 控制器:

@RestController
@RequestMapping("/account")
public class AccountRestController {

    @RequestMapping(value = "/{accountId}/find_instrument", method = RequestMethod.GET)
    Collection<Instrument> findAccountInstrument (@PathVariable String accountId) {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("account.xml");
        AccountService service = (AccountService) context.getBean("service");
        service.getAllAccountInstrument(accountId);
        context.close();
    }

    ...
}

但是,当我访问/account/100/find_instrument 时,我得到了错误

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined

编辑

这是我的数据库配置 应用程序.properties

# Database
spring.datasource.url= jdbc:postgresql://localhost:5432/appdb
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=create

hibernate.properties

hbm2ddl.auto = create
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql = true

【问题讨论】:

  • 我编辑了@Abdelhak 的问题。请注意,我的问题不是应用程序中缺少配置。

标签: java spring


【解决方案1】:

您的 Config 类中缺少完整的数据库配置。 试试这个例子:

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.hsqldb.jdbc.JDBCDriver");
dataSource.setUrl("jdbc:hsqldb:mem:testdb");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}

@Bean
public EntityManager entityManager() {
return entityManagerFactory().getObject().createEntityManager();
}

@Bean
 public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
 LocalContainerEntityManagerFactoryBean em = new     LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan("package.where.your.entites.like.CustSys.are.stored");
return em;
 }

根据entitymanagerfactory-is-defined

【讨论】:

  • 好吧,我已经在属性文件中定义了我的数据库配置。我担心的是,在没有定义数据库配置的情况下,似乎 spring 'auto config' 已经管理了它。但是,当使用ClassPathXmlApplicationContext 时,必须定义像上面这样的数据库配置。那么,如何使用 ClassPathXmlApplicationContext 来使用“自动”配置?
【解决方案2】:

如果使用spring boot,必须在application.properties文件中添加数据库配置

例如mysql

spring.datasource.url=jdbc:mysql://localhost/yourdatabaseName
spring.datasource.username=youruserName
spring.datasource.password=yourPassword
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

这里所有的属性 http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties

【讨论】:

  • 是的,我做到了。就像我说的,在第一个“上下文”中,instrumentRepository bean 被创建。
  • @ImportResource("classpath:account.xml") 添加这个注解 AccountRestController 类
  • 嗯.. 使用new ClassPathXmlApplicationContext("account.xml") 有什么区别?好吧,我试过@ImportResource。现在的错误是,“必须存在至少一个 JPA 元模型!”。
  • 我可以只重用全局配置中的entityManagerFactory 吗?就我而言,InstrumentRestController 的存储库已成功连接。 entityManagerFactory 没有明确定义。在使用ClassPathXmlApplicationContext 时,似乎必须加载或定义配置。
  • 我认为不要参考 entityManagerFactory
猜你喜欢
  • 1970-01-01
  • 2018-02-20
  • 1970-01-01
  • 2015-01-10
  • 2017-06-07
  • 2015-11-22
  • 1970-01-01
  • 2017-05-11
  • 1970-01-01
相关资源
最近更新 更多