How to inject parent primitive type property through child class constructor when componentscan is enabled

I have a parent class Car & a sub class Axio. So, i'm trying to pass an argument through super("Axio") within child constructor to constructor parameter in the parent class which then assign the value into a property defined within the parent class. When i try executing the application in spring boot it throws me an exception stating

Description:

Field car in com.test.efshop.controller.HelloController required a bean of type 'com.test.efshop.Axio' that could not be found.


Action:

Consider defining a bean of type 'com.test.efshop.Axio' in your configuration.

Can anyone please tell me how to achieve this in spring boot?. My Code is as below,

// Car class

package com.test.efshop;

public class Car {

private String carName;

public String getCarName() {
    return carName;
}

public void setCarName(String carName) {
    this.carName = carName;
}

public Car(String carName) {
    this.carName = carName;
}

public String print() {
    return "Car name is : "+carName;    
}
}

//sub class of car class which is Axio

  package com.test.efshop;

    public class Axio extends Car{

    public Axio() {
        super("Axio");
    }   
    }

//main method

package com.test.efshop;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.ViewResolver;

import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

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

}

//controller class

package com.test.efshop.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.test.efshop.Axio;
import com.test.efshop.Engine;

@Controller
public class HelloController {

    @Autowired
    private Axio car;

    @RequestMapping(value = "/hello")
    public ModelAndView print() {
        return new ModelAndView("index");
    }
    //This is the method which i used to return the value of Car class
    @RequestMapping(value = "/hello2")
    @ResponseBody
    public String print2() {
        return car.print();
    }


}

1 answer

  • answered 2018-01-11 19:46 ESala

    As pvpkiran commented, you can't @Autowire a class if it's not a Spring bean.

    Option a) you convert the Axio class into a service or component.

    @Component
    public class Axio extends Car {
        public Axio() {
            super("Axio");
        }   
    }
    

    Option b) you define a bean of type Axio.

    @SpringBootApplication
    public class DemoApplication extends SpringBootServletInitializer {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
        @Bean
        public Axio myAxioBean() {
            return new Axio();
        }
    
    }