提问Could not autowire. No beans of ‘XXX’ type found.

自定义一个xxxAutoConfiguration的Service,打包好放到另一个项目调用,但是提示报错,不知道为啥?如标题,直接点进去是能看到依赖的。

直接看代码吧!包引用这些都没有问题,提示HelloService不是bean类型。

调用的地方:

@RestController
public class HelloController {

@Autowired
HelloService helloService;

@RequestMapping("/sayhello")
public String sayHello(){
return helloService.sayHello("test");
}
}

错误如下图:

微信截图_20220217213211.png

 

下面三个类是自定义的bean

@Configuration
@ConditionalOnMissingBean(HelloService.class)
@EnableConfigurationProperties(HelloProperties.class)//默认放容器中
public class HelloServiceAutoConfiguration {

@Bean
public HelloService helloService(){
return new HelloService();
}
}

public class HelloService {

@Autowired
HelloProperties helloProperties;

public String sayHello(String userName){
return "这是打招呼:"+ helloProperties.getPrefix() + userName + helloProperties.getSuffix();
}
}

@ConfigurationProperties("com.hello")
public class HelloProperties {
private String prefix;
private String suffix;

public void setPrefix(String prefix) {
this.prefix = prefix;
}

public void setSuffix(String suffix) {
this.suffix = suffix;
}

public String getPrefix() {
return prefix;
}

public String getSuffix() {
return suffix;
}
}
请登录后发表评论