【问题标题】:java.lang.IllegalArgumentException: Not a managed type in spring boot appjava.lang.IllegalArgumentException:不是 Spring Boot 应用程序中的托管类型
【发布时间】:2018-07-24 11:54:33
【问题描述】:

我的代码中出现以下错误

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();
}

这是我的 serviceImpl

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);
    }
}

它显示我的位置实体类是非托管的,我尝试了各种答案但它不起作用,有什么帮助吗??

【问题讨论】:

  • 删除@EntityScan并删除@EnableJpaRepositories。
  • 不工作 @M.Deinum
  • 你自己在@Configuration上课吗?
  • 我没找到你?? @M.Deinum
  • 你有没有用@Configuration注解的类

标签: java spring spring-boot


【解决方案1】:

删除 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;
    }
}

试试这个。

【讨论】:

  • LocationRepository 是通过 setter 注入的,非常好。最好使用构造函数注入而不是字段(或设置器)注入,因为它是必需的依赖项。
  • 是的,它会被构造函数很好地自动装配。代码更新。
  • 你的答案不是答案。您没有首先解释为什么应用程序无法运行,尽管您的代码正在运行,但您主要转换了 @M.Deinum 和我在 cmets 中解释的内容。充分利用@SpringBootApplication中包含的@EnableAutoConfiguration注解的全部功能是很好的,但是您在这里所做的主要是优化,而不是解决方案
【解决方案2】:

请在您的LocationApplication 类中添加@Configuration 和@ComponentScan 注释。而且您还缺少实体类中的@Column 注释,请正确自动连接服务。

@Autowired 
private LocationRepository locationrepo;

【讨论】:

  • @SpringBootApplication 包括@Configuration 和@ComponentScan。旁边的存储库是通过设置器自动连接的。 @Column 注释不是强制性的。
  • 除了@m-deinum所说的,@Column注解不是必须的。
猜你喜欢
  • 2019-12-30
  • 2018-02-25
  • 2015-04-24
  • 1970-01-01
  • 2019-04-28
  • 2017-06-21
  • 2018-09-05
  • 2022-08-07
  • 1970-01-01
相关资源
最近更新 更多