[英]load dataSource from application.properties
I am using Spring + Hibernate for a school project. 我正在使用Spring + Hibernate进行学校项目。
Everything is working, i am able to get data from the database but i have a question about the database settings. 一切正常,我能够从数据库中获取数据,但是我对数据库设置有疑问。
Is it possible to replace the getDataSource
method for a config file? 是否可以将getDataSource
方法替换为配置文件? Example application.properties. 示例application.properties。
How can I implement it in my config file? 如何在我的配置文件中实现它? And is there a specified location for config files? 并有配置文件的指定位置吗?
This is my AppConfig.java 这是我的AppConfig.java
package com.exstodigital.photofactory;
import com.exstodigital.photofactory.dao.impl.UserDAOImpl;
import org.apache.commons.dbcp2.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@Configuration
@EnableTransactionManagement
public class AppConfig {
@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) {
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
// Models
sessionBuilder.scanPackages("com.exstodigital.photofactory.model");
return sessionBuilder.buildSessionFactory();
}
@Bean(name = "dataSource")
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("********");
dataSource.setUsername("********");
dataSource.setPassword("********");
return dataSource;
}
@Autowired
@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
return new HibernateTransactionManager(sessionFactory);
}
}
Of course you can 当然可以
Try something like 尝试类似
@Value("${spring.datasource.url}")
private String jdbcURl;
@Value("${spring.datasource.username}")
private String dbUsername;
@Value("${spring.datasource.password}")
private String dbPassword;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "your package" });
return sessionFactory;
}
@Bean(name = "dataSource")
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl(jdbcURl);
dataSource.setUsername(dbUsername);
dataSource.setPassword(dbPassword);
return dataSource
}
So your application.properties will have the following properties: 因此,您的application.properties将具有以下属性:
spring.datasource.url = {whatever you want to put}
spring.datasource.username = {Your username}
spring.datasource.password = {Your password}
Update your server.xml
to have the following lines, it is same like what you are doing in the getDataSource() but datasource is configured at the server level. 更新server.xml
以包含以下几行,就像在getDataSource()中所做的一样,但是数据源是在服务器级别配置的。
<Resource name="jdbc/MyDB"
global="jdbc/MyDB"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/UserDB"
username="pankaj"
password="pankaj123"
maxActive="100"
maxIdle="20"
minIdle="5"
maxWait="10000"/>
in your getDataSource just pull this datasource using the following lines of code and return it. 在您的getDataSource中,只需使用以下代码行提取此数据源并返回它。
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/MyLocalDB");
ref : http://www.journaldev.com/2513/tomcat-datasource-jndi-example-java 参考: http : //www.journaldev.com/2513/tomcat-datasource-jndi-example-java
yes it can be done. 是的,它可以做到。
first make a config file:give it a name in my case it is prop.properties and provide all details in key value pairs 首先创建一个配置文件:在我的情况下为它命名为prop.properties,并在键值对中提供所有详细信息
host=127.0.0.1
port=****
dbuser=****
dbpwd=****
dbname=****
then create a property object and file input stream 然后创建一个属性对象和文件输入流
public static Properties prop = new Properties();
static InputStream input = null;
load the property file by providing it's location. 通过提供其位置来加载属性文件。 i have my file in /opt folder 我的文件在/ opt文件夹中
static{
try {
input = new FileInputStream("/opt/prop.properties");
prop.load(input);
} catch (IOException ex) {
ex.printStackTrace();
}
}
now fetch all the property by providing the keys 现在通过提供密钥来获取所有属性
prop.getProperty("host");
prop.getProperty("port");
prop.getProperty("dbuser");
prop.getProperty("dbpwd");
prop.getProperty("dbname");
you can keep the file in the desired location. 您可以将文件保存在所需的位置。 but sure to provide the relative path in the program. 但一定要在程序中提供相对路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.