0

I have a legacy application which uses Spring for bean management (AnnotationConfigWebApplicationContext) and CDI (inject, brought by jersey-spring dependency) for DI.

I have the following situation:

@Service
@RequestScoped
@Scope(value = "request")
public class InjectedClass {
     private SomeEnum someAttribute;

     public void getSomeAttribute() {
          return someAttribute;
     }
}

@Service
public class MiddleLayer {
         @Inject
         public MiddleLayer(InjectedClass injectedClass) {
                
         }

         private void middleLayerMethod() {
             if (someAttribute == SomeEnum.Y) {
                  // do something specific
             } 
         }
}


// controller
public class SomeController { 
         @Inject
         // In this flow injectedClass instance is initialised with SomeAttribute = Y by ContainerRequestFilter
        public SomeController(MiddleLayer middleLayer) {
                
        }

        public void someMethod () {
               MethodResult result = middleLayer.middleLayerMethod();
               // do some additional things with result
        }
}

@Component
public class PeriodicActivity {
        @Inject
        // I need this MiddleLayer to be injected with injectedClass instance where SomeAttribute = X, since it doesn't go via request filter
        public PeriodicActivity(MiddleLayer middleLayer) {
                
        }

        public void method () {
               MethodResult result = middleLayer.middleLayerMethod();
               // do some other things with result
        }
}

Without DI what I need to happen would look like this:

public class PeriodicActivity() {
       InjectedClass injectedClassObjA = new InjectedClass();
       injectedClassObjA.setSomeAttribute(X);
       MiddleLayer middleLayer = new MiddleLayer(injectedClassObjA);
       middleLayer.middleLayerMethod();
}

I am looking to do something similar with dependencies. After some reading I am starting to wonder whether it is possible.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Victoriia
  • 105
  • 9

0 Answers0