I build an application with 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:
@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.
And, this is the problem I have. There is a config that define beans:
<context:annotation-config />
<bean id="service"
class="com.application.AccountService"></bean>
<jpa:repositories base-package="com.application.repositories"></jpa:repositories>
the AccountService class:
public class AccountService {
@Autowired
private InstrumentRepository instrumentRepository;
Collection<Instrument> getAllAccountInstrument(accountId) {
this. instrumentRepository.findAllInstrumentByAccountId(accountId);
}
}
and the REST controller:
@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
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
Edit
This is my DB configuration 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