Einen Aspect über eine Dependency injection definieren

CyberwolfXXL

Grünschnabel
Hallo ,
ich versuche mich gerade in AOP einzuarbeiten. Gibt es ein funktionierendes Beispiel, wie man einen Aspect per DI definieren kann?
Ich verwende Eclipse 3.3 und bekomme bei meinen Test immer einen Fehler.
Kann mir da vielleicht einer helfen?

Danke im Voraus.

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [aopASpectJ/context2.xml]; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException


XML:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> 

	<bean id="HalloWelt" class="aopASpectJ.HalloWelt" />
	<aop:config>
		<aop:aspect ref="helloFromSpringAOP">
			<aop:pointcut id="mainMethod"
				expression="execution(* main(..))" />
			<aop:after-returning pointcut-ref="mainMethod"
				method="sayBye" />
		</aop:aspect>
	</aop:config>

	<!-- andere Beans -->
</beans>
 
Zuletzt bearbeitet von einem Moderator:
Was meinst du mit einen Aspekt per DI definieren?
Möchtest du andere Spring-Beans in deinen Aspekt injizieren oder möchtest du einen Aspekt deklarieren der auf bestimmte Methoden einer Fachklasse reagiert?
Die Exception sieht mir so aus als sei aspectjweaver.jar nicht im classpath, die brauchst du für die Arbeit mit AspectJ.
Deine XML sollte so eigentlich trotzdem nicht funktionieren.
 
Wie Cojote schon sagt, die Exception deutet darauf hin, dass ein JAR fehlt. Grundsätzlich scheinst du aber zu versuchen, die Mainmethode einer Javaklasse mit einem Aspekt versehen zu wollen. Du kannst mit dem einfachen Spring AOP nur Springbeans mit Aspekten versehen. Angenommen du hast mit FooImpl eine Implementierungsklasse eines interfaces:

Java:
public interface Foo {

  public void myMethod(String bar);
}

und einen Aspekt

Java:
public class MyAspect {

  public void myAspectmethod() {
    // ..
  }
}

Dann musst du das so wiren:

XML:
<!-- AOP Target -->
<bean id="target" class="FooImpl" />

<!-- Aspect -->
<bean id="advice" class="MyAspect" />

<aop:config>
  <aop:advisor pointcut="execution(* myMethod(..))" advice-ref="advice" />
</aop:config>

Das nutzen der @AspectJ Annotation ist vielleicht etwas einfacher, der aop Namespace ist meiner Meinung nach etwas verwirrend zu benutzen ;).

Gruß
Ollie
 
Zuletzt bearbeitet von einem Moderator:
Hallo,

im Classpath brauchst du das:
spring.jar
aspectjrt.jar
aspectjweaver.jar
commons-logging.jar

schau mal hier:
XML:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
    <bean name="service" class="de.tutorials.services.internal.Service"/>
    
    <bean name="tracingAspect" class="de.tutorials.aspects.TracingAspect"/>
    
    <aop:config>
            <aop:pointcut id="serviceOperationExecution" 
                          expression="execution(* de.tutorials.services.IService+.*(..))"/>
            <aop:aspect ref="tracingAspect">
                <aop:before pointcut-ref="serviceOperationExecution" method="beforeServiceOperation"/>
            </aop:aspect>
    </aop:config>
    
            
</beans>

Java:
package de.tutorials.services;

public interface IService {
    void businessOperation();
}

Java:
/**
 * 
 */
package de.tutorials.services.internal;

import de.tutorials.services.IService;

/**
 * @author Thomas.Darimont
 *
 */
public class Service implements IService{
    public void businessOperation() {
        System.out.println("Service.businessOperation");
    }
}

Java:
/**
 * 
 */
package de.tutorials.aspects;

import org.aspectj.lang.JoinPoint;

/**
 * @author Thomas.Darimont
 * 
 */
public class TracingAspect {
    public void beforeServiceOperation(JoinPoint joinPoint) {
        System.out.println("Before: " + joinPoint);
    }
}

Java:
/**
 * 
 */
package de.tutorials;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import de.tutorials.services.IService;

/**
 * @author Thomas.Darimont
 *
 */
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext context = new FileSystemXmlApplicationContext("config/context.xml");
        IService service = (IService)context.getBean("service");
        service.businessOperation();
        
    }

}

Ausgabe:
Code:
05.08.2008 15:35:14 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1cafa9e: display name [org.springframework.context.support.FileSystemXmlApplicationContext@1cafa9e]; startup date [Tue Aug 05 15:35:14 CEST 2008]; root of context hierarchy
05.08.2008 15:35:15 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [D:\eclipse\workspaces\workspace-3.4\de.tutorials.spring.training\config\context.xml]
05.08.2008 15:35:15 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@1cafa9e]: org.springframework.beans.factory.support.DefaultListableBeanFactory@14a8cd1
05.08.2008 15:35:15 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@14a8cd1: defining beans [service,tracingAspect,org.springframework.aop.config.internalAutoProxyCreator,serviceOperationExecution,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0]; root of factory hierarchy
Before: org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint: execution(businessOperation)
Service.businessOperation

Ansonsten findest du im Forum zahlreiche weitere Beispiele zu AOP AspectJ / Spring AOP. Einfach mal danach suchen... ;-)

Gruß Tom
 
Zuletzt bearbeitet von einem Moderator:
Hallo und Danke für Euere Hilfe.
Ich habe tatsächlich die aspectjweaver.jar nicht im Classpath gehabt. Daran habe ich nicht gedacht , da ich aufgrund der Fehlermeldung an einen Fehler in der XML Datei geglaubt hatte.:)
 

Neue Beiträge

Zurück