NoSuchBeanDefinitionException:没有为使用注释的依赖项找到类型的限定bean

时间:2016-03-31 08:10:16

标签: spring hibernate spring-mvc spring-data-jpa entitymanager

我有这样的项目结构:

project structure

的persistence.xml:

 <?xml version="1.0" encoding="UTF-8" ?>
 <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
         version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">

<persistence-unit name="todos" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:jboss/datasources/PostgreSQLDS</jta-data-source>
    <class>server.entity.Holiday</class>
    <properties>
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
    </properties>

</persistence-unit>
</persistence>

Web.xml(如您所见,我正在使用上下文类,我将使用JSON):

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     metadata-complete="false">

<display-name>Spring MVC Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>server.config</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

WebAppConfig.class:

@Configuration
@EnableWebMvc
@ComponentScan("server")
public class WebAppConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(new ObjectMapper());
    converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));

    converters.add(converter);
}


@Bean
public InternalResourceViewResolver setupViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/pages/");
    resolver.setSuffix(".jsp");
    resolver.setViewClass(JstlView.class);

    return resolver;
 }
 }

HolidayController.class

@RestController
@RequestMapping("/holidays")
public class HolidayController {

@Autowired
private HolidayRepository holidayRepository;

@RequestMapping(value = "", method = RequestMethod.GET)
@ResponseBody
public List<Holiday> getAllHolidays() {
    //  List<User> list = userRepository.findAll();
    return holidayRepository.findAll();
}

HolidayRepository.class:

    @Repository 
    public interface HolidayRepository extends JpaRepository<Holiday, Long> {
 }

我有这个错误: org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到[server.repository.HolidayRepository]类型的限定bean:期望至少有1个bean符合此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

请帮帮我。

2 个答案:

答案 0 :(得分:1)

我似乎在你的JavaConfig中需要@EnableJpaRepositories

见第3.2节 - &gt; http://docs.spring.io/spring-data/jpa/docs/1.9.4.RELEASE/reference/html/#repositories.definition

创建一个新的JavaConfig类:

import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration
@EnableJpaRepositories
class JpaSpringDataConfig {}

或者将其添加到您的XML配置中:

<jpa:repositories base-package="server.repository"/>

由于您的日志显示它需要WebApplicationInitializer,请删除您的web.xml并包含类似的类:

public class RestWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { JpaSpringDataConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebAppConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

答案 1 :(得分:0)

将注释@Repository添加到HolidayRepository interface.

的实现中