简体   繁体   English

如何在不被aop:aspect引用的情况下实例化spring bean

[英]How to instantiate spring bean without being referenced from aop:aspect

Using Spring and Java; 使用Spring和Java;

I have a pointcut which works OK. 我有一个切入点,可以正常工作。 Now I want to remove the pointcut and AOP from the spring and just trigger the event with an event from inside the java code but I want "myAdvice" bean still called via Spring and its properties set. 现在,我想从Spring中删除切入点和AOP,而只是通过java代码内部的一个事件触发该事件,但是我希望仍然通过Spring及其属性集调用“ myAdvice” bean。

I want to get ridoff all advice things even in java code, no more advice or any trace of AOP, I already have a nice event system working. 我想摆脱所有建议,即使是在Java代码中,也不再需要任何建议或任何AOP痕迹,我已经有一个不错的事件系统。 I just want to instantiate my bean via Spring . 我只想通过Spring实例化我的bean

When I remove the second code block (one starting with "aop:config") then I noticed the bean "myAdvice" is not called and instantiated anymore. 当我删除第二个代码块(以“ aop:config”开头的代码块)时,我注意到不再调用并实例化bean“ myAdvice”。 How can i stil call it set its properties without referencing it from the "aop:aspect" ? 我怎么能不称呼它设置其属性,而无需从“ aop:aspect”引用它?

in my application context ; 在我的应用上下文中;

<bean id="myAdvice" class="com.myclass">
    <property name="name1" ref="ref1" />
    <property name="name2" ref="ref2" />        
</bean>


<aop:config proxy-target-class="true">
    <aop:aspect id="myAspect" ref="myAdvice">
        <aop:pointcut id="myPointcut" expression="execution(* com.myexcmethod" />
        <aop:around pointcut-ref="myPointcut" method="invoke" />
    </aop:aspect>
</aop:config>

If you want to be able to get at the bean itself, you can pass it into this function: 如果您想获得bean本身,可以将其传递给此函数:

public static Object unwrapProxy(Object proxiedBean) throws Exception {
    while(proxiedBean instanceof Advised) {
        proxiedBean = ((Advised) proxiedBean).getTargetSource().getTarget();
    }

    return proxiedBean;
}

Note that you need to have the loop in order to ensure that you unwrap all the AOP advice. 请注意,您需要进行循环以确保解开所有 AOP建议。

Your configuration looks fine . 您的配置看起来不错。 Your bean should be instantiated as a singleton bean when the ApplicationContext loads up . 当ApplicationContext加载时,应将您的bean实例化为单例bean。 Maybe you would want to check if you have a default-lazy-init setting which prevents eager loading of the beans. 也许您想检查一下是否有一个default-lazy-init设置,该设置可以防止立即加载Bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM