Can't get the aspect executed for proxy objects

S Kr

I am trying to execute a aspect on proxy object

package thispkg;

public class MyLogger {
    public void before() {
        System.out.println("=========Before========");
    }
    public void after() {
        System.out.println("=========After=========");
    }
    public void info() {
        System.out.println("=========Info=========");
    }
}

package thispkg;

public interface MyInterface {
    public void speak();
}

package thispkg;

public class MyInterfaceImpl implements MyInterface {
    @Override
    public void speak() {
        System.out.println("MyInterfaceImpl :: Hello world");
    }
}

package thispkg;

public class RandomClass {
    public void suvichar() {
        System.out.println("RandomClass (suvichar)::Karm kiye jaa, fal ki chinta mat kar");
    }
}

package thispkg;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("thispkg.xml");
        MyInterface in = (MyInterface) context.getBean("randomClass", RandomClass.class);
        in.speak();
    }
}

My 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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:spring-configured/>
    <bean id="mylogger" class="thispkg.MyLogger"/>
    <bean id="randomClass" class="thispkg.RandomClass"/>
    <bean id="myInterfaceImpl" class="thispkg.MyInterfaceImpl"/>
    <aop:config proxy-target-class="true">
        <aop:aspect id="usageTrackerAspect" ref="mylogger">
            <aop:declare-parents types-matching="thispkg.RandomClass+" implement-interface="thispkg.MyInterface" default-impl="thispkg.MyInterfaceImpl"/>
            <aop:pointcut expression="this(thispkg.MyInterface)" id="randompointcut"/>
            <aop:before pointcut-ref="randompointcut" method="info"/>
        </aop:aspect>
    </aop:config>
</beans>

I have tried both thispkg.MyInterfaceImpl & thispkg.RandomClass in pointcut expression but still can't get the ========Info======== printed. Only prints

MyInterfaceImpl :: Hello world

Any clue ?

S Kr

So after some tries and then more reading and then more retries, i found that i can get the desired behaviour when using delegate-ref instead of default-impl, thus allowing spring to manage my implementation. Also correcting my pointcut expression.

<aop:aspect ref="mylogger">
        <aop:declare-parents types-matching="thispkg.RandomClass+" implement-interface="thispkg.MyInterface" delegate-ref="myInterfaceImpl"/>
        <aop:pointcut expression="execution(* thispkg.MyInterface.speak())" id="randompointcut"/>
        <aop:before method="info" pointcut-ref="randompointcut"/>
</aop:aspect>

Output

=========Info=========
MyInterfaceImpl :: Hello world

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't get program executed with execv()

From Dev

Can't get program executed with execv()

From Dev

Can't seem to get fiddler to Proxy HttpClient

From Dev

Can't seem to get fiddler to Proxy HttpClient

From Dev

Can't get a value from $http promise and then() code not executed

From Dev

Can't get list of nested objects in JSON

From Dev

Can't get Last.FM API PHP Proxy to work

From Dev

Why can't this command be executed?

From Dev

.bashrc doesn't get executed

From Dev

Can't compile an AspectJ Aspect with @DeclareMixin

From Dev

Can't make Spring aspect lowest order

From Dev

Using preRemove/postRemove events to get which queries can be executed and which can't

From Dev

Using preRemove/postRemove events to get which queries can be executed and which can't

From Dev

Can't get objects into database in my BootStrap.config

From Dev

Can't get RKPathMatcher pathMatcher to match for deleting orphans objects

From Dev

Why I can't get the related objects of my pulgin?

From Dev

I can't get my card objects to print correctly

From Dev

Objects in JSON array - Can't get multidimenson arrays

From Dev

Can't get Out-GridView to display two objects

From Dev

JavaScript proxy objects don't work

From Dev

aSync Task can't be executed twice

From Dev

aSync Task can't be executed twice

From Dev

Multiple query don't get executed

From Dev

Why doesn't .vimrc get executed?

From Dev

Thread doesn't get executed in C

From Dev

onClick doesn't get executed in ViewHolder

From Dev

Why doesn't .vimrc get executed?

From Dev

In sympy plotting, how can I get a plot with a fixed aspect ratio?

From Dev

Why aspect j can't weave show Xlint cantFindType

Related Related

HotTag

Archive