[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]