Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring要求至少有一个bean符合此依赖项的autowire候选项的条件_Java_Spring_Hibernate - Fatal编程技术网

Java Spring要求至少有一个bean符合此依赖项的autowire候选项的条件

Java Spring要求至少有一个bean符合此依赖项的autowire候选项的条件,java,spring,hibernate,Java,Spring,Hibernate,我正在使用Spring和Hibernate,并试图“连接”必要的类,以便在服务中自动连接存储库 Repository类扩展了Crudepository 停止存储库 @Repository @RepositoryRestResource(collectionResourceRel = "stop", path = "stop") public interface StopRepository extends CrudRepository<StopJPA, Long> { St

我正在使用Spring和Hibernate,并试图“连接”必要的类,以便在服务中自动连接存储库

Repository类扩展了Crudepository

停止存储库

@Repository
@RepositoryRestResource(collectionResourceRel = "stop", path = "stop")
public interface StopRepository extends CrudRepository<StopJPA, Long> {

    StopJPA findById(@Param("id") Long id);

    StopJPA findByIdStop(@Param("idStop") String idStop);

    @Override
    void delete(StopJPA deleted);

    @Override
    List<StopJPA> findAll();

    // Optional<StopJPA> findOne(Long id);
    @Override
    StopJPA findOne(Long id);

    @Override
    StopJPA save(StopJPA persisted);

    void flush();

}
@Entity
@Table(name = "stop")
@EntityListeners(RepoListener.class)
public class StopJPA implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Column(name = "stop_description")
    private String stopDescription;

    @Column(name = "id_stop", nullable = false)
    private String idStop;

    public StopJPA() {
    }

    public StopJPA(String stopDescription, String idStop) {
        this.stopDescription = stopDescription;
        this.idStop = idStop;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getStopDescription() {
        return stopDescription;
    }

    public void setStopDescription(String stopDescription) {
        this.stopDescription = stopDescription;
    }

    public String getIdStop() {
        return idStop;
    }

    public void setIdStop(String idStop) {
        this.idStop = idStop;
    }

}
@EnableWebMvc
@Configuration
//@EnableJpaRepositories
@ComponentScan(basePackages = { "com.project.app" })

public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private Environment env;

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

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("hello");
    }

    @Bean
    public ApplicationContextProvider applicationContextProvider() {
        return new ApplicationContextProvider();
    }
}
以及服务类实现:

停止服务

@Service
final class RepoStopService {

@Service
final class RepoStopService {

    private final StopRepository stopRepository;

    @Autowired
    RepoStopService(StopRepository stopRepository) {
        this.stopRepository = stopRepository;
    } 
}
不幸的是,当我尝试在服务器上运行它时,出现以下异常:

严重:向侦听器发送上下文初始化事件时发生异常 类的实例
org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.unsatifiedDependencyException: 创建文件中定义了名为“repoStopService”的bean时出错 …\RepoStopService.class:
通过具有的构造函数参数表示的未满足的依赖项 类型的索引0 [com.project.app.services.repositories.StopRepository]::否 类型的限定bean
找到的[com.project.app.services.repositories.StopRepository] 依赖项:至少需要1个符合autowire条件的bean 此依赖项的候选项。依赖项注释:{}

@Repository
@RepositoryRestResource(collectionResourceRel = "stop", path = "stop")
public interface StopRepository extends CrudRepository<StopJPA, Long> {

    StopJPA findById(@Param("id") Long id);

    StopJPA findByIdStop(@Param("idStop") String idStop);

    @Override
    void delete(StopJPA deleted);

    @Override
    List<StopJPA> findAll();

    // Optional<StopJPA> findOne(Long id);
    @Override
    StopJPA findOne(Long id);

    @Override
    StopJPA save(StopJPA persisted);

    void flush();

}
@Entity
@Table(name = "stop")
@EntityListeners(RepoListener.class)
public class StopJPA implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Column(name = "stop_description")
    private String stopDescription;

    @Column(name = "id_stop", nullable = false)
    private String idStop;

    public StopJPA() {
    }

    public StopJPA(String stopDescription, String idStop) {
        this.stopDescription = stopDescription;
        this.idStop = idStop;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getStopDescription() {
        return stopDescription;
    }

    public void setStopDescription(String stopDescription) {
        this.stopDescription = stopDescription;
    }

    public String getIdStop() {
        return idStop;
    }

    public void setIdStop(String idStop) {
        this.idStop = idStop;
    }

}
@EnableWebMvc
@Configuration
//@EnableJpaRepositories
@ComponentScan(basePackages = { "com.project.app" })

public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private Environment env;

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

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("hello");
    }

    @Bean
    public ApplicationContextProvider applicationContextProvider() {
        return new ApplicationContextProvider();
    }
}
嵌套异常是 org.springframework.beans.factory.noSuchBean定义异常:否 类型的限定bean 找到的[com.project.app.services.repositories.StopRepository] 依赖项:至少需要1个符合autowire条件的bean 此依赖项的候选项。依赖项批注:{}at

有人知道为什么Spring没有选择@Repository注释吗


我的配置包含3个文件。 一个实现WebApplicationInitializer的AppInitializer类,一个扩展WebMVCConfigureAdapter的WebMvcConfig类,最后是一个PersistentContext

AppInitializer

public class AppInitializer implements WebApplicationInitializer {

    private static final String CONFIG_LOCATION = "com.project.app.config";
    private static final String MAPPING_URL = "/";

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        // Create the 'root' Spring application context
        WebApplicationContext context = getContext();

        // Manage the lifecycle of the root application context
        servletContext.addListener(new ContextLoaderListener(context));

        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet",
                new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping(MAPPING_URL);

    }

    private AnnotationConfigWebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation(CONFIG_LOCATION);
        return context;
    }
WebMvcConfig

@Repository
@RepositoryRestResource(collectionResourceRel = "stop", path = "stop")
public interface StopRepository extends CrudRepository<StopJPA, Long> {

    StopJPA findById(@Param("id") Long id);

    StopJPA findByIdStop(@Param("idStop") String idStop);

    @Override
    void delete(StopJPA deleted);

    @Override
    List<StopJPA> findAll();

    // Optional<StopJPA> findOne(Long id);
    @Override
    StopJPA findOne(Long id);

    @Override
    StopJPA save(StopJPA persisted);

    void flush();

}
@Entity
@Table(name = "stop")
@EntityListeners(RepoListener.class)
public class StopJPA implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Column(name = "stop_description")
    private String stopDescription;

    @Column(name = "id_stop", nullable = false)
    private String idStop;

    public StopJPA() {
    }

    public StopJPA(String stopDescription, String idStop) {
        this.stopDescription = stopDescription;
        this.idStop = idStop;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getStopDescription() {
        return stopDescription;
    }

    public void setStopDescription(String stopDescription) {
        this.stopDescription = stopDescription;
    }

    public String getIdStop() {
        return idStop;
    }

    public void setIdStop(String idStop) {
        this.idStop = idStop;
    }

}
@EnableWebMvc
@Configuration
//@EnableJpaRepositories
@ComponentScan(basePackages = { "com.project.app" })

public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private Environment env;

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

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("hello");
    }

    @Bean
    public ApplicationContextProvider applicationContextProvider() {
        return new ApplicationContextProvider();
    }
}
PersistentContext

@Component
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
public class PersistenceContext {

    @Autowired
    private Environment env;

    @Bean
    @Primary
    public DataSource dataSource() throws ClassNotFoundException {
        DataSource ds = new DataSource();

        String url = env.getProperty(SystemSettings.AMTAB_DS_URL);
        String user = env.getProperty(SystemSettings.AMTAB_DS_USERNAME);
        String pass = env.getProperty(SystemSettings.AMTAB_DS_PASSWORD);
        String driver = env.getProperty(SystemSettings.AMTAB_DS_DRIVER);

        // ds.setDriverClassName("org.postgresql.Driver");
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(user);
        ds.setPassword(pass);

        return ds;
    }

    @Bean
    LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        entityManagerFactoryBean.setPackagesToScan("com.project.app.services.entities");

        Properties jpaProperties = new Properties();

        // Configures the used database dialect. This allows Hibernate to create SQL
        // that is optimized for the used database.
        jpaProperties.put("hibernate.dialect",
                env.getRequiredProperty(SystemSettings.HIBERNATE_DIALECT));

        // Specifies the action that is invoked to the database when the Hibernate
        // SessionFactory is created or closed.
        jpaProperties.put("hibernate.hbm2ddl.auto",
                env.getRequiredProperty(SystemSettings.HIBERNATE_HBM2DDL));

        // Configures the naming strategy that is used when Hibernate creates
        // new database objects and schema elements
        // jpaProperties.put("hibernate.ejb.naming_strategy",
        // env.getRequiredProperty(SystemSettings.HIBERNATE_NAMING_STRATEGY));

        // If the value of this property is true, Hibernate writes all SQL
        // statements to the console.
        jpaProperties.put("hibernate.show_sql",
                env.getRequiredProperty(SystemSettings.HIBERNATE_SHOW_SQL));

        // If the value of this property is true, Hibernate will format the SQL
        // that is written to the console.
        jpaProperties.put("hibernate.format_sql",
                env.getRequiredProperty(SystemSettings.HIBERNATE_FORMAT_SQL));

        entityManagerFactoryBean.setJpaProperties(jpaProperties);

        return entityManagerFactoryBean;
    }

    /**
     * Because we are using JPA, we have to create a transaction manager bean that integrates the
     * JPA provider with the Spring transaction mechanism. We can do this by using the
     * JpaTransactionManager class as the transaction manager of our application.
     *
     * We can configure the transaction manager bean by following these steps:
     *
     * -> Create a new JpaTransactionManager object. -> Configure the entity manager factory whose
     * transactions are managed by the created JpaTransactionManager object.
     **/
    @Bean
    JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;
    }

}
解决方案

我只需要指定存储库的包,因为默认情况下,它是一个不会被搜索的位置-->@EnableJpaRepositories(“com.project.app.services.repositories”),您必须在
@配置
类上声明
@组件扫描
@EnableJpaRepositories
注释

@Repository
@RepositoryRestResource(collectionResourceRel = "stop", path = "stop")
public interface StopRepository extends CrudRepository<StopJPA, Long> {

    StopJPA findById(@Param("id") Long id);

    StopJPA findByIdStop(@Param("idStop") String idStop);

    @Override
    void delete(StopJPA deleted);

    @Override
    List<StopJPA> findAll();

    // Optional<StopJPA> findOne(Long id);
    @Override
    StopJPA findOne(Long id);

    @Override
    StopJPA save(StopJPA persisted);

    void flush();

}
@Entity
@Table(name = "stop")
@EntityListeners(RepoListener.class)
public class StopJPA implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Column(name = "stop_description")
    private String stopDescription;

    @Column(name = "id_stop", nullable = false)
    private String idStop;

    public StopJPA() {
    }

    public StopJPA(String stopDescription, String idStop) {
        this.stopDescription = stopDescription;
        this.idStop = idStop;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getStopDescription() {
        return stopDescription;
    }

    public void setStopDescription(String stopDescription) {
        this.stopDescription = stopDescription;
    }

    public String getIdStop() {
        return idStop;
    }

    public void setIdStop(String idStop) {
        this.idStop = idStop;
    }

}
@EnableWebMvc
@Configuration
//@EnableJpaRepositories
@ComponentScan(basePackages = { "com.project.app" })

public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private Environment env;

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

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("hello");
    }

    @Bean
    public ApplicationContextProvider applicationContextProvider() {
        return new ApplicationContextProvider();
    }
}
关于
@EnableJpaRepositories

注释以启用JPA存储库。默认情况下,将扫描带注释配置类的包以查找Spring数据存储库

在使用时,必须在
@配置
类上声明
@组件扫描
@EnableJpaRepositories
注释

关于
@EnableJpaRepositories

注释以启用JPA存储库。默认情况下,将扫描带注释配置类的包以查找Spring数据存储库


如果在将存储库类用作自动连线时出现此错误。 您只需要根据存储库类使用下面的注释

Mongo存储库:@enablemongorepositions(“spring.mongorestapi.repositories”)


JPA Repository:@EnableJPARepositories(“spring.jparestapi.repositories”)

如果在使用存储库类作为自动连线时出现此错误。 您只需要根据存储库类使用下面的注释

Mongo存储库:@enablemongorepositions(“spring.mongorestapi.repositories”)


JPA存储库:@EnableJPARepositories(“spring.jparestapi.repositories”)

很好。我更新了我的问题,并加入了我的配置文件。我照你说的做了,但它给了我和以前一样的错误。是不是(可能)因为我的配置文件不止这些?好的。问题解决了。我只需要指定存储库的包,因为它是默认情况下不会被搜索的位置-->@EnableJpaRepositories(“com.project.app.services.repositories”)。我更新了我的问题,并加入了我的配置文件。我照你说的做了,但它给了我和以前一样的错误。是不是(可能)因为我的配置文件不止这些?好的。问题解决了。我只需要指定存储库的包,因为它是默认情况下不会被搜索的位置-->@EnableJpaRepositories(“com.project.app.services.repositories”)