繁体   English   中英

java.lang.IllegalArgumentException:在Spring Boot应用程序中不是托管类型

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM