1 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊
您可以使用以下解決方案。將主類更改為以下代碼
@SpringBootApplication
public class MyrestapplicationApplication {
public static void main(String[] args) {
SpringApplication.run(MyrestapplicationApplication.class, args);
}
}
然后為您的配置創(chuàng)建一個(gè)單獨(dú)的類。以及擺脫緊密耦合的架構(gòu)。
@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);
}
}
然后,您的控制器將更改為以下內(nèi)容。利用依賴注入
@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);
}
}
家庭服務(wù)簡(jiǎn)單也應(yīng)該實(shí)施家庭服務(wù)
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;
}
}
*注意 - 不要忘記刪除以下配置,因?yàn)樗鼈儸F(xiàn)在在類中處理。可以在類中定義更多 Bean@Autowired,@RepositoryBusinessConfigBusinessConfig
添加回答
舉報(bào)