当前位置: 首页 > 知识库问答 >
问题:

IllegalArgumentException:不是托管类型:class

严曜文
2023-03-14

我正在创建两个不同的数据源的Spring引导应用程序。我已经为单独的数据库创建了配置文件。对于每个数据库,实体在不同的包中,模型在不同的包中。当我跑的时候

mvn清洁安装

它正确地创建数据库和所有表。但是在创建存储库时总是失败。下面我提供所有必要的细节:

主类

@SpringBootApplication
@EnableAutoConfiguration
public class Demo {

    public static void main(String[] args) {
        SpringApplication.run(Demo.class, args);
    }
}

Application.Properties

spring.application.name= nyota-plateform
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.database= MYSQL
spring.jpa.generate-ddl= true
spring.jpa.hibernate.ddl-auto= update
spring.jpa.properties.hibernate.implicit_naming_strategy= org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl


spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.jdbc-url= jdbc:mysql://localhost:3306/DB1?useUnicode=true&createDatabaseIfNotExist=true&useSSL=false
spring.datasource.username= *
spring.datasource.password= *
spring.datasource.dbcp2.max-idle= 10000

authdb.datasource.driver-class-name=com.mysql.jdbc.Driver
authdb.datasource.jdbc-url= jdbc:mysql://localhost:3306/DB2?useUnicode=true&createDatabaseIfNotExist=true&useSSL=false
authdb.datasource.username= *
authdb.datasource.password= *
authdb.datasource.dbcp2.max-idle= 10000
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "com.test.demo.repo.db1" })
public class DatabaseConfiguration {

    @Primary
    @Bean(name = "dataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
            @Qualifier("dataSource") DataSource dataSource) {
        return builder.dataSource(dataSource).packages("com.test.demo.model.db1").persistenceUnit("main").build();
    }

    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(
            @Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }

}
@Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(basePackages = { "com.test.demo.repo.db2" })
    public class DatabaseConfiguration {

    @Primary
    @Bean(name = "dataSource2")
    @ConfigurationProperties(prefix = "authdb.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "entityManagerFactory2")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
            @Qualifier("dataSource2") DataSource dataSource) {
        return builder.dataSource(dataSource).packages("com.test.demo.model.db2").persistenceUnit("main").build();
    }

    @Bean(name = "transactionManager2")
    public PlatformTransactionManager transactionManager(
            @Qualifier("entityManagerFactory2") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}
@Entity
public class AuthUser {

    @Id
    @GeneratedValue
    private Long id;

    @Column
    private String name;

    @Column(nullable=false,unique=true)
    private String username;

    @Column(nullable=false)
    private String password;

    @Column(nullable=false)
    private String role;

    @Column
    private String uniqueId = UUID.randomUUID().toString();


    public AuthUser(String name, String username, String password, String role) {
        super();
        this.name = name;
        this.username = username;
        this.password = password;
        this.role = role;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String email) {
        this.username = email;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getUniqueId() {
        return uniqueId;
    }

    public void setUniqueId(String uniqueId) {
        this.uniqueId = uniqueId;
    }
}
@Repository
public interface AuthUserRepository extends JpaRepository<AuthUser, Long> {

    public AuthUser findByUsernameAndPassword(String email,String password);
}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authUserRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.test.demo.model.auth.AuthUser
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:579) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:741) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at com.test.demo.Demo.main(Demo.java:14) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:496) [spring-boot-maven-plugin-2.0.2.RELEASE.jar:2.0.2.RELEASE]

有人能帮忙吗?我缺少了什么参数,或者我在这里做错了什么?提前感谢!

共有1个答案

长孙燕七
2023-03-14

我在使用不同的数据源时遇到了类似的问题。使用@qualifier注释解决了这个问题。在这些配置中,您可以使用@qualifier(“db1”)注释bean,并在存储库中使用@transactional(TransactionManager=“db1”)

 类似资料:
  • UnsatisfiedDependencyException:创建名为“Location ServiceImpl”的bean时出错:通过方法“Set LocationRepo”参数0表示未满足的依赖关系;嵌套异常是org.springframework.beans.factory.beanCreationException:创建名为“location repository”的bean时出错:调用i

  • 我正在使用和。它在这里 我有一个像这样的域名。而且,似乎不推荐使用注释,所以我使用了。 类如下所示 使用该类的如下所示 但当我启动应用程序时,我得到了错误 问题出在哪里?

  • 问题内容: 我使用Spring boot + JPA,启动服务时遇到问题。 这是Application.java文件, 我使用UCp进行连接池,下面是DataSource配置, UserDetailsService Implementation, Service layer implementation, The repository class, @Repository Entity class

  • 我使用spring boot+JPA,在启动服务时遇到了一个问题。 下面是application.java文件, 我使用UCp进行连接池,数据源配置如下所示, UserDetailsService实现, 服务层实现, repository类, 实体类, WebSecurityConfig类, 这些包如下所示, 类在-中 类在-中 实体类在- 服务类在- 控制器在- 存储库类在-中 在-中 谢谢

  • 我正在尝试运行一个非常简单的Spring Boot应用程序,但是我得到以下错误消息: 下面是我的主要应用程序类代码: 下面是Todo类: 我尝试将相应的包名添加到我的@ComponentScan注释中,并尝试将@Component注释添加到我的Todo类中,但都没有奏效。

  • 我对spring data JPA有意见。我克隆了这个存储库https://github.com/royclarkson/spring-rest-service-oauth,它工作得非常好。然后我想用real database替换import.sql,并想将它与Postgres连接起来。我遵循了下一个教程:http://devcrumb.com/hibernate/spring-data-jpa-