2010-07-30 7 views
0

Je lis Spring in Action et j'essaye de donner un exemple aop.Pas de sortie aop utilisant spring aop 2.0

package com.springinaction.chapter01.knight; 
import org.springframework.beans.factory.BeanFactory; 
import org.springframework.beans.factory.xml.XmlBeanFactory; 
import org.springframework.core.io.FileSystemResource; 

public class KnightApp { 
    public static void main(String[] args) throws Exception { 
     BeanFactory factory = new XmlBeanFactory(new FileSystemResource("knight.xml")); 
     Knight knight = (Knight) factory.getBean("knight"); 
     knight.embarkOnQuest(); 
    } 
} 

Le fichier KnightOfTheRoundTable.java:

package com.springinaction.chapter01.knight; 

public class KnightOfTheRoundTable implements Knight { 
    private String name; 
    private Quest quest; 

    public KnightOfTheRoundTable(String name) { 
     this.name = name; 
    } 

    public Object embarkOnQuest() throws QuestFailedException { 
     //minstrel.singBefore(this); 
     HolyGrail grail = (HolyGrail) quest.embark(); 
     //minstrel.singAfter(this); 
     return grail; 
    } 

    public void setQuest(Quest quest) { 
     this.quest = quest; 
    } 

    @Override 
    public String getName() { 
     // TODO Auto-generated method stub 
     return name; 
    } 
} 

Le fichier Minstrel.java:

package com.springinaction.chapter01.knight; 

import org.apache.log4j.Logger; 

public class Minstrel { 
    private static final Logger SONG = Logger.getLogger(Minstrel.class); 

    public void singBefore(Knight knight) { 
     SONG.info("Fa la la; Sir " + knight.getName() + " is so brave!"); 
     System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); 
    } 

    public void singAfter(Knight knight) { 
     SONG.info("Tee-hee-he; Sir " + knight.getName() 
       + " did embark on a quest!"); 
    } 
} 

La sortie est la suivante:

DEBUG ClassUtils - Class [org.apache.commons.collections.map.LinkedMap] or one of its dependencies is not present: java.lang.ClassNotFoundException: org.apache.commons.collections.map.LinkedMap 
DEBUG ClassUtils - Class [edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap] or one of its dependencies is not present: java.lang.ClassNotFoundException: edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap 
INFO XmlBeanDefinitionReader - Loading XML bean definitions from file [C:\Users\Chris\workspace\chapter01\knight.xml] 
DEBUG DefaultDocumentLoader - Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl] 
DEBUG PluggableSchemaResolver - Loading schema mappings from [META-INF/spring.schemas] 
DEBUG PluggableSchemaResolver - Found XML schema [http://www.springframework.org/schema/aop/spring-aop-2.0.xsd] in classpath: org/springframework/aop/config/spring-aop-2.0.xsd 
DEBUG DefaultBeanDefinitionDocumentReader - Loading bean definitions 
DEBUG XmlBeanFactory - Creating shared instance of singleton bean 'knight' 
DEBUG XmlBeanFactory - Creating instance of bean 'knight' with merged definition [Root bean: class [com.springinaction.chapter01.knight.KnightOfTheRoundTable]; scope=singleton; abstract=false; lazyInit=false; autowireCandidate=true; autowireMode=0; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [C:\Users\Chris\workspace\chapter01\knight.xml]] 
DEBUG XmlBeanFactory - Eagerly caching bean 'knight' to allow for resolving potential circular references 
DEBUG XmlBeanFactory - Creating shared instance of singleton bean 'quest' 
DEBUG XmlBeanFactory - Creating instance of bean 'quest' with merged definition [Root bean: class [com.springinaction.chapter01.knight.HolyGrailQuest]; scope=singleton; abstract=false; lazyInit=false; autowireCandidate=true; autowireMode=0; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [C:\Users\Chris\workspace\chapter01\knight.xml]] 
DEBUG XmlBeanFactory - Eagerly caching bean 'quest' to allow for resolving potential circular references 

Fichier knight.xml :

<bean id="minstrel" class="com.springinaction.chapter01.knight.Minstrel" /> 
<aop:config> 
    <aop:aspect ref="minstrel"> 
     <aop:pointcut id="questPointcut" 
      expression="execution(* *.embarkOnQuest(..)) and target(bean)" /> 
     <aop:before method="singBefore" pointcut-ref="questPointcut" 
      arg-names="bean" /> 
     <aop:after-returning method="singAfter" 
      pointcut-ref="questPointcut" arg-names="bean" /> 
    </aop:aspect> 
</aop:config> 
<bean id="quest" class="com.springinaction.chapter01.knight.HolyGrailQuest" /> 
<bean id="knight" 
    class="com.springinaction.chapter01.knight.KnightOfTheRoundTable"> 
    <constructor-arg value="Bedivere" /> 
    <property name="quest" ref="quest" /> 
</bean> 

Le code minstrel n'est pas appelé.

Merci.

+1

Tout d'abord, votre exemple est incomplet. Je ne vois aucune définition de l'interface 'Knight' et' Quest'. Je pourrais deviner, de cource, mais toujours. La façon dont vous avez formaté votre question n'aide pas non plus à comprendre le problème. – wax

Répondre

0

Désolé,

fichier knight.xml:

<bean id="minstrel" class="com.springinaction.chapter01.knight.Minstrel" /> 
<aop:config> 
    <aop:aspect ref="minstrel"> 
     <aop:pointcut id="questPointcut" 
      expression="execution(* *.embarkOnQuest(..)) and target(bean)" /> 
     <aop:before method="singBefore" pointcut-ref="questPointcut" 
      arg-names="bean" /> 
     <aop:after-returning method="singAfter" 
      pointcut-ref="questPointcut" arg-names="bean" /> 
    </aop:aspect> 
</aop:config> 
<bean id="quest" class="com.springinaction.chapter01.knight.HolyGrailQuest" /> 
<bean id="knight" 
    class="com.springinaction.chapter01.knight.KnightOfTheRoundTable"> 
    <constructor-arg value="Bedivere" /> 
    <property name="quest" ref="quest" /> 
</bean> 
0

Je vois deux problèmes: 1.
Vos journaux contiennent ClassNotFoundException pour les classes org.apache.commons.collections.map.LinkedMap et edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap Wich est tout à fait étrange, parce que votre code doesn Je ne mentionne aucune de ces classes, et Spring, pour autant que je sache, ne les utilise pas non plus. Pour corriger ces exceptions, vous devez ajouter les bibliothèques Apache common-collections.jar et backport-util-concurrent.jar à votre chemin de classe.

2. Vous utilisez l'implémentation BeanFactory au lieu de l'implémentation ApplicationContext. Selon la documentation Spring, les implémentations de BeanFactory ne prennent pas en charge AOP. Voici une citation à partir de là:
Un BeanFactory ne fait que simuler et configurer des beans. Un ApplicationContext fait également cela, et il fournit l'infrastructure de support pour activer de nombreuses fonctionnalités spécifiques à l'entreprise telles que les transactions et AOP.

0

J'ai également eu le même problème en lisant à propos de Spring AOP. Puis j'ai ajouté la dépendance dans mon fichier pom.xml (j'ai utilisé spring avec maven) et ensuite ça a commencé à fonctionner.

<dependency> 
<groupId>aspectj</groupId> 
<artifactId>aspectjweaver</artifactId> 
<version>1.5.4</version> 
<scope>compile</scope> 
</dependency> 

Remarque: J'ai utilisé le code suivant pour charger le fichier de configuration du ressort.

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");