Outils pour utilisateurs

Outils du site


lang:java:spring

Ceci est une ancienne révision du document !


Création du projet

Nécessite un projet de type Spring.

Cycle de vie d'un Bean

POA autour d'un bean managé

Bean basique

Bonjour.java
public class Bonjour {
  private String message;
 
  public void getMessage() {
    System.out.println("Your Message : " + message);
  }
 
  public void setMessage(String message) {
    this.message = message;
  }
 
  public void init() {
    System.out.println("Initialisation");
  }
}

La classe inspectant l'initialisation des classes

InitBonjour.java
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
 
public class InitBonjour implements BeanPostProcessor {
  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("Before : " + beanName);
    return bean;
  }
 
  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("After : " + beanName);
    return bean;
  }
}

Le programme principal.

Principale.java
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Principale {
  public static void main(String[] args) {
    // Chargement du fichier Beans.xml
    // Initialisation des beans et appel des pré et post initialiseur
    try (ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml")) {
      Bonjour obj = (Bonjour) context.getBean("bonjour");
      obj.getMessage();
    // Fermeture du contexte
    }
  }
}

Le fichier de configuration Beans.xml

Beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<p:beans xmlns:p="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd ">
  <p:bean class="com.m2i.InitBonjour" />
  <p:bean id="bonjour" class="com.m2i.Bonjour" init-method="init">
    <p:property name="message" value="Bonjour à tous." />
  </p:bean>
  <p:bean id="bonjour2" class="com.m2i.Bonjour" init-method="init">
    <p:property name="message" value="Bonjour à tous." />
  </p:bean>
</p:beans>

Le rendu

déc. 17, 2016 11:45:11 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFOS: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1f17ae12: startup date [Sat Dec 17 23:45:11 CET 2016]; root of context hierarchy
déc. 17, 2016 11:45:11 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFOS: Loading XML bean definitions from class path resource [Beans.xml]
Before : bonjour
Initialisation
After : bonjour
Before : bonjour2
Initialisation
After : bonjour2
Your Message : Bonjour à tous.
déc. 17, 2016 11:45:12 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFOS: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1f17ae12: startup date [Sat Dec 17 23:45:11 CET 2016]; root of context hierarchy
lang/java/spring.1482013360.txt.gz · Dernière modification : 2016/12/17 23:22 de root