2017-06-11 1 views
0

J'ai appliqué un 'BeforeAdvice' via 'ProxyFactoryBean' sur un Bean de type "MyXMLApplication" maintenant je ne peux pas accéder directement à l'objet Bean {par getBean (MyXMLApplication.class)}, il donne une erreur: -Spring AOP: Pouvons-nous accéder à l'objet bean par la méthode getBean ("beanName") après avoir appliqué ProxyFactoryBean sur ce Bean?

par setter dépendance injection myxml Exception dans le thread "principal" org.springframework.beans.factory.NoUniqueBeanDefinitionException: Aucun bean qualifiant de type [com.journaldev.spring.di.consumer.MyXMLApplication] est défini: haricot correspondant unique attendu found 2: MyXMLApp, proxy

Cependant, je suis en mesure d'obtenir l'objet bean par l'objet bean "proxy" {(MyXMLApplication) context.getBean ("proxy")}. Maintenant, ma question est après l'application de proxy sur n'importe quel bean est-il un moyen d'y accéder directement sans proxy bean.

Mon code est:

********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", "[email protected]"); 
       context.close(); 
      } 

Répondre

0

Désolé les gars, l'erreur est venue parce que j'appelle getBean (MyXMLApplication.class) [par type] qui est la raison pour laquelle il fonde deux haricots "proxy" et "MyXMLApp" de type MyXMLApplication.class et en donnant l'erreur.

Ce n'est pas quelque chose comme nous ne pouvons pas appeler la méthode 'getBean' sur le bean directement après avoir appliqué n'importe quel proxy.