当遇到Spring数据solr和Spring mvc时,我遇到了问题。如果只使用spring数据solr,一切运行正常,但是为了集成spring mvc我已经看到了关于配置Spring mvc的问题。
存储库类:
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
public class ManufacturerServiceImpl implements ManufacturerService {
@Autowired
private ManufacturerRepository manufacturerRepository;
public List<Manufacturer> getAllManufacturer() {
return manufacturerRepository.getAllManufacturer();
}
}
控制器类:(更新)
@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>
记录消息:(更新)
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 :
似乎缺少servlet上下文或任何文件,我没有经验来整合Spring数据solr和Spring mvc。如何解决问题?非常感谢!
答案 0 :(得分:0)
您的存储库未连接到Controller。
@Controller
public class ManagementManufacturerController {
@Autowired
private ManufacturerService manufactureService;
----
应该做的伎俩。你已经用SessionFactory做了这个。 Autowired告诉Spring它应该注入一个匹配类型和/或名称的Bean。
€: 您的服务类未实例化。很难说为什么,但很可能你错过了一些@ComponentScan注释或之前有错误。
由于服务只是从存储库中包装该方法,因此尝试直接自动装配存储库而不是服务类。