【发布时间】: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 的问题。请注意,我的问题不是应用程序中缺少配置。