• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Spring Boot 2.0.0.M4需要找不到名为'entityManagerFactory'的bean

用户头像
it1352
帮助1

问题说明

在spring-boot 2.0.0.M4的版本中,我遇到以下问题:

With the version of spring-boot 2.0.0.M4 I have this issue:


    Description:

Field userRepository in 
webroot.websrv.auth.service.JwtUserDetailsServiceImpl required a bean 
named 'entityManagerFactory' that could not be found.


Action:

Consider defining a bean named 'entityManagerFactory' in your 
configuration.

[WARNING] 
 java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at ...
Caused by: org.springframework.context.ApplicationContextException: 
Unable to start web server; nested exception is 
org.springframework.boot.web.server.WebServerException: Unable to start 
embedded Tomcat

Caused by: 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error 
creating bean with name 'jwtUserDetailsServiceImpl': Unsatisfied 
dependency expressed through method 'setUserRepository' parameter 0; 
nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating 
bean with name 'userRepository': Cannot create inner bean '(inner 
bean)#770f146b' 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)#770f146b': Cannot resolve reference to 
bean 'entityManagerFactory' while setting constructor argument; nested 
exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No 
bean named 'entityManagerFactory' available

Spring Boot应用程序启动:

Spring boot application start:


            package webroot.websrv;

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

            @SpringBootApplication
            public class WebcliApplication {

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

已实现的Jwt服务JwtUserDetailsServiceImpl:

The implemented Jwt service JwtUserDetailsServiceImpl:


        @Service

        public class JwtUserDetailsServiceImpl implements 
    UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    /**
     * Injects UserRepository instance
     * @param userRepository to inject
      */
    @Autowired
   public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

这里是用户存储库:

package webroot.websrv.auth.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import webroot.websrv.auth.entity.User;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

/**
 * Finds user by email
 * @param email to look for
 * @return user by given email
 */
User findByEmail(String email);

/**
 * Finds user by name
 * @param name to look for
 * @return user by given name
 */
User findByName(String name);
 }

我已经多次看到此问题的报告,但是在大多数情况下,对于给定的解决方案我都可以接受.

I have saw several time this issue reported here but most of the cases I have it OK for the given solutions.

我是否必须为实体管理器显式定义一个bean?应该自动注入否吗?

Do I have to explicit define a bean to the entity manager ? It should be automatically injected no ?

我为UserRepository添加了类,它扩展了JpaRepository.

I added the class for the UserRepository it extends a JpaRepository.

谢谢

布鲁诺

正确答案

#1

我找到了解决方案,有必要实现JpaConfiguration:

I found the solution, it was necessary to implement a JpaConfiguration:

 @Configuration
@EnableJpaRepositories(basePackages = "webroot.webserv",
    entityManagerFactoryRef = "entityManagerFactory",
    transactionManagerRef = "transactionManager")
@EnableTransactionManagement
public class JpaConfiguration {

@Autowired
private Environment environment;

@Value("${datasource.sampleapp.maxPoolSize:10}")
private int maxPoolSize;

/*
 * Populate SpringBoot DataSourceProperties object directly from 
application.yml 
 * based on prefix.Thanks to .yml, Hierachical data is mapped out of 
the box with matching-name
 * properties of DataSourceProperties object].
 */
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSourceProperties dataSourceProperties(){
    return new DataSourceProperties();
}

/*
 * Configure HikariCP pooled DataSource.
 */
@Bean
public DataSource dataSource() {
    DataSourceProperties dataSourceProperties = dataSourceProperties();
        HikariDataSource dataSource = (HikariDataSource) 
org.springframework.boot.jdbc.DataSourceBuilder
                .create(dataSourceProperties.getClassLoader())

.driverClassName(dataSourceProperties.getDriverClassName())
                .url(dataSourceProperties.getUrl())
                .username(dataSourceProperties.getUsername())
                .password(dataSourceProperties.getPassword())
                .type(HikariDataSource.class)
                .build();
        dataSource.setMaximumPoolSize(maxPoolSize);
        return dataSource;
}

/*
 * Entity Manager Factory setup.
 */
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() 
throws NamingException {
    LocalContainerEntityManagerFactoryBean factoryBean = new 
LocalContainerEntityManagerFactoryBean();
    factoryBean.setDataSource(dataSource());
    factoryBean.setPackagesToScan(new String[] { "webroot.websrv" });
    factoryBean.setJpaVendorAdapter(jpaVendorAdapter());
    factoryBean.setJpaProperties(jpaProperties());
    return factoryBean;
}

/*
 * Provider specific adapter.
 */
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new 
HibernateJpaVendorAdapter();
    return hibernateJpaVendorAdapter;
}

/*
 * Here you can specify any provider specific properties.
 */
private Properties jpaProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", 
  environment.getRequiredProperty
("spring.jpa.properties.hibernate.dialect")
 );
 ...
    return properties;
}

@Bean
@Autowired
public PlatformTransactionManager 
transactionManager(EntityManagerFactory emf) {
    JpaTransactionManager txManager = new JpaTransactionManager();
    txManager.setEntityManagerFactory(emf);
    return txManager;
}
}

Thanks for the suggestions.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfhcjc
系列文章
更多 icon
同类精品
更多 icon
继续加载