0

I have a SpringBoot application which has hibernate integrated to it. I have wired in hibernate properties using Spring. When I use the sessionFactory in a Junit test I can do all the CRUD Operations but when I run the SpringBootApplication i.e., when I run the Main class which has @SpringBootApplication in it; it ends up getting an

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} exception.

Any idea on how to solve this?

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class GreetingRepositoryDAOTest {

    @Resource
    private BaseRepository greetingRepositoryImpl;

    @Test
    public void test() {
        System.out.println(((Greeting) greetingRepositoryImpl.findOne(1L)).getText());
        Greeting gr = new Greeting();
        gr.setText("Third Text");
        greetingRepositoryImpl.save(gr);
    }
}

@RestController
@SpringBootApplication
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

}

applicationContext

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context.xsd">
    <import resource="hibernateContext.xml" />
    <context:component-scan base-package="com.deepaksp.springboot" />
    <context:property-placeholder location="classpath:application.properties" />
</beans>

hibernateContext

DAO Class

@Repository
public class BaseDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public Criteria createCriteria(Class<?> clazz, String alias) {
        return currentSession().createCriteria(clazz, alias);
    }

    public Session currentSession() {
        return sessionFactory.getCurrentSession();
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}
  • Could you paste your code? Are you trying to access bean in static context? – staszek Nov 07 '16 at 19:49
  • Give us the code of applicationContext.xml... – Mechkov Nov 07 '16 at 19:51
  • @staszek I am running the static main method which will use sessionFactory to create entities in database. –  Nov 07 '16 at 19:52
  • For using beans in static context you can use solutions from http://stackoverflow.com/questions/1018797/autowired-static-field-spring-2-5 – staszek Nov 07 '16 at 20:00
  • @staszek But I am not using beans in static context. I am just running the springboot application through a static main method which loads data to the DB and it doesn't use beans in static context anywhere. –  Nov 07 '16 at 20:06

1 Answers1

0

Spring Boot by default does not load beans applicationContext.xml. You can do it by annotating your Application class additionally with:

@ImportResource("classpath:applicationContext.xml")

This will add BaseDAO to Spring Boot application context, but it looks that you have more problems in your code:

  • there is no need for creating PropertyPlaceholder for application.properties - it's being loaded by Spring Boot by default
  • have a look at Spring Data JPA and how does it work with Spring Boot - it may simplify your Hibernate configuration (I see you have another hibernateContext.xml that most likely is not needed).
  • consider dropping xml beans configuration in favour of @Configuration classes.
Maciej Walkowiak
  • 12,372
  • 59
  • 63
  • Thank you it worked with the @ImportResource. I will look into Spring Data JPA as well. I will need the propertyPlaceholder for application.properties for Junit tests, they won't work with out that. –  Nov 08 '16 at 07:59
  • 1
    If you use Spring Boot 1.4 instead of `@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:applicationContext.xml" })`use `@RunWith(SpringRunner.class) @SpringBootTest` and `application.properties` will be loaded automagically. – Maciej Walkowiak Nov 08 '16 at 08:01