1

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
Morilla Thaisa
  • 121
  • 1
  • 1
  • 10

2 Answers2

1

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

Community
  • 1
  • 1
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
  • Well, I have defined my DB config in a property files. My concern is, without defining a DB configuration, it seems the spring 'auto config' has managed it. But, when using `ClassPathXmlApplicationContext`, the DB config like yours above must be defined. So, how to use that 'auto' config using `ClassPathXmlApplicationContext`? – Morilla Thaisa Feb 15 '16 at 23:19
1

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

kakashi hatake
  • 1,175
  • 2
  • 10
  • 18
  • Yeah, I did that. Like I said, in the first 'context', the `instrumentRepository` bean is created. – Morilla Thaisa Feb 15 '16 at 23:11
  • @ImportResource("classpath:account.xml") add this annotation AccountRestController class – kakashi hatake Feb 16 '16 at 00:36
  • Hmm.. What the difference with using `new ClassPathXmlApplicationContext("account.xml")`? And well, I've tried `@ImportResource`. The error now is, "At least one JPA metamodel must be present!". – Morilla Thaisa Feb 16 '16 at 01:01
  • Can I just reuse `entityManagerFactory` from the global configuration? In my case, the repository at `InstrumentRestController` is wired successfully. The `entityManagerFactory ` is not defined explicitly. While using `ClassPathXmlApplicationContext`, it seems the configuration must be loaded or defined. – Morilla Thaisa Feb 16 '16 at 01:08
  • I think not refer entityManagerFactory – kakashi hatake Feb 16 '16 at 08:50