Home > OS >  "Bean type int could not be found" error, how can I solve it?
"Bean type int could not be found" error, how can I solve it?

Time:12-14

I made a TempRequest class and registed as a bean

The code below is TempRequest class.

package com.wook.model.dto;

import org.springframework.stereotype.Component;

@Component
public class TempRequest {

    private int x;
    private int y;
    private String baseDate;
    private String tempKey; 
    
    public TempRequest(int x, int y, String baseDate, String tempKey) {
        super();
        this.x = x;
        this.y = y;
        this.baseDate = baseDate;
        this.tempKey = tempKey;
    }

    public String getTempKey() {
        return tempKey;
    }

    public void setTempKey(String tempKey) {
        this.tempKey = tempKey;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public String getBaseDate() {
        return baseDate;
    }

    public void setBaseDate(String baseDate) {
        this.baseDate = baseDate;
    }

    @Override
    public String toString() {
        return "TempRequest [x="   x   ", y="   y   ", baseDate="   baseDate   ", tempKey="   tempKey   "]";
    }
    
}

To use TempRequest class I Autowired at ReadWeatherController class

ReadWeatherController Class

package com.wook.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wook.model.dto.TempRequest;

@RestController
public class ReadWeatherController {

    private TempRequest tr;

    public ReadWeatherController(TempRequest tr) {
        super();
        this.tr = tr;
    }


    //이제 여기다가 API를 만들면됨
    @GetMapping("/todayMaxMin")
    public String readWeather(
            @RequestParam(value="x")int x,
            @RequestParam(value="y")int y,
            @RequestParam(value="baseDate")String baseDate) {
        
        String tempKey = baseDate String.valueOf(x) String.valueOf(y);
        
        tr = new TempRequest(x,y,baseDate,tempKey);
        
        return tr.toString();
    }
    
}

And to use these beans I added a packges with using scanBasePackages in main class. Application Class

package com.wook.run;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication(scanBasePackages= {"com.wook.controller","com.wook.model","com.wook.service"})
public class Application {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SpringApplication.run(Application.class, args);
    }

}

However with this code I get an error like this.


APPLICATION FAILED TO START


Description:

Parameter 0 of constructor in com.wook.model.dto.TempRequest required a bean of type 'int' that could not be found.

Action:

Consider defining a bean of type 'int' in your configuration.

How can I solve this problem?

CodePudding user response:

The problem is that Spring is trying to create a bean for TempRequest (and the only constructor available requires 2 ints and 2 Strings). You actually don't need TempRequest as a Spring-managed Bean, so remove @Component annotation:

public class TempRequest {
    (...)
}

Then you also need to remove TempRequest as a property in ReadWeatherController because you actually don't need it:

@RestController
public class ReadWeatherController {

    //이제 여기다가 API를 만들면됨
    @GetMapping("/todayMaxMin")
    public String readWeather(
            @RequestParam(value="x") int x,
            @RequestParam(value="y") int y,
            @RequestParam(value="baseDate") String baseDate) {
        
        String tempKey = baseDate String.valueOf(x)   String.valueOf(y);
        
        TempRequest tr = new TempRequest(x, y, baseDate, tempKey);
        
        return tr.toString();
    }
}

As a side note, please consider moving your main class Application to the package com.wook so that you avoid the need of adding all the packages in @SpringBootApplication(scanBasePackages= {"com.wook.controller","com.wook.model","com.wook.service"}):

package com.wook;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@SpringBootApplication encapsulates @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations with their default attributes. The default value for @ComponentScan means that all the sub packages on the package the @ComponentScan is used are scanned. That is why it is usually a good practice to include the main class in the base package of the project.

CodePudding user response:

Do you really need a class-level variable TempRequest tr in ReadWeatherController? As I see no use of it, rather have a method level variable inside the ReadWeatherController.readWeather() method.

What's happening is since you have TempRequest tr as a parameter in your ReadWeatherController constructor, spring is trying to autowire TempRequest and TempRequest doesn't have a no-args constructor and the default all args constructor has int and string parameter in it. Since the first param in TempRequest constructor is type int and spring can't find bean type of int, spring is throwing Consider defining a bean of type 'int' in your configuration error. Change the controller to the following

@RestController
public class ReadWeatherController {


    //이제 여기다가 API를 만들면됨
    @GetMapping("/todayMaxMin")
    public String readWeather(
            @RequestParam(value="x")int x,
            @RequestParam(value="y")int y,
            @RequestParam(value="baseDate")String baseDate) {
        
        String tempKey = baseDate String.valueOf(x) String.valueOf(y);
        
        TempRequest tr = new TempRequest(x,y,baseDate,tempKey);
        
        return tr.toString();
    }
    
}

This should solve your problem.

CodePudding user response:

You have a @Component with a constructor as defined below:

@Component
public class TempRequest {
...    
    public TempRequest(int x, int y, String baseDate, String tempKey) {
        ...
    }

The error message is saying it can't find beans of type int as declared in the component's constructor.

If this TempRequest is intentionally a bean (component) I would suggest refactoring as follows:

@Component
public class TempRequest {
    ...    
    public TempRequest(TempRequestConfig trc) {
        ...
    }
    ...
}

And then you can define a TempRequestConfig bean:

@Component
public class TempRequestConfig {
    public int getX() { ... }
    public int getY() { ... }
    public String getBaseDate() { ... }
    public String getTempKey() { ... }
}

This way you can inject the config bean into your temp request bean with Spring's dependency injection.

  • Related