什么是配置数据库连接的好方法?
我把数据库配置的配置类中:什么是配置数据库连接的好方法?
@Configuration
@ComponentScan("com.ambre.pta")
@EnableTransactionManagement
@PropertySources({
@PropertySource("classpath:fr/referentiel.properties")
})
public class ApplicationContextConfig {
@Autowired
private Environment env;
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean(name = "dataSource")
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl("jdbc:oracle:thin:@192.168.1.123:1521:xe");
dataSource.setUsername("pta");
dataSource.setPassword("pta");
return dataSource;
}
...
}
的问题是,每次该项目将被交付给不同的客户,则开发人员必须修改这些CONFIGS和重建项目,并最终regenrate战争文件。
那么是否有简单的程序来更改数据库配置而不重建或重新生成war文件?
您可以将数据库配置移动到外部属性中。
添加这样的事情
@PropertySources({
@PropertySource("classpath:fr/referentiel.properties")
@PropertySource("${db.properties}")
})
,然后当你运行你的应用程序只需添加此参数(通知“文件:”前缀)
-Ddb.properties=file:/path-to.properties
您可以在Access数据库的属性您像这样的配置
@Autowired
private Environment env;
...
dataSource.setDriverClassName(env.getProperty("db.driver"));
dataSource.setUrl(env.getProperty("db.url"));
dataSource.setUsername(env.getProperty("db.username"));
dataSource.setPassword(env.getProperty("db.password"));
或与@Value
注释,例如
@Value("${db.driver}")
private String dbDriver;
属性文件path-to.properties
应该只是一个常规的属性文件。所以
db.driver=oracle.jdbc.driver.OracleDriver
db.url=jdbc:oracle:thin:@192.168.1.123:1521:xe
db.username=pta
db.password=pta
我建议你使用Spring开机,它提供了开箱即用的模板项目。另外,在Spring启动应用程序中设置数据库连接非常简单。这是我目前使用的两种方法。
如果您在内存数据库要的,你可以只包括H2/HSQLDB/DERBY依赖于你的pom.xml文件,你是好去。 Spring Boot会在您的pom中看到这些依赖关系,并为您创建一个开箱即用的数据源。这些数据库的一个缺点是它们不提供持久性存储。
-
如果你想要一个持久性存储,比如PostgreSQL,你可以在你的pom.xml文件中包含这个依赖关系,并在你的/ src/main/resources文件夹中维护一个application.properties文件。 例如,如果你有一个PostgreSQL数据库
track-courier
端口5432
本地机器上运行,该文件的内容将类似于下面spring.datasource.url=jdbc:postgresql://localhost:5432/track-courier spring.datasource.username=postgres spring.datasource.password=postgres spring.jpa.hibernate.ddl-auto=update
而且你也不需要手动创建的任何数据源豆在配置类中。春季开机照顾那个开箱即用。您可以查看this链接以供参考。
您可以使用数据源连接到数据库。 下面是使用数据源连接数据库的示例代码。
@Bean
public DataSource dataSource_aw_es() {
DataSource dataSource = null;
Context ctx = null;
try {
ctx = new InitialContext();
dataSource = (DataSource) ctx.lookup("java:/comp/env/jdbc/DataSourcename");
} catch (NamingException e) {
}
return dataSource;
}
您只需要在服务器中配置数据源。 最重要的是,不需要共享数据库凭证以连接到数据库。
下面是在服务器上的数据源配置。(下面configuartion是为Tomcat server.We需要在Tomcat服务器的context.xml配置如下片段。)
<Resource name="jdbc/DataSourceName" auth="Container" type="javax.sql.DataSource"
maxActive="-1" maxIdle="-1" maxWait="-1"
username="user" password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://IP_DB:7011/Schema_name"/>
使用该 – Jens
您可以使用属性春天轮廓。您可以为“dev”设置配置文件,为“生产”设置另一个配置文件。 https://spring.io/blog/2011/02/14/spring-3-1-m1-introducing-profile/ –