我想动态更新会话工厂对象。由于我想动态更新/更改数据库凭据而无需重新启动服务器,我希望这些更改反映在数据源中,并重新创建会话工厂。
public class DataSourceConfiguration
{
private String url;
private int port;
private String userName;
private String password;
private String schema;
@Bean(name="dataSource")
public DataSource getDataSource()
{
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
String urlPath= "jdbc:mysql://"+url+":"+port+"/"+schema+"?zeroDateTimeBehavior=convertToNull";
ds.setUrl(urlPath);
ds.setUsername(userName);
ds.setPassword(password);
return ds;
}
@Bean(name = "sessionFactory")
@Scope("prototype")
public SessionFactory getSessionFactory() throws Exception
{
Properties properties = getHibernateProperties();
LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
factory.setPackagesToScan(new String[] { "org.netlink.notification.modal" });
factory.setDataSource(getDataSource());
factory.setHibernateProperties(properties);
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public HibernateTransactionManager txManager()
{
try
{
return new HibernateTransactionManager(getSessionFactory());
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Bean
public Properties getHibernateProperties()
{
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
properties.put("hibernate.show_sql", true);
return properties;
}
}