无法自动装配字段:JpaRepository,Spring

时间:2017-03-03 20:44:35

标签: java spring hibernate

我正在尝试在服务中自动发送回复。但它会抛出BeanCreationException。嵌套异常是

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#208fa9ab' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#208fa9ab': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined

这是我的hibernate配置文件

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages ="nfy.sltraveller.repository")
@ComponentScan({ "nfy.sltraveller.configuration" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfig {

@Autowired
private Environment environment;

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan(new String[] { "nfy.sltraveller.model"        });
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
 }

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
    dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
    dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
    dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
    return dataSource;
}

private Properties hibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
    properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
    properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
    return properties;        
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
   HibernateTransactionManager txManager = new HibernateTransactionManager();
   txManager.setSessionFactory(s);
   return txManager;
}
}

这是服务,

@Service
public class InitDbService {

@Autowired
private UserRepository userRepository;

@Autowired
private RoleRepository roleRepository;

@PostConstruct
public void init(){
    Role roleUser = new Role();
    roleUser.setName("ROLE_USER");
    roleRepository.save(roleUser);
}
}

这是存储库,

package nfy.sltraveller.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import nfy.sltraveller.model.Role;


public interface RoleRepository extends JpaRepository<Role, Integer>{

}

我也试过使用@Repository注释。但它没有用。 为什么Spring无法识别存储库?

1 个答案:

答案 0 :(得分:1)

重新添加@Repository注释并将存储库包添加到扫描列表

@ComponentScan({ "nfy.sltraveller.configuration", "nfy.sltraveller.repository" })