Spring Boot:需要一个名为'entityManagerFactory'的bean无法找到

时间:2017-09-24 15:44:17

标签: java spring spring-boot

错误:

  

说明

     

nashtech.tiennguyenm3.config.DataSeedingListener中的字段userRepository需要一个无法找到的类型为“nashtech.tiennguyenm3.dao.IUserRepository”的bean。

     

动作:

     

考虑在配置中定义类型为'nashtech.tiennguyenm3.dao.IUserRepository'的bean。

代码:

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);
}

application.properties

# ===============================
# 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”})时,会发生此错误。

2 个答案:

答案 0 :(得分:1)

您需要使用@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);
    }
}

答案 1 :(得分:0)

如果所有存储库/ daos都在包nashtech.tiennguyenm3.dao下 然后你可以添加:

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

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

如果他们在不同的包中,那么您可以添加:

@SpringBootApplication(scanBasePackages={"nashtech.tiennguyenm3"})
@EnableJpaRepositories(basePackages = { "nashtech.tiennguyenm3.dao", "the.other.package"})
public class MusicStoreApplication {

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