Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
弹簧&x2B;Hibernate,将sessionFactory自动连接到Hibernate DAO_Hibernate_Spring_Autowired_Sessionfactory - Fatal编程技术网

弹簧&x2B;Hibernate,将sessionFactory自动连接到Hibernate DAO

弹簧&x2B;Hibernate,将sessionFactory自动连接到Hibernate DAO,hibernate,spring,autowired,sessionfactory,Hibernate,Spring,Autowired,Sessionfactory,我有一个Hibernate DAO,根据Hibernate API 3和Spring 3.x,我只使用一个sessionFactory,而不是HibernateDataSupport+getHibernateTemplate()-我希望这是一个好的选择- 现在,我的目标是使用注释将sessionFactory自动连接到DAO中 在我的spring.xml中,我有: <context:component-scan base-package="data" /> 我在spring.xml

我有一个Hibernate DAO,根据Hibernate API 3和Spring 3.x,我只使用一个
sessionFactory
,而不是
HibernateDataSupport
+
getHibernateTemplate()
-我希望这是一个好的选择-

现在,我的目标是使用注释将sessionFactory自动连接到DAO中

在我的
spring.xml
中,我有:

<context:component-scan base-package="data" />
我在
spring.xml
加载期间没有错误,但是
sessionFactory
仍然是
null

我该怎么办

编辑

这是我在
spring.xml
中的
sessionFactory
声明:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation=
       "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- <property name="driverClassName" value="org.h2.Driver" /> -->
        <property name="url" value="my/db/url" />
        <property name="username" value="myUsername" />
        <property name="password" value="myPassword" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="data" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <context:component-scan base-package="data" />

    <tx:annotation-driven/>

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

</beans>
可能意味着它找不到
PersonHDAO
bean


谢谢大家。

您申报sessionFactory bean了吗

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>file:src/hibernate.cfg.xml</value>
</property>
</bean>

文件:src/hibernate.cfg.xml

我想你的问题是@Autowired

您可以创建一个类

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
@ComponentScan("com.your.package")
public class HibernateConfig {

  private Environment environment;

  @Autowired
  public void setEnvironment(Environment environment) {
    this.environment = environment;
  }

  @Bean
  public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan(new String[] { "com.your.package" });
    sessionFactory.setHibernateProperties(properties());
    return sessionFactory;
  }

  @Bean
  public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(environment.getRequiredProperty("hibernate.connection.driver_class"));
    dataSource.setUrl(environment.getRequiredProperty("hibernate.connection.url"));
    dataSource.setUsername(environment.getRequiredProperty("hibernate.connection.username"));
    dataSource.setPassword(environment.getRequiredProperty("hibernate.connection.password"));
    return dataSource;
  }

  private Properties properties() {
    Properties hibernateProperties = new Properties();
    hibernateProperties.setProperty("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
    hibernateProperties.setProperty("hibernate.hbm2ddl.auto",
            environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
    return hibernateProperties;
  }
}

@毛里齐奥·库奇亚拉:当然,我用更多的信息编辑了我的第一篇文章。这是一个网络应用程序?您是否在web.xml中声明了org.springframework.web.context.ContextLoaderListener?@Maurizio Cucchiara:不,这是一个桌面应用程序,我没有web.xml文件。您是如何获得PersonHDAO实例的?使用AnnotationConfigApplicationContext?@Maurizio Cucchiara:我只需创建新的istance:new PersondHDAO()。。。这是一个错误吗?我使用以下命令初始化spring上下文:ApplicationContext context=newclasspathXMLapplicationContext(新字符串[]{“spring.xml”});
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>file:src/hibernate.cfg.xml</value>
</property>
</bean>
@Autowired
private SessionFactory sessionFactory;
 @Autowired
 private SessionFactory sessionFactory;

 public void setSessionFactory(SessionFactory sessionFactory) {
     this.sessionFactory = sessionFactory;
 }
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
@ComponentScan("com.your.package")
public class HibernateConfig {

  private Environment environment;

  @Autowired
  public void setEnvironment(Environment environment) {
    this.environment = environment;
  }

  @Bean
  public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan(new String[] { "com.your.package" });
    sessionFactory.setHibernateProperties(properties());
    return sessionFactory;
  }

  @Bean
  public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(environment.getRequiredProperty("hibernate.connection.driver_class"));
    dataSource.setUrl(environment.getRequiredProperty("hibernate.connection.url"));
    dataSource.setUsername(environment.getRequiredProperty("hibernate.connection.username"));
    dataSource.setPassword(environment.getRequiredProperty("hibernate.connection.password"));
    return dataSource;
  }

  private Properties properties() {
    Properties hibernateProperties = new Properties();
    hibernateProperties.setProperty("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
    hibernateProperties.setProperty("hibernate.hbm2ddl.auto",
            environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
    return hibernateProperties;
  }
}