简体   繁体   中英

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

I need a spring datasource like:

<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.

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. But they are pretty much the same, replace getDriverClass() with driverClassName , apparently .

@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?

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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