lang:java:spring
Table des matières
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
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.
- Logging.java
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"; } }
- Main.java
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.
- Beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:beans="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.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd "> <aop:aspectj-autoproxy /> <beans:bean id="bonjour" class="fonction.Bonjour" /> <beans:bean class="aspects.Logging" /> </beans:beans>
lang/java/spring.txt · Dernière modification : de root

