Home > front end >  Spring to Spring boot Migration - No qualifying bean of type 'java.lang.String' error
Spring to Spring boot Migration - No qualifying bean of type 'java.lang.String' error

Time:09-03

I am working on migration of a spring project to spring boot. The spring project is using xml configuration for defining beans. I want to use the same beans while migrating to springboot instead of generating the beans automatically. So I imported the xml file in my application.java file which contains the main method. When I run the springboot application, I am getting below error for one of the beans.

Error: Error creating bean with name 'validateAuthentication': Unsatisfied dependency expressed throuth field authenticateCloudHost No qualifying bean of type 'java.lang.string' available: expected single matching bean but found2: CONNECT_TIMEOUT, READ_TIMEOUT

authenticateCloudHost in the bean is a property which is getting a value from the application.properties file inside resource folder of the project. The code sample looks like below.

applicationContext.xml:

<bean id="validateAuthentication" class = "com.abc.JAXRSValidateAuthenticateAssociateClient>
   <constructor-arg index="0" value = "${api.client.id}">
   <constructor-arg index="1" value = "${api.client.password}">
   <property name="authenticateCloudHost" value = "${api.client.cloud.host}">
<bean>

JAXRSValidateAuthenticateClient.java:

@Named
public class JAXRSValidateAuthenticateAssociateClient {
    String id;
    String password;

    public JAXRSValidateAuthenticateAssociateClient (String id, String password) {
        this .id= id
        this.password = password
    }

    @Inject
    private String authenticateCloudHost ;

    public void setAuthenticateCloudHost(String authenticateCloudHost) {
        this.authenticateCloudHost  = authenticateCloudHost;
    }

}

Application.java:

@SpringBootApplication
@ImportResource("classpath:applicationContext.xml")
public class Application {
    public static void main (String[] args){
        SpringAPplication.run(Application.class, args);
    }

}

Any help will be highly appreciated.

CodePudding user response:

You cannot inject an object of type string unless you have one in your context. I assume you wanted to inject a value form the configuration file. In general, it looks strange that the other two strings are injected as constructor parameters and field injection is used for the third one.

If you still want to inject 'api.client.cloud.host' property from your configuration file, you should use

@Value("${api.client.cloud.host}")
private String authenticateCloudHost; 

annotation instead of @Inject Though if you can change the code, I would rather recommend to add another parameter to your constructor and use constructor injection instead. You can either use the same annotation on the constructor parameter, or create the bean manually and inject the 'api.client.cloud.host' value at the class where you call constructor of you JAXRSValidateAuthenticateAssociateClient bean

  • Related