=====Création du projet===== Nécessite un projet de type [[ide:eclipse:projet#creation_d_un_projet_spring|Spring]]. =====Cycle de vie d'un Bean===== {{:lang:java:spring:spring-bean-life-cycle.png|Cycle de vie d'un Bean Spring}} [[https://howtodoinjava.com/spring/spring-core/spring-bean-life-cycle/|Spring Bean Life Cycle Tutorial - HowToDoInJava]] {{ :lang:java:spring:spring_-_bean_life_cycle_explained_-_howtodoinjava_2019-12-13_22_09_42_.html |Archive du 05/2013 le 13/12/2019}} =====POA autour d'un bean managé===== Bean basique 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 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. 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 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 =====En combinaison avec AspectJ===== Le projet ne doit pas être configuré en AspectJ mais les libraires d'AspectJ doivent être dans le ''Build path''. On travaille avec la même classe ''Bonjour'' (package ''fonction'') que ci-dessus. package aspects; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @Aspect public class Logging { @Around("execution(* fonction.Bonjour.getMessage())") public String aroundGetMessage() { return "INTERCEPTION"; } } package test; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import fonction.Bonjour; public class Main { public static void main(String[] args) { try (ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("Beans.xml")) { Bonjour bonjour = (Bonjour) ctx.getBean("bonjour"); System.out.println(" Compte : " + bonjour.getMessage()); } } } Le fichier ''Beans.xml'' est dans le dossier ''src''. Ici, il faut charger la classe bean ''fonction.Bonjour'' et la classe contenant l'aspect ''aspects.Logging''. ''aspectj-autoproxy'' indique qu'on travaille avec ''AspectJ'' et pas avec ''Spring AOP''.