我不确定为什么Spring Boot无法识别我认为配置正确的第二个数据源。
我一直收到以下消息:...Error creating bean with name 'leadRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.ref.api.test.lead.model.LeadEntity
我为.packages("com.ref.api.test.lead.model")
设置了正确的路径,但是由于某些原因,spring-boot不会在那里检查我的实体。
PrimaryDsConfig
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
basePackages = { "com.ref.api.repository" }
)
public class PrimaryDsConfig {
@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.ref.api.model")
.persistenceUnit("foo")
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory
entityManagerFactory
) {
return new JpaTransactionManager(entityManagerFactory);
}
}
AdditionDsConfig
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "barEntityManagerFactory",
transactionManagerRef = "barTransactionManager",
basePackages = { "com.ref.api.test.lead.repository" }
)
public class AdditionDsConfig {
@Bean(name = "barDataSource")
@ConfigurationProperties(prefix = "addition.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "barEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
barEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("barDataSource") DataSource dataSource
) {
return
builder
.dataSource(dataSource)
.packages("com.ref.api.test.lead.model")
.persistenceUnit("bar")
.build();
}
@Bean(name = "barTransactionManager")
public PlatformTransactionManager barTransactionManager(
@Qualifier("barEntityManagerFactory") EntityManagerFactory
barEntityManagerFactory
) {
return new JpaTransactionManager(barEntityManagerFactory);
}
application.yml
spring:
datasource:
jdbc-url: jdbc:postgresql://blabla..
username: bla
password: bla
addition:
datasource:
jdbc-url: jdbc:postgresql://blabla2..
username: bla2
password: bla2
LeadEntity
package com.ref.api.test.lead.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "lead")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@SequenceGenerator(name = "lead_id_seq",
sequenceName = "lead_id_seq",
allocationSize = 1)
public class LeadEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "lead_id_seq")
private Long lead_id;
private String place_id;
private String name;
private String formatted_address;
private String country;
private String state;
private String city;
private String postal_code;
private String formatted_phone_number;
private String international_phone_number;
private String website;
private String vicinity;
private String rating;
private String price_level;
private String lat;
private String lng;
private String types;
private String place_url;
private String utc_offset;
private String opening_hours;
private String email;
private String logo;
}
LeadRepository
package com.ref.api.test.lead.repository;
import com.ref.api.test.lead.model.LeadEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface LeadRepository extends JpaRepository<LeadEntity, Long> {
}
ApiApplication
@SpringBootApplication
@EnableJpaRepositories(repositoryFactoryBeanClass = EntityGraphJpaRepositoryFactoryBean.class)
@EnableCaching
public class ApiApplication {
答案 0 :(得分:0)
我弄清楚了为什么spring-boot没有检测到我的第二个数据源。
因为我的@EnableJpaRepositories
上有public class ApiApplication
,所以很困惑。
当我将repositoryFactoryBeanClass = EntityGraphJpaRepositoryFactoryBean.class
移到PrimaryDsConfig
时,一切开始正常工作。
因此,请注意您有多少@EnableJpaRepositories
。