简体   繁体   中英

Why do I get the "Not a managed type" error?

My main project (A) contains the structure: xyz

My project depends on a jar file (B) with a.b.c package structure

Project B contains the entity that I need in project A.

In project A , I specify the following.

@EnableScheduling
@ComponentScan(
    basePackages = {"x.y.z", "a.b.c"},
    includeFilters = {
            @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Indexed.class})
    })
@EntityScan(basePackages = {"x.y.z", "a.b.c"})
@SpringBootApplication
public class Application extends SpringBootServletInitializer { ...

My entity in Project B : abc.models.MyEntity;

import javax.persistence.*;

@Entity
@Table(
    name = "my_entity"
)
@EntityListeners({AuditingEntityListener.class})
public class MyEntity { ...

But every time I start, I get the same error "Not a managed type: abc.models.MyEntity" .

I have tried different configurations, specify different paths, use import annotation, but nothing works.

@Configuration
@Import({MyEntity.class})
... Not Work ...
@EntityScan(basePackages = {"x.y.z.*", "a.b.c.*"})
... Not Work ...
@EntityScan(basePackages = {"x.y.z.models.*", "a.b.c.models*"})
... Not Work ...    
@ComponentScan(
    basePackages = {"x.y.z.*", "a.b.c.*"},
    includeFilters = {
            @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Indexed.class})
    })

It's always the same mistake. But dependencies from Project A are loaded without problems.

I also have other projects C DE, which also depend on jar project B , it works fine in them with the same configuration.

So. The problem was this dependency.

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-indexer</artifactId>
    <version>5.3.21</version>
</dependency>

The beans in project B (abc package) simply did not get into the META-INF/spring.components file.

You either need to remove this dependency, or generate your own META-INF/spring.components file for project B .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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