代码之家  ›  专栏  ›  技术社区  ›  Geka

当我使用aspect时,bean不会被注入

  •  0
  • Geka  · 技术社区  · 7 年前

    当我使用spring aspect-bean时 我的班级 不注入。为什么可能,在这种情况下,我能做些什么来启用注入?

    如果 aop:配置 在XML中注释掉,输出为:

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

    如果 aop:配置 不是 在XML中注释掉,输出为:

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

    应用程序。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 +'}'; }
    }
    

    我的方面。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>
    

    2 回复  |  直到 7 年前
        1
  •  0
  •   Ivonet    7 年前

    MyClass 似乎不是@组件,因此可能无法在ComponentScan中找到

        2
  •  0
  •   Geka    7 年前

    一切都变得简单了。Spring AOP代理不代理字段