如何从persistence.xml获取spring的数据源配置?

时间:2022-01-05 12:38:16

I need a spring datasource like:

我需要一个spring数据源,如:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">                  
  <property name="driverClassName" value="<driver>"/>                
  <property name="url" value="<url>" />                
  <property name="username" value="<user>" />     
  <property name="password" value="<pass>" />
</bean> 

I need to obtain driver, url, user, pass from persistence.xml.

我需要从persistence.xml获取驱动程序,URL,用户,传递。

Tanks a lot!

坦克很多!

2 个解决方案

#1


0  

Here is my snippet for doign the same, you will obviously have to use your BasicDataSource instead of the ComboPooledDataSource I use. But they are pretty much the same, replace getDriverClass() with driverClassName, apparently.

这是我的doign片段相同,你显然必须使用你的BasicDataSource而不是我使用的ComboPooledDataSource。但它们几乎相同,显然是用driverClassName替换getDriverClass()。

@Autowired
private ComboPooledDataSource dataSource;

public String myMethod() {
  return dataSource.getDriverClass());
}

#2


0  

Do you want to print it, or use it in your application for connecting to the dB?

您想要打印它,还是在您的应用程序中使用它来连接到dB?

If later one is the case, then, create a bean for sessionFactory, set hibernateProperties for the same where you can inject datasource as well.

如果稍后是这种情况,那么,为sessionFactory创建一个bean,将hibernateProperties设置为同样可以注入数据源的地方。

In java code, autowire sessionFactory object (or set it using a setter method) and call getCurrentSession method for the same.

在java代码中,autowire sessionFactory对象(或使用setter方法设置它)并为其调用getCurrentSession方法。

For getting various attributes, use chained getter methods to return datasource and extract all the details.

要获取各种属性,请使用链式getter方法返回数据源并提取所有详细信息。

Let me know if you face any issue or need more details for the same.

如果您遇到任何问题或需要更多详细信息,请与我们联系。

#1


0  

Here is my snippet for doign the same, you will obviously have to use your BasicDataSource instead of the ComboPooledDataSource I use. But they are pretty much the same, replace getDriverClass() with driverClassName, apparently.

这是我的doign片段相同,你显然必须使用你的BasicDataSource而不是我使用的ComboPooledDataSource。但它们几乎相同,显然是用driverClassName替换getDriverClass()。

@Autowired
private ComboPooledDataSource dataSource;

public String myMethod() {
  return dataSource.getDriverClass());
}

#2


0  

Do you want to print it, or use it in your application for connecting to the dB?

您想要打印它,还是在您的应用程序中使用它来连接到dB?

If later one is the case, then, create a bean for sessionFactory, set hibernateProperties for the same where you can inject datasource as well.

如果稍后是这种情况,那么,为sessionFactory创建一个bean,将hibernateProperties设置为同样可以注入数据源的地方。

In java code, autowire sessionFactory object (or set it using a setter method) and call getCurrentSession method for the same.

在java代码中,autowire sessionFactory对象(或使用setter方法设置它)并为其调用getCurrentSession方法。

For getting various attributes, use chained getter methods to return datasource and extract all the details.

要获取各种属性,请使用链式getter方法返回数据源并提取所有详细信息。

Let me know if you face any issue or need more details for the same.

如果您遇到任何问题或需要更多详细信息,请与我们联系。