2016-04-29 73 views
0

我無法解決與其他類似帖子相關的問題。我試圖在Spring Boot中使用Hibernate。其他具有這種配置的項目工作正常。這一個沒有。也許你們中的一個知道如何解決這個問題。NoSuchBeanDefinitionException:找不到符合條件的[org.hibernate.SessionFactory]類型的bean依賴關係

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=sessionFactory)} 

java.lang.reflect.InvocationTargetException 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:478) 
at java.lang.Thread.run(Thread.java:722) Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'priceDaoHibernateImpl' defined in file [D:\Sonstiges\Entwicklung\Projekte\gatrinWS\target\classes\de\xxx\gatrinws\persistence\PriceDaoHibernateImpl.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=sessionFactory)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=sessionFactory)} 
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) 
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) 
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) 
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) 
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) 
at de.xxx.gatrinws.Application.main(Application.java:10) 
... 6 more Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=sessionFactory)} 
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) 
... 25 more 

這是我的配置

<context:component-scan base-package="de.xxx.gatrinws"/> 

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>classpath:global.properties</value> 
     </list> 
    </property> 
    <property name="ignoreUnresolvablePlaceholders" value="true"/> 
    <property name="ignoreResourceNotFound" value="true"/> 
    <property name="placeholderPrefix" value="${"/> 
</bean> 

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 
    <property name="driverClass" value="${jdbc.driverClassName}"/> 
    <property name="jdbcUrl" value="${jdbc.url}"/> 
    <property name="user" value="${jdbc.username}"/> 
    <property name="password" value="${jdbc.password}"/> 
    <property name="acquireIncrement" value="${c3p0.acquireIncrement}"/> 
    <property name="minPoolSize" value="${c3p0.minPoolSize}"/> 
    <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/> 
</bean> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource"> 
     <ref local="dataSource"/> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
      <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
      <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
      <prop key="hibernate.hbm2ddl.auto">validate</prop> 
      <prop key="hibernate.cache.use_second_level_cache">true</prop> 
      <prop key="hibernate.cache.use_first_level_cache">true</prop> 
      <prop key="maxwait">10000</prop> 
      <prop key="maxidle">25</prop> 
      <prop key="minidle">5</prop> 
     </props> 
    </property> 
    <property name="packagesToScan"> 
     <array> 
      <value>de.xxx.gatrinws</value> 
     </array> 
    </property> 
</bean> 

<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 

<tx:annotation-driven transaction-manager="txManager"/> 

這裏是我的道,其中SessionFactory的自動裝配的似乎不工作: (.class文件位於:de.xxx.gatrinws .service so component scan should work,should not it?)

@Repository 
@Transactional 
public class PriceDaoHibernateImpl implements PriceDao { 

private final SessionFactory sessionFactory; 

@Autowired 
public PriceDaoHibernateImpl(@Qualifier(value = "sessionFactory") SessionFactory sessionFactory) { 
    this.sessionFactory = sessionFactory; 
} 

@Override 
public Product findProduct(String ean) { 
    return null; // to be added 
} 

}

有誰知道如何解決這個問題?服務自動裝配就像一個魅力。看來sessionFactory配置有什麼問題。但是:這是我爲其他項目工作的方式。

在此先感謝!

(如果你想知道:我x'ed封裝基板名)

編輯:

它 「似乎」 是applicationContext.xml中(Spring配置)複製到/目標/運行時,某些屬性無法解析。我需要在任何可以正確解析屬性的地方添加目標文件夾嗎? see screenshot here

未複製的配置可以毫無問題地解決所有屬性。

+0

您是否在嘗試@Autowired時聲明SessionFactory? –

+0

是的。已經嘗試過。沒有不同。 – Hoax

+0

你可以嘗試將你的applicationContext.xml放在src/main/webapp/WEB-INF中嗎?這應該是它的默認位置 –

回答

-3

您是否嘗試過使用自動佈線註釋?如下所示:

@Autowired 
    private SessionFactory sessionFactory; 
+0

是的,已經嘗試將其添加到成員。這兩個通常應該工作,但不能解決問題:( – Hoax

+0

我沒有太多的想法,但你也可以嘗試@Autowired(required = false) – sudhir

+0

你可以與下面的帖子進行交叉驗證:http:// stackoverflow。 COM /問題/ 25288716/RQ能 - 不自動裝配,道無預選賽 - 豆的型發現換依賴?= 1 – sudhir

相關問題