简体   繁体   English

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

[英]how can get the datasource configuration for spring from persistence.xml?

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! 坦克很多!

Here is my snippet for doign the same, you will obviously have to use your BasicDataSource instead of the ComboPooledDataSource I use. 这里是我的doign相同片断,你会明显有使用你的BasicDataSource ,而不是我用的是ComboPooledDataSource的。 But they are pretty much the same, replace getDriverClass() with driverClassName , apparently . 但是它们几乎相同, 显然getDriverClass()替换为driverClassName

@Autowired
private ComboPooledDataSource dataSource;

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

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代码中,自动装配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. 如果您遇到任何问题或需要更多详细信息,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM