Sprit000 2013-06-19 13:47
浏览 385
已采纳

Hibernate AnnotationSessionFactoryBean 获取sessionFactory问题

[b]
[color=red]下面的代码中,getSessionFactory方法中a.getObject()返回的值为null.请问是应该怎么解决啊???[/color]
[/b]

[code="java"]

@SuppressWarnings("unchecked")
@Configuration
@Component
public class AppConfig implements ServletConfigAware,ServletContextAware{

//hibernate(读取配置文件中的值) 
private @Value("${hibernate.dialect}") String dialect; 
private @Value("${hibernate.show_sql}") String showSql;
private @Value("${hibernate.hbm2ddl.auto}") String hbm2ddl;
private @Value("${hibernate.default_schema}") String default_schema;
private @Value("${hibernate.max_fetch_depth}") String max_fetch_depth;
private @Value("${hibernate.query.factory_class}")String query_factory_class;
//jdbc
private @Value("${jdbc.driverClass}") String driverClass;
private @Value("${jdbc.url}") String jdbcUrl;
private @Value("${jdbc.user}") String user;
private @Value("${jdbc.password}") String password;

//c3p0
private @Value("${c3p0.minPoolSize}") int minPoolSize;
private @Value("${c3p0.maxPoolSize}") int maxPoolSize;
private @Value("${c3p0.initialPoolSize}") int initialPoolSize;
private @Value("${c3p0.maxIdleTime}") int maxIdleTime;
private @Value("${c3p0.maxStatements}") int maxStatements;
private @Value("${c3p0.acquireIncrement}") int acquireIncrement;
private @Value("${c3p0.idleConnectionTestPeriod}") int idleConnectionTestPeriod;
private @Value("${c3p0.acquireRetryAttempts}") int acquireRetryAttempts;
private @Value("${c3p0.breakAfterAcquireFailure}") boolean breakAfterAcquireFailure;

//mail
private @Value("${mail.host}") String host;
private @Value("${mail.username}") String username;
private @Value("${mail.password}") String mailpwd;
private @Value("${mail.smtp.auth}") String smtpAuth;
private @Value("${mail.smtp.timeout}") String smtpTimeout;

private @Value("${system.hibernate.models}") String models;     //hibernate扫描的包

private ApplicationContext applicationContext;


public ApplicationContext getApplicationContext() {
    return applicationContext;
}

public void setApplicationContext(ApplicationContext applicationContext) {
    this.applicationContext = applicationContext;
}

//数据源
@Bean(name="dataSource")
public DataSource getDataSource(){
    ComboPooledDataSource cpds = new ComboPooledDataSource(); 
    cpds.setJdbcUrl(jdbcUrl);
    try {
        cpds.setDriverClass(driverClass);
    } catch (PropertyVetoException e) {
        e.printStackTrace();
    }
    cpds.setUser(user);
    cpds.setPassword(password);

    //连接池中保留的最小连接数
    cpds.setMinPoolSize(minPoolSize);
    //连接池中保留的最大连接数。Default: 15
    cpds.setMaxPoolSize(maxPoolSize);
    //初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3
    cpds.setInitialPoolSize(initialPoolSize);
    //最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0
    cpds.setMaxIdleTime(maxIdleTime);
    //当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3
    cpds.setAcquireIncrement(acquireIncrement);
    //每60秒检查所有连接池中的空闲连接。Default: 0
    cpds.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
    //定义在从数据库获取新连接失败后重复尝试的次数。Default: 30
    cpds.setAcquireRetryAttempts(acquireRetryAttempts);
    //获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。Default: false-->
    cpds.setBreakAfterAcquireFailure(breakAfterAcquireFailure);
    cpds.setMaxStatements(maxStatements);

    return cpds;
}

//Hibernate - SessionFactory
@Bean(name="sessionFactory")
@DependsOn("dataSource")
public SessionFactory getSessionFactory() throws Exception { 
    Properties properties = new Properties();
    properties.put("hibernate.dialect", dialect);
    /**
    properties.put("hibernate.show_sql", showSql);
    properties.put("hibernate.hbm2ddl.auto", hbm2ddl);
    properties.put("hibernate.max_fetch_depth", max_fetch_depth);
    properties.put("hibernate.temp.use_jdbc_metadata_defaults", "false");  
    properties.put("hibernate.jdbc.fetch_size", "50");
    properties.put("hibernate.jdbc.batch_size", "25"); */

    AnnotationSessionFactoryBean a = new AnnotationSessionFactoryBean(); 
    a.setDataSource(this.getDataSource());
    a.setHibernateProperties(properties);  
    a.setPackagesToScan(models.split(","));

    SessionFactory sessionFactory = a.getObject();
    System.out.println("dataSource = "+this.getDataSource());
    System.out.println("sessionFactory = "+sessionFactory);
    return sessionFactory;

}

public void setServletConfig(ServletConfig config) {

}

@Override
public void setServletContext(ServletContext arg0) {

}

}

[/code]

  • 写回答

1条回答 默认 最新

  • jinnianshilongnian 2013-06-19 13:58
    关注

    你需要先调用afterPropertiesSet 再调用getObject

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件