java.lang.IllegalArgumentException : Not a managed type in spring boot app

标签 java spring spring-boot

我的代码中出现以下错误

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'locationServiceImpl': Unsatisfied dependency expressed through method 'setLocationrepo' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'locationRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class 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> {

}

这是我的服务接口(interface)

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

这是入门类(class)

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

尝试一下。

关于java.lang.IllegalArgumentException : Not a managed type in spring boot app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51497953/

相关文章:

java - 在 Spring Boot 应用程序中使用 Rackspace 时,Apache jclouds java.lang.NoSuchMethodError

java - 无法在目标目录中构建 war 文件 : Failed to delete validation-api-2. 0.1.Final.jar

java - 增强的 for 循环在每次迭代的链表上执行哪些具体操作?

java - @SpringBootTest 测试类是否正确?

java NaN 和 -infinity

java - 在 Spring 中尝试 Bean 定义继承,但没有达到预期效果

java - 如何创建带参数的工厂方法?

java - 删除方法在 Spring Boot&Thymeleaf 中不起作用

java - 如何找出 JVM 对我的代码应用了哪些优化?

java - Java中用逗号或双引号对分割字符串的优雅算法