嗨,我正在Spring Boot中使用Java配置进行JPA和Hibernate配置,但在这个错误下我停留了几个小时。 即使我已将UserRepository声明为bean,它仍然无法获取该存储库。
package com.repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {}
我正在使用此存储库的服务类:
package com.service;
@Service
public class AppointmentPaymentServiceImpl implements AppointmentPaymentService {
@Autowired
private UserRepository userRepository;
......
......
}
我的数据库配置:
package com.config;
@Configuration
@PropertySource(value = { "classpath:application.properties" })
@EnableTransactionManagement
@EnableJpaRepositories("com.repository.*")
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
public class DBConfig {
@Value("${spring.datasource.driver-class-name}")
public String driver;
@Value("${spring.datasource.url}")
public String url;
@Value("${spring.datasource.username}")
public String username;
@Value("${spring.datasource.password}")
public String password;
@Value("${spring.jpa.properties.hibernate.dialect}")
public String dialect;
@Value("${spring.jpa.hibernate.ddl-auto}")
public String ddl;
@Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "sessionFactory")
public LocalSessionFactoryBean hibernateSessionFactory(DataSource dataSource) {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "com.model.*" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
HibernateTransactionManager transactionManagerHib(SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory);
return transactionManager;
}
/*@Bean
@Qualifier(value = "entityManager")
public EntityManager entityManager(EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}*/
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabase(Database.MYSQL);
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan("com.model.*");
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(hibernateProperties());
return em;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
Properties hibernateProperties() {
return new Properties() {
{
setProperty("hibernate.hbm2ddl.auto", ddl);
setProperty("hibernate.connection.useUnicode", "true");
setProperty("spring.jpa.hibernate.ddl-auto", ddl);
setProperty("hibernate.dialect", dialect);
setProperty("spring.jpa.properties.hibernate.dialect", dialect);
setProperty("hibernate.globally_quoted_identifiers", "true");
setProperty("hibernate.connection.CharSet", "utf8mb4");
setProperty("hibernate.connection.characterEncoding", "utf8");
}
};
}
}
这是我的主要课程:
package com;
@SpringBootApplication
@ComponentScan(basePackages = { "com.*"})
@EnableCaching
@Configuration
@PropertySource({"classpath:logging.properties"})
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
如果我仅使用spring数据jpa,则我的pom.xml包含hibernate和jpa的这些依赖项,然后hibernate-core 5.0.12.Final默认情况下是导入的,我不想这样做:
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
.
.
.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.3.Final</version>
</dependency>
</dependencies>
错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userRepository in com.service.AppointmentPaymentServiceImpl required a bean of type 'com.repository.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'com.repository.UserRepository' in your configuration.
我的用户实体:
package com.model;
@Entity
@Table(name = "USER")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@NotNull
@Column(name = "city_id")
private Long cityId;
@Column(name = "name")
private String name;
@Column(name = "age")
private int age;
@Column(name = "locality")
private String locality;
@Column(name = "gender")
private String gender;
}
答案 0 :(得分:3)
在主spring boot应用程序类中添加以下注释解决了我的问题:
@ComponentScan(“ com.myapp.repositories”)
答案 1 :(得分:0)
您的@EnableJpaRepositories
注释错误。您无需定义以这种方式在哪里找到存储库的软件包。
假定它们所驻留的包称为:
foo.somepackage.repositories
,则注释应为@EnableJpaRepositories("foo.somepackage.repositories")
。
尝试更正注释,以正确正确地扫描您的存储库软件包,以将其带入上下文。
答案 2 :(得分:0)
我认为您的代码必须在<insert service here>
下组织,并且Book
应该有效。
例如:String[]
,String[] entries =line.split(",");
Book book = new Book(entries[0], entries[1], Integer.parseInt(entries[2].trim()));
答案 3 :(得分:0)
只需将AppointmentPaymentServiceImpl
和UserRepository
放在同一包中,对我来说,它们就是com.
答案 4 :(得分:0)
我遇到了同样的问题。我只是将存储库从我的工作项目复制到一个新项目,并且在考虑定义类型为 com.[packagename].repository.[repository-name] 的 bean 时遇到了同样的错误。以上没有对我有用,后来我做了以下事情:
去掉了POM文件中JPA依赖的版本(去掉了版本标签)
我使用的是 Java 15,当我从 start.spring.io 创建 SpringBoot 项目时,只有 11 和 16 个 fava 版本。所以我为我的项目选择了 11。我也在 POM 文件中更改了它
15(之前是11)现在我删除了主文件中的 @ComponentScan 注释,这也是早期对我有用的解决方案。
只需使缓存无效并重新启动
输入 mvn clean install
mvn clean spring-boot:run 希望这对你也有用