NoSuchBeanDefinitionException:没有为依赖项

时间:2016-09-11 17:22:09

标签: java spring hibernate jpa-2.0 spring-bean

我正在尝试在内存数据库中设置基于Java的hibernate并从测试开始使用它,但是当加载Hibernate bean时,应用程序上下文失败了。这个练习的目的是使用CrudRepository为几个类和WRITE设置CRUD操作的简单服务

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.orm.jpa.JpaVendorAdapter] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ... 72 more

相关配置和源代码:

hibernate.properties

jdbc.driverClassName = org.hsqldb.jdbcDriver
jdbc.url = jdbc:hsqldb:mem:press
jdbc.username = user
jdbc.password = 
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.show_sql = false
hibernate.format_sql = false

HibernateConfig.java - 无法加载

@Configuration
@EnableTransactionManagement
@PropertySource(value = { "classpath:hibernate.properties" })
public class HibernateConfig {

    @Autowired
    private Environment environment;

    @Bean
    public EntityManagerFactory entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
        LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
        lef.setDataSource(dataSource);
        lef.setJpaVendorAdapter(jpaVendorAdapter);
        lef.setPackagesToScan("net.agilob.press.entity");
        return lef.getNativeEntityManagerFactory();
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "net.agilob.press.entity" });
        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"));
        properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("create-drop"));

        return properties;
    }

    @Bean
    @Autowired
    public HibernateTemplate getHibernateTemplate(SessionFactory sessionFactory) {
        HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);
        return hibernateTemplate;
    }

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

AppConfig.java - 作为测试配置加载的文件

@Configuration
@ComponentScan(basePackages = "net.agilob.press.entity")
@EnableJpaRepositories(basePackages = "net.agilob.press.entity.repository")
@Import(HibernateConfig.class)
public class AppConfig {

    @Bean
    public PostService getPostService() {
        return new PostService();
    }
}

PostServiceTest.java - 这个应该创建Post的实例,将其保存在内存数据库中并稍后提取(只是为了看它是否有效):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
@Transactional
public class PostDAOTest {

    @Autowired
    private PostService postService;

    @Before
    public void setUp() {
        Post post = new Post();
        post.setContent("body");
        post.setDescription("description");
        post.setIntroduction("introduction");
        post.setTitle("title");
        post.setTitleBrowser("title browser");
        postRepository.save(post);
    }

    @Test
    public void testList() {
        // checks if spring wiring was correct
        assertNotNull("Problem with spring beans", postRepository);
        assertFalse(postRepository.findTop10ByTitleOrderByIdAsc().isEmpty());
    }
}

PostService:

@Service("postService")
@Transactional
public class PostService {

    @Autowired
    private PostRepository postRepository;

...
}

PostRepository:

public interface PostRepository extends BaseRepository<Post, Long> {
}

我试图在HibernateConfig中再创建一个@Beans注释方法,该方法正在构建JpaVendorAdapter实例,但是当我运行测试时它失败了:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.persistence.EntityManager]: Factory method 'createSharedEntityManager' threw exception; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ... 59 more Caused by: java.lang.NullPointerException at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.initProxyClassLoader(SharedEntityManagerCreator.java:199) at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.<init>(SharedEntityManagerCreator.java:191) at org.springframework.orm.jpa.SharedEntityManagerCreator.createSharedEntityManager(SharedEntityManagerCreator.java:163) at org.springframework.orm.jpa.SharedEntityManagerCreator.createSharedEntityManager(SharedEntityManagerCreator.java:120) at org.springframework.orm.jpa.SharedEntityManagerCreator.createSharedEntityManager(SharedEntityManagerCreator.java:92) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ... 60 more

我的pom.xml

因此我认为设置中存在其他问题。

任何帮助如何以更好的方式设计和设置它非常感谢。

2 个答案:

答案 0 :(得分:1)

为了让您的entityManagerFactory知道提供者类以及jpa方言,您需要在entityManagerFactory bean上设置jpaVendorAdaptor。这可以作为

完成
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
    jpaVendorAdapter.setShowSql(true);
    jpaVendorAdapter.setDatabase(Database.HSQL);
    jpaVendorAdapter.setDatabasePlatform(MySQL5Dialect.class.getName());
    jpaVendorAdapter.setGenerateDdl(false);
    return jpaVendorAdapter;
}

并在您的entityManagerFactory中调用此方法,如下所示:

lef.setJpaVendorAdapter(jpaVendorAdapter());

答案 1 :(得分:0)

您确定您拥有所有必需的依赖项吗?

您的类路径上是否有HibernateJpaVendorAdapter?

尝试添加依赖项:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>4.3.2.RELEASE</version>
</dependency>