Reputation: 131
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
Upvotes: 1
Views: 537
Reputation: 1185
if you use spring boot, you must add database configurations in application.properties file
For example 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
Upvotes: 1
Reputation: 8387
You're missing completly DB configuration in your Config class. 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
Upvotes: 1