Spring boot vaadin project @Autowired

FoufaFaFa

I am using spring boot vaadin project.I have a repository like this:public interface PersonneRepository extends JpaRepository<Person, Integer>, JpaSpecificationExecutor {}

I want to instantiate this repository in my class .In Ui and in Views I do this: @Autowired PersonneRepository repo; this work easily but in simple class(public class x{}) repo return null .And I don't like to passe it in parameter or session .Have you any idea please?

agassner

To inject dependencies the dependent classes have to be managed by Spring. This can be achieved with the class annotation @Component:

Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.

For use in Vaadin classes @SpringComponent it's recommended:

Alias for {@link org.springframework.stereotype.Component} to prevent conflicts with {@link com.vaadin.ui.Component}.

Example:

@Repository // Indicates that an annotated class is a "Repository", it's a specialized form of @Component
public interface PersonRepository extends JpaRepository<Person, Long> {
    // Spring generates a singleton proxy instance with some common CRUD methods
}

@UIScope // Implementation of Spring's {@link org.springframework.beans.factory.config.Scope} that binds the UIs and dependent beans to the current {@link com.vaadin.server.VaadinSession}
@SpringComponent
public class SomeVaadinClassWichUsesTheRepository {

    private PersonRepository personRepository;

    @Autowired // setter injection to allow simple mocking in tests
    public void setPersonRepository(PersonRepository personRepository) {
        this.personRepository = personRepository;
    }

    /**
     * The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.
     */ 
    @PostConsruct
    public init() {
        // do something with personRepository, e.g. init Vaadin table...
    }
}

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

Spring Boot: erro autowired

Spring Boot Autowired null

Construtor Vaadin com beans Autowired Spring JPA

NullPointerException usando @autowired em Vaadin e Spring

Spring boot @Autowired repository is null

A variável Spring-Boot @Autowired é nula

Spring Boot - Environment @Autowired throws NullPointerException

Spring Boot @Autowired custom application.properties

O campo Autowired é nulo no aplicativo Spring Boot

Spring boot encontrar autowired em outro pacote

Exceção autowired do construtor Spring boot

Vaadin Flow 18/19 sem Spring Boot?

Vaadin 10 is not using templates in Spring-Boot

Spring Boot Security com Vaadin Login

Vaadin faltando alguma função no Spring Boot

spring data jpa repository inject fails using @Autowired in spring boot

Erro de boot Spring @Autowired RestTemplateBuilder com junit

Java Spring-boot - Como usar @Autowired com @ServerEndpoint?

A anotação @Autowired é obrigatória no construtor no Spring boot

Spring-Boot Elasticseach EntityMapper não pode ser autowired

Spring Boot - @Autowired não está funcionando em @Service

Spring Boot Request Scoped Beans Autowired não preenchido

Repositório autowired nulo no controlador para um aplicativo Spring Boot

Spring Boot @Autowired no teste de unidade retorna NullPointerException

Spring Boot Autowired um repo em um modelo

Repositório nulo autowired do Spring boot mongoDB

Spring-Boot @Autowired na classe principal está ficando nulo

In which classes is it allowed to use @Autowired in a Spring Boot application?

O serviço @Autowired do Spring Boot é nulo no WAR

TOP lista

quentelabel

Arquivo