【发布时间】:2019-02-18 17:32:17
【问题描述】:
我正在开发 Spring over Hibernate 项目,而我才刚刚开始。我试图有一个 SpringBootApplication 写入 MySql 一些 Location 对象。但每次我都面临一些错误。我从头到尾做了所有事情,但一切仍然无法解决。请帮帮我。 这是我的错误
APPLICATION FAILED TO START
***************************
Description:
Field service in com.project.abhishek.location.controller.LocationController
required a bean of type'com.project.abhishek.location.service.LocationService' that could not be
found.
Action:
Consider defining a bean of type'com.project.abhishek.location.service.LocationService' in your configuration.
应用类
@SpringBootApplication
@ComponentScan(basePackages= {"com.project.abhishek.location.controller"})
@EntityScan(value ="com.project.abhishek.location.entity")
@EnableJpaRepositories(basePackages = {"com.project.abhishek.location.repos"})
public class StudentApplication {
public static void main(String[] args) {
SpringApplication.run(StudentApplication.class, args);
}}
控制器
@Controller
public class LocationController {
@Autowired
private LocationService service;
@RequestMapping("/saveLoc")
public String saveLocation(@ModelAttribute("location") Location location, ModelMap modelMap) {
Location locationSaved = getService().saveLocation(location);
String msg = "Location save with id:" +locationSaved.getId();
modelMap.addAttribute("msg", msg);
return "createLocation";
}
public LocationService getService() {
return service;
}
public void setService(LocationService service) {
this.service = service;
}
服务类
@Service
public class LocationServiceImpl implements LocationService {
@Autowired
private LocationRepos locationRepos;
@Override
public Location saveLocation(Location location) {
return locationRepos.save(location);
}
@Override
public Location updateLocation(Location location) {
return locationRepos.save(location);
}
@Override
public void deleteLocation(Location location) {
locationRepos.delete(location);
}
@Override
public Location getLocation(int id) {
Optional<Location> optional = locationRepos.findById(id);
Location location = optional.get();
return location;
}
@Override
public List<Location> getAllLocation() {
return locationRepos.findAll();
}
public LocationRepos getLocationRepos() {
return locationRepos;
}
public void setLocationRepos(LocationRepos locationRepos) {
this.locationRepos = locationRepos;
}
}
实体
@Entity
public class Location {
@Id
private int id;
private String code;
private String name;
private String type;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Location [id=" + id + ", code=" + code + ", name=" + name + ", type=" + type + "]";
}
我的包结构是
com.project.abhishek.location---application classs
com.project.abhishek.location.controller---controller class
com.project.abhishek.location.entity---entity class
com.project.abhishek.location.repos---repository class
com.project.abhishek.location.service---service class
【问题讨论】:
-
把你的
@Service注解放在LocationService接口上,用@Component注解实现 -
感谢您的建议,但遇到同样的错误。
-
好的,那么可能尝试在
@ComponentScan(basePackages= {"com.project.abhishek.location.controller"})中包含com.project.abhishek.location.service -
感谢兄弟,它工作了,但我可以知道为什么它不能更早工作,因为我的服务包在应用程序包下。对于每项服务,我是否每次都必须将其包含在 @component 扫描中。
-
我在下面添加了一些解释的答案。
标签: java spring spring-mvc spring-boot spring-data-jpa