当前位置: 首页 > 知识库问答 >
问题:

NoSuchBeanDefinitionException:未找到使用批注的依赖项类型的限定bean

云捷
2023-03-14

我有这样的项目结构:

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>

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> {
 }

请帮帮我。

共有1个答案

端木望
2023-03-14

看来您需要在JavaConfig中使用@enableJPararePositories

参见第3.2->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[] { "/" };
    }

}
 类似资料:
  • 我的项目使用的是Spring3.2和Struts2(都使用了注释)。我正在通过Java配置Spring。当我尝试将一个服务注入到一个动作中时,我从Struts2得到以下错误: > 找不到依赖项的[com.ceiwc.mvc.service.IUserService]类型的合格bean:至少需要一个合格的bean作为此依赖项的自动候选项。依赖项注释:{@org.SpringFramework.Bea

  • 我正在运行一个JUnit测试,嗯,我的Spring启动项目,我是这样写的: 找到依赖项[com.br.suppcomm.ocp.dao.logindao]得[com.br.suppcomm.ocp.dao.logindao]:需要至少1个具有此依赖项自动候选资格得bean.依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(

  • 我在调用get请求(modes-calcul)时遇到这个错误,我不明白为什么...我的依赖注入是正确的吗? ModeCalculController: 谢谢你的帮助..

  • 尝试部署 Web 应用程序时,由于日志记录错误(如下所示),它将失败: Java . lang . nosuchmethoderror:org . SLF 4j . SPI . locationawarelogger . log(Lorg/SLF 4j/Marker;ljava/lang/String;il Java/lang/String;ljava/lang/Throwable;) 我已经谷歌

  • 我在JBoss上部署我的Java应用程序时遇到了一个愚蠢的问题。在我使用接口类更改源代码之前,一切都很好。所以这是我的问题: ... 在JBoss 7.1上部署时会出现以下错误: 10:05:34838错误[org.jboss.msc.service.fail](msc服务线程1-6)MSC00001:无法启动服务jboss。部署。单元“mdk-exchange-1.1.0.战争”。WeldSer