![](/img/trans.png)
[英]Spring - AWS (no spring boot): java.lang.IllegalArgumentException: Not a managed type
[英]java.lang.IllegalArgumentException: Not a managed type in spring boot app
我的代碼中出現以下錯誤
org.springframework.beans.factory.UnsatisfiedDependencyException:創建名稱為“ locationServiceImpl”的bean時出錯:通過方法“ setLocationrepo”參數0表示的不滿足的依賴關系; 嵌套異常是org.springframework.beans.factory.BeanCreationException:創建名稱為“ locationRepository”的bean時出錯:調用init方法失敗; 嵌套異常是java.lang.IllegalArgumentException:不是托管類型:com.logan.location.entities.Location類
這是我的存儲庫接口
package com.logan.location.repos;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.logan.location.entities.Location;
@Repository
public interface LocationRepository extends JpaRepository<Location, Integer> {
}
這是我的服務界面
package com.logan.location.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.logan.location.entities.Location;
@Service
public interface LocationService {
Location saveLocation(Location location);
Location updateLocation(Location location);
void deleteLocation(Location location);
Location getLocationById(int id);
List<Location> getAllLocations();
}
這是我的服務
package com.logan.location.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.logan.location.entities.Location;
import com.logan.location.repos.LocationRepository;
@Service
public class LocationServiceImpl implements LocationService {
private LocationRepository locationrepo;
@Autowired
public void setLocationrepo(LocationRepository locationrepo) {
this.locationrepo = locationrepo;
}
public Location saveLocation(Location location) {
// TODO Auto-generated method stub
return locationrepo.save(location);
}
public Location updateLocation(Location location) {
// TODO Auto-generated method stub
return locationrepo.save(location);
}
public void deleteLocation(Location location) {
// TODO Auto-generated method stub
locationrepo.delete(location);
}
public Location getLocationById(int id) {
// TODO Auto-generated method stub
return locationrepo.findById(id).get();
}
public List<Location> getAllLocations() {
// TODO Auto-generated method stub
return locationrepo.findAll();
}
public LocationRepository getLocationrepo() {
return locationrepo;
}
}
這是我的實體課
package com.logan.location.entities;
import javax.persistence.Entity;
import javax.persistence.Id;
@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;
}
}
這是入門班
package com.logan.location;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@EntityScan("com.logan.location.entities")
@EnableJpaRepositories(basePackages = {"com.logan.location.repos"})
@SpringBootApplication
public class LocationApplication {
public static void main(String[] args) {
SpringApplication.run(LocationApplication.class, args);
}
}
它顯示我的位置實體類不受管理,我已經嘗試了各種答案,但沒有用,有幫助嗎?
在LocationRepository之前刪除@Repository
批注。 無需添加。
public interface LocationRepository extends JpaRepository<Location, Integer> {
}
還要刪除@EntityScan("com.logan.location.entities")
和@EnableJpaRepositories(basePackages = {"com.logan.location.repos"})
@SpringBootApplication
public class LocationApplication {
public static void main(String[] args) {
SpringApplication.run(LocationApplication.class, args);
}
}
添加位置存儲庫bean,如下所示:
@Service
public class LocationServiceImpl implements LocationService {
private LocationRepository locationrepo;
public LocationServiceImpl(LocationRepository locationrepo){
this.locationrepo = locationrepo;
}
}
試試這個。
請在LocationApplication
類中添加@Configuration
和@ComponentScan
批注。 同樣,您也缺少實體類中的@Column
注釋,請正確地自動連接服務。
@Autowired
private LocationRepository locationrepo;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.