简体   繁体   English

如何自动连接CrudRepository的实现实例

[英]How to autowire an instance of implementation of CrudRepository

I build an application with Spring Boot. 我用Spring Boot构建一个应用程序。 The main class looks like this 主类看起来像这样

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class TheApplication {

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

Then I have an interface that defined the repository. 然后,我有一个定义存储库的接口。

package com.application.repositories

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

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

In the REST controller, I defined the class: 在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();
    }

    ...
}

From here, if I run the application and access http://localhost:8080/instrument/find_all the bean for InstrumentRepository is created. 从这里开始,如果我运行该应用程序并访问http://localhost:8080/instrument/find_all ,则会创建InstrumentRepository的bean。


And, this is the problem I have. 而且,这就是我的问题。 There is a config that define beans: 有一个定义bean的配置:

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

the AccountService class: AccountService类:

public class AccountService {
    @Autowired
    private InstrumentRepository instrumentRepository;

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

and the REST controller: 和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();
    }

    ...
}

But, when I access /account/100/find_instrument , I got the error 但是,当我访问/account/100/find_instrument ,出现了错误

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

Edit 编辑

This is my DB configuration application.properties 这是我的数据库配置application.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 hibernate.properties

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

You're missing completly DB configuration in your Config class. 您在Config类中完全缺少数据库配置。 Try this for example: 尝试以下示例:

@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;
 }

According to entitymanagerfactory-is-defined 根据实体管理工厂定义

if you use spring boot, you must add database configurations in application.properties file 如果使用spring boot,则必须在application.properties文件中添加数据库配置

For example mysql 例如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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM