AOP Methodenauswahl

Generic1

Top Contributor
Hi,

Ich habe folgenden pointcut welcher sehr gut funktioniert, Ich bin jetzt aber kein AOP experte und weiß jetzt nicht genau, wie ich den Pointcut ändern soll, damit der Pointcut nur in meiner Klasse HomeController zuschlägt.
Vielleicht kann das jemand aus dem FF beantworten.
Besten Dank dafür,
lg
Generic1

[XML]
<aop:before pointcut="execution(* *.searchMember(..))" method="beforeMethodInvokation"/>
[/XML]
 

mvitz

Top Contributor
Ich meine, dass müsste
[XML]<aop:before pointcut="execution(* HomeController.searchMember(..))" method="beforeMethodInvokation"/>[/XML]
sein.
 

Generic1

Top Contributor
Der Meinung war ich auch aber das mag er nicht:

Code:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: warning no match for this type name: HomeController [Xlint:invalidAbsoluteTypeName]


Ich hab den HomeController so definiert:

Java:
@Controller
public final class HomeController {
...


Irgednwie ist der nicht bekannt im Spring Framework.
Was kann ich da noch machen?

lg
 

mvitz

Top Contributor
Dann wars wohl doch:
[XML]<aop:before pointcut="execution(* path.to.my.package.HomeController.searchMember(..))" method="beforeMethodInvokation"/>[/XML]

eventuell kann man das Package auch nochmal mit * bezeichnen, aber für deinen Fall sollte das wohl auch so passen.
 

Generic1

Top Contributor
Das hab ich vorher auch gerade probiert:

[XML]
<bean id="loggingAdvice" class="path.to.my.advice.LoggingAdvice" />

<aop:config>
<aop:aspect ref="loggingAdvice">
<aop:before pointcut="execution(* path.to.my.controller.HomeController.searchMember(..))" method="beforeMethodInvokation"/>
</aop:aspect>
</aop:config>
[/XML]

Da wird aber interessanter weise die beforeMethodInvokation- Methode von meinem Advice nicht mehr aufgerufen obwohl die methode searchMember aufgerufen wird (da hab ich ein System.out.println drinnen).
Ehrlich gesagt keine Ahnung warum das so ist, wenn ich * *searchMember(..) als pointcut habe, dann wird die Methode beforeMethodInvokation einwandfrei aufgerufen - mein problem ist halt nur, dass ich mehrere searchMember Methoden in meiner Appl habe und das eben granularer haben will.
Weiß da noch jemand was dazu?
 

mvitz

Top Contributor
Ok, folgendes Beispiel funktioniert bei mir:
[XML]<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
Index of /schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
Index of /schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">


<bean id="controller" class="de.mvitz.example.spring.aop.HomeController" />
<bean id="aspect" class="de.mvitz.example.spring.aop.MyAspect" />

<aop:config>
<aop:aspect ref="aspect">
<aop:before pointcut="execution(* de.mvitz.example.spring.aop.HomeController.searchMember(..))"
method="before" />
</aop:aspect>
</aop:config>

</beans>[/XML]
Java:
package de.mvitz.example.spring.aop;

public class HomeController {

    public void searchMember(int id) {
        System.out.println("HomeController.searchMember(" + id + ")");
    }

}
Java:
package de.mvitz.example.spring.aop;

public class MyAspect {

    public void before() {
        System.out.println("MyAspect.before()");
    }

}
Java:
package de.mvitz.example.spring.aop;

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

public class Main {

    public static void main(String[] args) {
        final ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        context.getBean(HomeController.class).searchMember(1);
    }

}
Code:
08.07.2012 15:51:35 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2a0ecd7e: startup date [Sun Jul 08 15:51:35 CEST 2012]; root of context hierarchy
08.07.2012 15:51:35 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [application.xml]
08.07.2012 15:51:36 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2f8a49e0: defining beans [controller,aspect,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0]; root of factory hierarchy
MyAspect.before()
HomeController.searchMember(1)
[XML]<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.mvitz.example</groupId>
<artifactId>spring-aop</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.12</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>[/XML]
 

Generic1

Top Contributor
Ich vermute mal, dass @Controller und AOP im XML konfigurieren nicht zusammenpasst. Ich hab die Services auch im XML definiert, das klappt dann.
 

mvitz

Top Contributor
Auch mit @Controller und <context:component-scan base="de.mvitz.example.spring.aop" /> funktioniert es bei mir, naja egal, hast dein Problem ja gelöst.
 

Generic1

Top Contributor
Hätte jetzt noch eine Frage dazu, ich möchte auf einen Parameter zugreifen:

[XML]
<aop:config>
<aop:aspect ref="loggingAdvice">
<aop:before pointcut="execution(* at.eventtiming.businesslogic.service.IHomeService.searchMember(..)) and args(membername)" method="beforeMethodInvokation"/>
</aop:aspect>
</aop:config>
[/XML]


die Methode beforeMethodInvokation schaut so aus

Java:
 public void beforeMethodInvokation(String membername) {
        logger.info("beforeMethodInvokation");
        System.out.println("Before perform invokation: ");
        }

leider wird die Methode beforeMethodInvokation nicht aufgerufen. Weiß jemand was ich da falsch mache?

lg
 

Neue Themen


Oben