Frühjahr erwartet mindestens 1 Bohne, die als qualifiziert autowire-candidate für diese Abhängigkeit

Bin ich mit Spring und Hibernate und ich bin versucht, "wire" die Klassen, die nötig sind, um Autowire eine Repository innerhalb einer Service.

Repository-Klasse erweitert CrudRepository

StopRepository

@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();

}

Die Entity-Klasse.

StopJPA

@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;
    }

}

Und der Service Klasse Umsetzung:

StopService

@Service
final class RepoStopService {

@Service
final class RepoStopService {

    private final StopRepository stopRepository;

    @Autowired
    RepoStopService(StopRepository stopRepository) {
        this.stopRepository = stopRepository;
    } 
}

Leider, wenn ich versuche, führen Sie es auf server bekomme ich diese exception:

SEVERE: Exception sending context initialized event to listener
Instanz der Klasse

org.springframework.web.Kontext.ContextLoaderListener

org.springframework.Bohnen.factory.UnsatisfiedDependencyException:
Fehler bei der Erstellung bean mit dem Namen 'repoStopService' (definiert in der Datei
...\RepoStopService.class:

Unzufrieden Abhängigkeit ausgedrückt durch das argument im Konstruktor mit
index 0 der Typ
[com.Projekt.app.services.repositories.StopRepository]: : Nein
qualifying bean vom Typ

[com.Projekt.app.services.repositories.StopRepository] gefunden
Abhängigkeit: erwartet mindestens 1 Bohne, die als qualifiziert autowire
Kandidaten für diese Abhängigkeit. Abhängigkeit Anmerkungen: {};

verschachtelte Ausnahme ist
org.springframework.Bohnen.factory.NoSuchBeanDefinitionException: Nein
qualifying-bean des Typs
[com.Projekt.app.services.repositories.StopRepository] gefunden
Abhängigkeit: erwartet mindestens 1 Bohne, die als qualifiziert autowire
Kandidaten für diese Abhängigkeit. Abhängigkeit Anmerkungen: {} an
.....

Weiß jemand, warum der Frühling nicht abholen @Repository annotation?


Meine Konfiguration besteht aus 3 Dateien.
Ein AppInitializer Klasse, die WebApplicationInitializer , ein WebMvcConfig Klasse, die Sie erweitert WebMvcConfigurerAdapter und schließlich eine PersistentContext Klasse.

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

@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;
    }

}

LÖSUNG

Ich brauchte nur angeben, auch die Paket-repositories, weil Ihr eine Lage, die nicht standardmäßig durchsucht --> @EnableJpaRepositories("com.Projekt.app.services.repositories")

Schreibe einen Kommentar