Spring AOP:我们可以在 Bean 上应用 ProxyFactoryBean 后通过 getBean("beanName") 方法访问 Bean 对象吗?

Spring AOP : Can we access bean object by getBean("beanName") method after applying ProxyFactoryBean on that Bean?

我已经在 "MyXMLApplication" 类型的 Bean 上应用了 'BeforeAdvice' 到 'ProxyFactoryBean' 现在我无法直接访问 Bean 对象{by getBean(MyXMLApplication.class )}, 它给出了错误:-

通过setter依赖注入myxml 线程 "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException 中的异常:未定义 [com.journaldev.spring.di.consumer.MyXMLApplication] 类型的合格 bean:预期单个匹配 bean 但找到 2:MyXMLApp,proxy

但是我可以通过 "proxy" bean object{(MyXMLApplication)context.getBean("proxy")} 获取 bean 对象。现在我的问题是在对任何 bean 应用代理之后是否有任何方法可以在没有代理 bean 的情况下直接访问它。

我的代码是:

********Before Advisor*********




 public class MyBeforeAdvisor implements MethodBeforeAdvice{
        public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
            // TODO Auto-generated method stub
            System.out.println("before advice run");
        }
    }

*********applicationContext.xml********


<bean id="twitter" class="com.journaldev.spring.di.services.TwitterService"></bean>
<bean id="MyXMLApp" class="com.journaldev.spring.di.consumer.MyXMLApplication" autowire="byType"/>
<bean id="beforeAdvice" class="com.journaldev.spring.aop.advisor.MyBeforeAdvisor"></bean>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="MyXMLApp"></property>
        <property name="interceptorNames">
          <list>
             <value>beforeAdvice</value>
          </list>
        </property>
</bean>


**********Main Bean Class************


  public class MyXMLApplication {
        private MessageService service;
        public void setService(MessageService svc){
            System.out.println("by setter dependency injection myxml");
            this.service=svc;
        }
        public boolean processMessage(String msg, String rec) {
            return this.service.sendMessage(msg, rec);
        }
    }



**********Autowired Bean Interface ******* 


    public interface MessageService {
            boolean sendMessage(String msg, String rec);
        }

**********Autowired Bean Impl*********


 public class TwitterService implements MessageService {
        public boolean sendMessage(String msg, String rec) {
            System.out.println("Twitter message Sent to "+rec+ " with Message="+msg);
            return true;
        }
    }



 ************* Main function**********


      public static void main(String[] args) {
                ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                        "applicationContext.xml");
     MyXMLApplication app = context.getBean(MyXMLApplication.class); --> Not Working

    //MyXMLApplication app = (MyXMLApplication)context.getBean("proxy"); -->working

                app.processMessage("Hi", "abc@abc.com");
                context.close();
            }

抱歉,伙计们,错误来了,因为我正在调用 getBean(MyXMLApplication.class) [按类型],这就是为什么它找到了两个 bean "proxy" 和 "MyXMLApp" 类型 MyXMLApplication.class 并给出错误。

并不是说我们不能在 bean 上应用任何代理后直接在 bean 上调用 'getBean' 方法。