Ron Buenavida
Ron Buenavida

Reputation: 59

springboot 1.4.7 No qualifying bean of type 'java.lang.String'

I seem to be getting No qualifying bean of type 'java.lang.String' exception when trying to run the project. Anyone have any idea what i'm missing? It needs to be a spring boot 1.4.7 project.

final String name is what is causing the exception in FiltersContextAPIController.java

Here are the 4 files i'm using

// pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources-local</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <properties>
        <vehicle-inventory-clientapi.version>5.83.0-SNAPSHOT</vehicle-inventory-clientapi.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.7.RELEASE</version>
        <relativePath />
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sm360.auto.vehicleinventory</groupId>
            <artifactId>vehicle-inventory-clientapi</artifactId>
            <version>${vehicle-inventory-clientapi.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>
// FiltersContextAPIController.java

package com.sm360.auto.widgetsupplier;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
// import com.sm360.auto.vehicleinventory.clientapi.service.ColorAPIService;

@RestController
// @ComponentScan
@EnableAutoConfiguration
public class FiltersContextAPIController extends AbstractFiltersContextAPIController {

    @Autowired
    public FiltersContextAPIController(final String name) {
        super(name);
    }

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(FiltersContextAPIController.class, args);
    }

}
// AbstractFiltersContextAPIController.java

package com.sm360.auto.widgetsupplier;

// import com.sm360.auto.vehicleinventory.clientapi.service.ColorAPIService;
import org.springframework.beans.factory.annotation.Value;

import java.util.Locale;

public abstract class AbstractFiltersContextAPIController {

    // private ColorAPIService colorAPIService;
    private String name = "Ron";

    @Value("${beta.inventory.listing:false}")
    private boolean isBetaInventoryListing;

    public AbstractFiltersContextAPIController(final String name) {
        // this.colorAPIService = colorAPIService;
        // this.colorAPIService.getExteriorColor(1, Locale.CANADA);
        this.name = name;
    }

    protected String getFilters() {
        return "";
    }
}

and a resources-local folder with application.yml file

// application.yml

security.basic.enabled: false

Upvotes: 0

Views: 4833

Answers (1)

Dranna
Dranna

Reputation: 505

In Spring, a Bean is an object whose lifecycle is handled by the framework itself.

When you @Autowired in a constructor, you are telling Spring (oversimplified): please, provide this constructor with an Object. In your example you are asking Spring to inject a dependency of type String.

But you did not tell Spring where it can find such a Bean! You need to 'register' Beans using annotations like @Component, @Service, etc.

That's what is happening here. However your example hints that there is also a design/architecture issue.

You need to ask yourself what is the purpose of that string you're trying to inject and if it makes sense (to me, seeing your snippets, it does not). Usually, the injection of a String is more likely a configuration-like property using the @Value annotation.

If you need more help, please explain what is the goal of that String name

Upvotes: 3

Related Questions