0

I have this code:

Caller:

public class MyManager {

@Inject
private ObjectProvider<MyService> mySvcProvider;

public void processMyData(){
     MyService mySvc = mySvcProvider.getObject("myName", "c:\myfolder");
     mySvc.processValues();
}

Callee:

@Named
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class MyService{
   
    private long recId;
    private List<String> values;
  
    @Inject
    private mySingletonDBService mnDBSvc;
    @Inject
    private mySingletonFileService myFileSvc;
    
    @Inject
    public myManager(long recName, File folder)
            throws DataExtractException {
        
        this.recId = myDBSvc.getRecId(recName); <- Throws NPE
        this.values = myFileSvc.getValues(recId);
       
    }
 
    public processMyValues(){
          ....
    }
}

So in the callee when the constructor is invoked it fails accessing myDBSvc, and myFileSvc. Both are null. How do I inject my singleton services into this prototypeBean. . My package is registered in applicationContext.xml as shown below, so all beans from here should be found>

 <context:component-scan base-package="my.package.myProject"/>

BTW this injection works everywhere else on other Singleton Beans. It does not work ONLY on this prototype Bean. So I could technically try Injecting these servicer into the caller 'MyManager' and pass them as constructor arguments, but it is nasty. In the example I only have two services, but in my production code I have about 10-15 services I use. Putting it all on constructor, and then calling this constructor from the callee looks very wrong. There should be a way I can inject a singleton bean into a prototype bean.

The other option I tried is below using applicationContext.getBean(Class, args). Same result: Constructor is invoked correctly. But still those two services are null. Never Injected. I dont know what the difference is, but I also trie d@Autowired instead of Inject. that did not work either. Same result.

Nila
  • 71
  • 6
  • Take care of java naming conventions. Classnames should start with upper case character – Jens Mar 31 '23 at 21:52
  • thanks, and I do ofcourse, cant not follow naming conventions in production applications – Nila Mar 31 '23 at 22:50

0 Answers0