Ildelian
Ildelian

Reputation: 1318

Spring Bean Not Found

I have this project hierarchy:

A SpringConfiguration.java file with one Bean (SoapServiceBean). A LoggerUtilsConfiguration.java with two beans (ConfigManager and LoggerManager).

SpringConfiguration imports LoggerUtilsConfigueration with an @Import.

The code:

@Configuration
@Import({com.myApp.LoggerUtilsConfiguration.class})
public class SpringConfiguration {

    @Lazy
    @Bean(name = "soapServiceBean")
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    public SoapServiceBean soapServiceBeanInit(
            @Qualifier("configManager") ConfigManager  configManager ,
            @Qualifier("loggerManager") LoggerManager loggerManager) 
            throws ServiceException, SystemException {

        SoapServiceBean service = new SoapServiceBean();
        service.setConfigManager(configManager);
        service.setLoggerManager(loggerManager);

        return service; 
    }

}

@Configuration
public class LoggerUtilsConfiguration {

    @Bean(name = "configManager")
    @Scope(BeanDefinition.SINGLETON)
    public ConfigManager configManagerInit() {

        ConfigManager cManager = new ConfigManager();

        return cManager; 
    }

    @Bean(name = "loggerManager")
    @Scope(BeanDefinition.SINGLETON)
    public LoggerManager loggerManagerInit() {

        LoggerManager cManager = new LoggerManager();

        return cManager; 
    }

}

My problema is that, "configManager" is a valid bean, but loggerManager throws this Exception:

No qualifying bean of type [com.myApp.LoggerManager] found for dependency: 
    expected at least 1 bean which qualifies as autowire candidate for this dependency. 
    Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=loggerManager)} 

Is very Strange, because If I copy the loggerManager Bean method into the main @Configuration class, the app starts with no problem and loggerManager is started with no problem.

"LoggerUtilsConfiguration" is not part of my App, is a Maven dependency external jar from another dev team. I have decompiled it and I cannot see anything strange, "configManager" and "loggerManager" has the same annotations with the same values except the bean name.

Any idea?

Upvotes: 0

Views: 2584

Answers (4)

Ildelian
Ildelian

Reputation: 1318

Solved.

My problem is the WebSphere, that store an old version of the third party JAR.

\was\wasprofiles\wp_profile80\installedApps\GUUPZN00Cell\MY-APP.ear\lib\LOGGER-LIB-1.0.0.jar
\was\wasprofiles\wp_profile80\installedApps\GUUPZN00Cell\MY-APP.ear\lib\LOGGER-LIB-1.0.1.jar

The version "1.0.0" LOGGER-LIB doesn't contains the bean "loggerManager" and WebSphere is loading this version of the library. It seems that the file was blocked when websphere try to delete it. I had to stop the server to delete it properly.

Upvotes: 1

Ananthapadmanabhan
Ananthapadmanabhan

Reputation: 6216

Try providing autowire annotation to properly enable constructor injection like :

@Configuration
@Import({com.myApp.LoggerUtilsConfiguration.class})
public class SpringConfiguration {

    @Lazy
    @Autowired
    @Bean(name = "soapServiceBean")
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    public SoapServiceBean soapServiceBeanInit(
            @Qualifier("configManager") ConfigManager  configManager ,
            @Qualifier("loggerManager") LoggerManager loggerManager) 
            throws ServiceException, SystemException {

        SoapServiceBean service = new SoapServiceBean();
        service.setConfigManager(configManager);
        service.setLoggerManager(loggerManager);

        return service; 
    }

}

The @Qualifier annotation is used to resolve the bean based on names while the @Autowired annotation provides constructor injection of beans. The @Bean annotation should support constructor injection without the need for @Autowired

Upvotes: 0

Vüsal
Vüsal

Reputation: 2706

Try to use @DependsOn:

    @Lazy
    @Bean(name = "soapServiceBean")
    @DependsOn({ "configManager", "loggerManager" })
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    public SoapServiceBean soapServiceBeanInit(
            @Qualifier("configManager") ConfigManager  configManager ,
            @Qualifier("loggerManager") LoggerManager loggerManager) 
            throws ServiceException, SystemException {

        SoapServiceBean service = new SoapServiceBean();
        service.setConfigManager(configManager);
        service.setLoggerManager(loggerManager);

        return service; 
    }

Upvotes: 0

David Newcomb
David Newcomb

Reputation: 10943

If the 2 managers are singletons you can @Autowired them in.

If there is an existing bean called loggerManager of a different type then it wouldn't find yours, but it depends on how your classpath is set up.

@Configuration
@Import({com.myApp.LoggerUtilsConfiguration.class})
public class SpringConfiguration {

    @Autowired
    ConfigManager configManager;
    @Autowired
    LoggerManager loggerManager;

    @Lazy
    @Bean(name = "soapServiceBean")
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    public SoapServiceBean soapServiceBeanInit() 
            throws ServiceException, SystemException {

        SoapServiceBean service = new SoapServiceBean();
        service.setConfigManager(configManager);
        service.setLoggerManager(loggerManager);

        return service; 
    }

}

Upvotes: 0

Related Questions