我用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
修改
这是我的数据库配置 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
hbm2ddl.auto = create
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql = true
答案 0 :(得分: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;
}
答案 1 :(得分:1)
如果使用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