Home When I use aspect - bean does not injected
Reply: 2

When I use aspect - bean does not injected

Geka
1#
Geka Published in 2017-12-07 22:23:31Z

When I use spring-aspect - bean myClass does not injected. Why it possible, and what can i do to enable injection it this case?

If aop:config is commented out in the XML, output is:

myClass from constructor: MyClass{myInt=3333}
myClass from main: MyClass{myInt=3333}
app logEvent

If aop:config is NOT commented out in the XML, output is:

myClass from constructor: MyClass{myInt=3333}
myClass from main: null
LOG: BEFORE : App logEvent
app logEvent

App.java

public class App {
    MyClass myClass;

    public static void main(String[] args) {
        ApplicationContext ctx;
        ctx = new ClassPathXmlApplicationContext("conf.xml");
        App app = (App) ctx.getBean("app");

        System.out.println("myClass from main: " + app.myClass);
        app.logEvent();
    }

    public App(MyClass myClass) {
        this.myClass = myClass;
        System.out.println("myClass from constructor: " + this.myClass);
    }

    public void logEvent(){
        System.out.println("app logEvent");
    }
}

MyClass.java

public class MyClass {
    int myInt=6789;

    public int getMyInt() {return myInt;}

    public void setMyInt(int myInt) {this.myInt = myInt;}

    @Override
    public String toString() {return "MyClass{myInt=" + myInt +'}'; }
}

MyAspect.java

public class MyAspect {
    public void logBefore(JoinPoint joinPoint) {
        System.out.println("LOG: BEFORE : " +
                joinPoint.getTarget()
                        .getClass().getSimpleName() + " " +
                joinPoint.getSignature()
                        .getName()
        );
    }
}

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

<bean id="app" class="com.geka.spring2.App" c:myClass-ref="myClass"/>
<bean id="myClass" class="com.geka.spring2.MyClass" p:myInt="3333" />

<bean id="aBean" class="com.geka.spring2.MyAspect"  />

<aop:aspectj-autoproxy />

<aop:config>
    <aop:aspect id="myAspect" ref="aBean">
        <aop:pointcut id="cLogg" expression="execution(* *.logEvent(..))"/>
        <aop:before pointcut-ref="cLogg" method="logBefore"/>
    </aop:aspect>
</aop:config>

Ivonet
2#
Ivonet Reply to 2017-12-08 20:08:34Z

MyClass does not seem to be a @Component and will therefore probably not be found in the ComponentScan

Geka
3#
Geka Reply to 2017-12-12 09:19:51Z

Everything turned out to be simpler. Spring AOP proxies do not proxying the fields

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.362798 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO