IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Boot Java Discussion :

instantiation du contexte de Spring dans une application SpringBoot


Sujet :

Spring Boot Java

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    145
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2015
    Messages : 145
    Points : 62
    Points
    62
    Par défaut instantiation du contexte de Spring dans une application SpringBoot
    Bonjour tous,

    Je développe une application basé sur SpringBoot. J'aimerai pouvoir instancier des Beans de l'application pour faire quelques tests. Après quelques recherches j'ai vu qu'on peut le faire grâce à la classe "ApplicationContextProvider" qui implémente "ApplicationContextAware"
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     
    @Component
    public class ApplicationContextProvider implements ApplicationContextAware {
     
        @Autowired
        private ApplicationContext applicationContext;
     
        @Override
        public void setApplicationContext( ApplicationContext applicationContext ) throws BeansException {
            this.applicationContext = applicationContext;
     
        }
     
        public ApplicationContext getContext() {
            return applicationContext;
        }
     
    }
    Ensuite j'essaie de récupérer le context de l'application via cette classe et d'instancier un bean

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     
    @SpringBootApplication
    public class MyProjetApplication {
     
        public static void main( String[] args ) {
            SpringApplication.run( MyProjetApplication.class, args );
     
            ApplicationContextProvider provider = new ApplicationContextProvider();
     
            ApplicationContext ctx = provider.getContext();
     
            ClientMetierImlp clientMetierImlp = ctx.getBean( ClientMetierImlp.class );
            Client client = new Client( "CC1", "A" );
            clientMetierImlp.saveClient( client );
     
        }
     
    }
    Mais j'ai un NPE (ici ligne 10) dont je ne comprends pas la cause
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    Exception in thread "main" java.lang.NullPointerException
    	at com.projet.config.MyProjetApplication.main(MyProjetApplication.java:20)
    Ma classe client implémente, où "Client" est une Entity

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    @Service
    public class ClientMetierImlp implements ClientMetier {
     
        @Autowired
        ClientRepository clientRepository;
     
        @Override
        public Client saveClient( Client client ) {
     
            return clientRepository.save( client );
        }
     
    }
    Merci de toute aide

  2. #2
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Bonjour,

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
        public static void main( String[] args ) {
            ConfigurableApplicationContext ctx = SpringApplication.run( MyProjetApplication.class, args );
            ClientMetierImlp clientMetierImlp = ctx.getBean( ClientMetierImlp.class );
            Client client = new Client( "CC1", "A" );
            clientMetierImlp.saveClient( client );
         }
    Et n'oublie pas l'annotation @ComponentScan.
    Sinon, montre nous l'erreur complet.

    A+.

  3. #3
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    145
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2015
    Messages : 145
    Points : 62
    Points
    62
    Par défaut
    Et n'oublie pas l'annotation @ComponentScan.
    Dans une application SpringBoot, l'injection de cette annotation n'est pas faite par Spring lui même. Je rappelle que je n'ai pas de fichier application applicationContext.xml.

  4. #4
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Citation Envoyé par batanga09 Voir le message
    Dans une application SpringBoot, l'injection de cette annotation n'est pas faite par Spring lui même.
    Depuis quand on injecte une annotation .
    Citation Envoyé par batanga09 Voir le message
    Je rappelle que je n'ai pas de fichier application applicationContext.xml.
    Et alors?

  5. #5
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    145
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2015
    Messages : 145
    Points : 62
    Points
    62
    Par défaut
    Depuis quand on injecte une annotation .
    Le matin...Je voulait dire Spring s'occupe de la configuration. De plus je pense ne plus avoir besoin de @ComponentScan vu que j'ai déja @SpringBootApplication ...

  6. #6
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Citation Envoyé par batanga09 Voir le message
    Le matin...Je voulait dire Spring s'occupe de la configuration. De plus je pense ne plus avoir besoin de @ComponentScan vu que j'ai déja @SpringBootApplication ...
    Ah, oui, je n'ai pas fait attention avec @SpringBootApplication . Mais la façon dont tu l'as expliquée prête confusion.

    A+.

Discussions similaires

  1. Intégrer Spring dans une application Swing
    Par j_esti dans le forum AWT/Swing
    Réponses: 2
    Dernier message: 11/09/2013, 16h54
  2. Intégration de Spring dans une application GWT
    Par sabrina_sab dans le forum Spring Web
    Réponses: 0
    Dernier message: 04/11/2012, 14h38
  3. [Security] Intégrer la sécurité dans une application swing/spring
    Par kayoum dans le forum Spring
    Réponses: 1
    Dernier message: 31/05/2010, 01h36
  4. Réponses: 1
    Dernier message: 20/02/2010, 19h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo