0

I'm facing the problem when intergrate Spring data solr and Spring mvc. If just use spring data solr, everything are run fine, but to intergrate spring mvc I have seen the problem about configure of Spring mvc.

Repository class:

public class ManufacturerRepositoryImpl implements ManufacturerRepository{

private Logger logger = Logger.getLogger(ManufacturerRepositoryImpl.class);

@Autowired
private SessionFactory factory;

@SuppressWarnings("unchecked")
public List<Manufacturer> getAllManufacturer() {        
    Query query = factory.getCurrentSession().createQuery("SELECT m FROM Manufacturer m");
    return query.list();        
}
}

Service class:

@Service    
public class ManufacturerServiceImpl implements ManufacturerService {

@Autowired
private ManufacturerRepository manufacturerRepository;

public List<Manufacturer> getAllManufacturer() {
    return manufacturerRepository.getAllManufacturer();
}
}

Controller class: (UPDATED)

@Controller
public class ManagementManufacturerController {
@Autowired     **// UPDATED**
private ManufacturerService manufactureService;

@RequestMapping(value="/manageManufacturer")
public String getAllManufacturers(HttpServletRequest request, 
                                          HttpServletResponse response,
                                          ModelMap model) {

    List<Manufacturer> manuList = manufactureService.getAllManufacturer();
    model.put("manuList", manuList);

    return "manageManufacturer";
}
}

JSP:

<c:forEach var="renter" items="${manuList}">
    <c:out value="${renter.id}" />
    <c:out value="${renter.name}" />
</c:forEach> --%>

Pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.0.1.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- SOLR -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-solr</artifactId>
        <version>${spring-data-solr.verion}</version>
    </dependency>

    <!-- JSON -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.11</version>
    </dependency>

    <!-- WEB -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Hibernate 4 dependencies -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-commons -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons</artifactId>
        <version>1.12.5.RELEASE</version>
    </dependency>               
</dependencies>

Log message: (UPDATED)

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'managementManufacturerController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.totoroads.service.ManufacturerService com.totoroads.data.solr.showcase.product.web.ManagementManufacturerController.manufactureService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.totoroads.service.ManufacturerService] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:648)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:909)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:898)
at com.totoroads.data.solr.showcase.Application.main(Application.java:40)
  Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.totoroads.service.ManufacturerService com.totoroads.data.solr.showcase.product.web.ManagementManufacturerController.manufactureService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.totoroads.service.ManufacturerService] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 16 more
   Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.totoroads.service.ManufacturerService] 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)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 18 more
   2017-03-08 19:17:18.198  INFO 4716 --- [           main] utoConfigurationReportLoggingInitializer : 

It's seem lack servlet context or any file, and I don't have experienced to intergrate Spring data solr and Spring mvc. How to fix the problem ? Thank so much !

Khanh Luong Van
  • 476
  • 3
  • 14
  • 31

1 Answers1

0

Your Repository is not wired to the Controller.

@Controller
public class ManagementManufacturerController {

  @Autowired
  private ManufacturerService manufactureService;
----

Should do the trick. You already did this with the SessionFactory. Autowired tells Spring it should inject a Bean which matches type and/or name.

€: Your service class is not instantiated. Difficult to say why but most likely you are missing some @ComponentScan annotation or there is an error before.

As the service just wraps the method from the repository try to directly autowire the repository instead the service class.

Dennis Ich
  • 3,585
  • 6
  • 27
  • 44
  • Hi @Dennis Ich, thanks for your reply, I have updated Annotation autowired, but it get an exception as above – Khanh Luong Van Mar 08 '17 at 12:23
  • 1
    If you edit your original Question just add to it please and don't edit other stuff as the information could be usefull for others that might come here with similar problems :). As for the other see my edit. – Dennis Ich Mar 08 '17 at 12:29
  • Yes I see, this is my mistake for not clear of question, but your solution is good for me, I will try it or create new question... thank @Dennis Ich – Khanh Luong Van Mar 08 '17 at 12:36