Using autwired beans in an object created at runtime

I have a class B that implements a W-interface. It has a standard implementation of the W method. Class C and D override the default implementation for which they need a service whose spring bean is being created. Line a and b come from the user and therefore I cannot create a bean from B / C / D in advance. Therefore, I have a factory that creates a new object based on user parameters (it will create B / C / D based on parameters). Is there a clean way to use the beans service (aa / bb / cc / dd, etc.) from inside C and D (spring autowires during server startup, at the time needed for the B / C / D instance parameter are unavailable) or is there a better way to solve the problem?

 Class B implements W{
      String a;
      String b;
      B (String a, String b);

      w_method(){

      }
   }

    Class C extends B {
      @Autowired
      Service aa;

      @Autowired
      Service bb;

      @Autowired
      Service cc;

      @override
      w_method(){
      }
    }

Class D extends B {
  @Autowired
  Service dd;

  @override
  w_method(){
  }
}
+3
2

, factory B/C/D, , , @Autowired.

axtavt. ApplicationContext, , Spring : http://nurkiewicz.blogspot.co.uk/2010/08/creating-prototype-spring-beans-on.html

+6

:

  • , , , Spring.

    Spring beans prototype factory:

    public C createC(String a, String b) {
        return applicationContext.getBean("c", a, b);
    }
    

    , factory ApplicationContext.

  • @Configurable AspectJ. Spring , new. . 7.8.1 AspectJ Spring.

  • applicationContext.getAutowireCapableBeanFactory().autowireBean(...);
    

    , , autowire ( ..).

+7

All Articles