1 回答
TA贡献1831条经验 获得超9个赞
您可以使用以下解决方案。将主类更改为以下代码
@SpringBootApplication
public class MyrestapplicationApplication {
public static void main(String[] args) {
SpringApplication.run(MyrestapplicationApplication.class, args);
}
}
然后为您的配置创建一个单独的类。以及摆脱紧密耦合的架构。
@Configuration
@EntityScan("com.skilldistillery.edgemarketing.entities")
@EnableJpaRepositories("com.skilldistillery.myRest.repositories")
public class BusinessConfig {
@Bean
public HouseService houseService(final HouseRepo houseRepo){
return new HouseServiceImpl(houseRepo);
}
}
然后,您的控制器将更改为以下内容。利用依赖注入
@RestController
@RequestMapping("api")
@CrossOrigin({ "*", "http://localhost:4200" })
public class HouseController {
private HouseService houseServ;
public HouseController(HouseService houseServ) {
this.houseServ = houseServ;
}
@GetMapping(value = "index/{id}",produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)
public House show(@PathVariable("id") Integer id) {
return houseServ.show(id);
}
}
家庭服务简单也应该实施家庭服务
public class HouseServiceImpl implements HouseService{
private HouseRepo hRepo;
public HouseServiceImpl(HouseRepo hRepo) {
this.hRepo = hRepo;
}
@Override
public List<House> index() {
return null;
}
public House show(Integer id) {
Optional<House> opt = hRepo.findById(id);
House house = new House();
if (opt.isPresent()) {
house = opt.get();
}
return house;
}
}
*注意 - 不要忘记删除以下配置,因为它们现在在类中处理。可以在类中定义更多 Bean@Autowired,@RepositoryBusinessConfigBusinessConfig
添加回答
举报