Spring Boot : required a bean named 'entityManagerFactory' that could not be found

Viewed 27125

Error:

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.

Code:

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

When I add (scanBasePackages={"nashtech.tiennguyenm3"}) after @SpringBootApplication, this error occurs.

2 Answers

You need to annotate your application class with @EnableJpaRepositories in order to have the @Repository bean created.

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

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

If all your repositories/daos are under the package nashtech.tiennguyenm3.dao then you can add:

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

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

If they are in different packages then you can add this:

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