gpt4 book ai didi

java - Spring 启动 : required a bean named 'entityManagerFactory' that could not be found

转载 作者:搜寻专家 更新时间:2023-11-01 03:32:26 24 4
gpt4 key购买 nike

错误:

Description:

Field userRepository in nashtech.tiennguyenm3.config.DataSeedingListener required a bean of type 'nashtech.tiennguyenm3.dao.IUserRepository' that could not be found.

Action:

Consider defining a bean of type 'nashtech.tiennguyenm3.dao.IUserRepository' in your configuration.

代码:

package nashtech.tiennguyenm3.musicstore;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages={"nashtech.tiennguyenm3"})
public class MusicStoreApplication {

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

DataSeedingListener.java

package nashtech.tiennguyenm3.config;

import java.util.HashSet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import nashtech.tiennguyenm3.dao.IRoleRepository;
import nashtech.tiennguyenm3.dao.IUserRepository;
import nashtech.tiennguyenm3.model.Role;
import nashtech.tiennguyenm3.model.User;

@Component
public class DataSeedingListener implements ApplicationListener<ContextRefreshedEvent> {

@Autowired
private IUserRepository userRepository;

@Autowired
private IRoleRepository roleRepository;

@Autowired
private PasswordEncoder passwordEncoder;

@Override
public void onApplicationEvent(ContextRefreshedEvent arg0) {
// Roles
if (roleRepository.findByName("ROLE_ADMIN") == null) {
roleRepository.save(new Role("ROLE_ADMIN"));
}

if (roleRepository.findByName("ROLE_MEMBER") == null) {
roleRepository.save(new Role("ROLE_MEMBER"));
}

// Admin account
if (userRepository.findByEmail("admin@gmail.com") == null) {
User admin = new User();
admin.setEmail("admin@gmail.com");
admin.setPassword(passwordEncoder.encode("123456"));
HashSet<Role> roles = new HashSet<>();
roles.add(roleRepository.findByName("ROLE_ADMIN"));
roles.add(roleRepository.findByName("ROLE_MEMBER"));
admin.setRoles(roles);
userRepository.save(admin);
}

// Member account
if (userRepository.findByEmail("member@gmail.com") == null) {
User user = new User();
user.setEmail("member@gmail.com");
user.setPassword(passwordEncoder.encode("123456"));
HashSet<Role> roles = new HashSet<>();
roles.add(roleRepository.findByName("ROLE_MEMBER"));
user.setRoles(roles);
userRepository.save(user);
}
}
}

IUserReponsitory.java

package nashtech.tiennguyenm3.dao;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import nashtech.tiennguyenm3.model.User;

@Repository
public interface IUserRepository extends CrudRepository<User, Integer> {
public User findByEmail(String email);
}

应用程序属性

# ===============================
# DATASOURCE
# ===============================

# Set here configurations for the database connection

# Connection url for the database "mycontact"
spring.datasource.url=jdbc:mysql://localhost:8181/db_musicstore

# MySQL username and password
spring.datasource.username=root
spring.datasource.password=

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.dbcp.test-while-idle=true
spring.datasource.dbcp.validation-query=SELECT 1

# ===============================
# JPA / HIBERNATE
# ===============================

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager).

# Show or not log for each sql query
spring.jpa.show-sql=true

# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto=update

# Naming strategy
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy

# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

security.basic.enabled=false

当我在@SpringBootApplication 之后添加(scanBasePackages={"nashtech.tiennguyenm3"}) 时,会出现此错误。

最佳答案

您需要使用 @EnableJpaRepositories 注释您的应用程序类,以便创建 @Repository bean。

@SpringBootApplication(scanBasePackages={"nashtech.tiennguyenm3"})
@EnableJpaRepositories(basePackageClasses = IUserRepository.class) // <-- add this
public class MusicStoreApplication {

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

关于java - Spring 启动 : required a bean named 'entityManagerFactory' that could not be found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46391867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com