jeudi 12 novembre 2020

How can Spring have problems with args setting for a bean that has no args?

The instantiating class is:

@Service("aaa")
public class CustomAaaApiService {
    @Inject
    protected AaaApiKmxImpl aaaApiKmxImpl;
    ....

The instantiated class is:

@Service
public class AaaApiKmxImpl{
    ...
    @PostConstruct
    protected void init() {
        File userInfoCsv = new File(sprRootDir, USER_INFO_FILE_NAME);
        userInfoCsvEngine = (CSVEngine) context.getBean("CSVEngine", userInfoCsv);
        File userRolesCsv = new File(sprRootDir, USER_ROLES_FILE_NAME);
        userRolesCsvEngine = (CSVEngine) context.getBean("CSVEngine", userRolesCsv);
    }
    ....

Both of them are mentioned in the configuration xml file UhradyForIns.xml as:

<bean id="aaaApiService" class="amcssz.spr.srv.sec.CustomAaaApiService"/>
<bean id="AaaApiKmxImpl" class="amcssz.spr.srv.sec.AaaApiKmxImpl"/>

That configuration is called from the test by

@ContextConfiguration({ "classpath:UhradyForIns.xml"})

I am getting the output:

Error creating bean with name 'AaaApiKmxImpl': Invocation of init method failed; nested exception is 
org.springframework.beans.factory.BeanDefinitionStoreException: Can only specify arguments for the 
getBean method when referring to a prototype bean definition   

Why does Spring say it cannot specify arguments when init() has no arguments? The normal launch of the application runs without these problems. Of course, I would get the beans configuration from it, but both these files have no description in any configurations, except that test one.

I don't think the reason could be these creatBean's in the init(), for then the error message will name CSVEngine bean as the bean that could not be created. And the content of init() due to @PostConstruct won't be called at context creation.

Aucun commentaire:

Enregistrer un commentaire