我正在 NullpointerExcetion 尝试使用属性文件::
在 SpringBoot 中获取 SessionFactoryapplication.properties ::
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/databseName
db.username=root
db.password=
# Hibernate
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=none
entitymanager.packagesToScan=com
我的配置文件 - 除了SpringBoots默认配置文件之外,我创建了另一个配置文件。我不想把它们混在一起。
@Configuration
@EnableTransactionManagement
@PropertySource({"classpath:application.properties"})
@ComponentScan({"com"})
public class Configuration {
@Value("${db.driver}")
private String DRIVER;
@Value("${db.password}")
private String PASSWORD;
@Value("${db.url}")
private String URL;
@Value("${db.username}")
private String USERNAME;
@Value("${hibernate.dialect}")
private String DIALECT;
@Value("${hibernate.show_sql}")
private String SHOW_SQL;
@Value("${hibernate.hbm2ddl.auto}")
private String HBM2DDL_AUTO;
@Value("${entitymanager.packagesToScan}")
private String PACKAGES_TO_SCAN;
public Configuration() {
// TODO Auto-generated constructor stub
}
@Bean
public DataSource dataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
//dataSource.setDriverClassName(DRIVER);
dataSource.setUrl(URL);
dataSource.setUsername(USERNAME);
dataSource.setPassword(PASSWORD);
System.out.println("CALLING ...................... DATASOURCE ..........");
return dataSource;
}
@Bean
public LocalSessionFactoryBean sessionFactory(){
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(PACKAGES_TO_SCAN);
Properties hibernateProperties = new Properties();
System.out.println("CALLING ...................... LocalSessionFactoryBean ....1......");
hibernateProperties.put("hibernate.dialect", DIALECT);
hibernateProperties.put("hibernate.show_sql", SHOW_SQL);
hibernateProperties.put("hibernate.hbm2ddl.auto", HBM2DDL_AUTO);
sessionFactory.setHibernateProperties(hibernateProperties);
System.out.println("CALLING ...................... LocalSessionFactoryBean ......2....");
return sessionFactory;
}
}
dataSource()中的Print语句显示在控制台中,但 sessionFactory()中的print语句从不显示。 应该在代码中的某处调用 sessionFactory()吗?
我的Utility类是::
public class HibernateUtility {
@Autowired
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
我认为 @Autowired 应该可以在配置中调用 sessionFactory(),但这不是这种情况
我打开和关闭会话的DAO方法是::
public abstract class PromoHibernateDAOImpl{
// ANY OF THE FIRST TWO LINES IN THIS METHOD GIVES **NullPointerException**
// One is in comment
public void openCurrentSession(){
//this.currentSession = HibernateUtility.getSessionFactory().openSession();
this.currentSession = HibernateUtility.getSessionFactory().getCurrentSession();
currentTransaction = currentSession.beginTransaction();
}
public void closeCurrentSession(){
if(currentSession != null && currentTransaction != null){
currentTransaction.commit();
currentSession.close();
}
}
}
openCurrentSession()中的第一个两条线中的任何一条GIVES NullPointerException 。
我分别尝试了每一个。我认为我的配置中的方法 LocalSessionFactoryBean sessionFactory()永远不会被执行,因此实用程序类中的" sessionFactory" 永远不会被初始化,尽管该方法还有 @Bean 。
我做错了什么?
提前致谢