obolen_an
obolen_an

Reputation: 61

Spring does not find bean

I get the following error:

Field delegate in package.rest.api.MyApiController required a bean of type 'package.rest.api.MyApiDelegate' that could not be found.

I've tried to add annotation @ComponentScan("package.rest") to the main class, but it didn't help.

package package;
@SpringBootApplication
@Import(SpecificationConf.class)
//@ComponentScan("package.rest")
public class MyApp extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}
package package.rest.api;

@Controller
@RequestMapping(...)
public class MyApiController implements MyApi {

    @Autowired
    @Qualifier("myService")
    private MyApiDelegate delegate;

}
package package.rest;

@Service("myService")
public class MyApiDelegateImpl implements MyApiDelegate {
...
}
package package.rest.api;

public interface MyApiDelegate {
...
}

Can that problem happen due to incorrect configuration of the Application context or an inappropriate maven profile? I've tried to move classes to the same package, but it also didn't help.

Upvotes: 0

Views: 523

Answers (1)

João Dias
João Dias

Reputation: 17460

Given that you are building a JAR file and not a WAR file, please try dropping SpringBootServletInitializer from your main class as follows:

package package;

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

Upvotes: 1

Related Questions