java 你如何在 Spring 中配置数据源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29195935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you configure a DataSource in Spring?
提问by luboskrnac
I am studying for Spring Core certification and I have some doubt about how correctly answer to this question:
我正在学习 Spring Core 认证,我对如何正确回答这个问题有些疑问:
How do you configure a DataSource in Spring? Which bean is very useful for development?
你如何在 Spring 中配置数据源?哪个 bean 对开发非常有用?
I think that I do something like this to configure DataSourcein a Spring XML configuration file:
我想我这样做是为了在 Spring XML 配置文件中配置DataSource:
<bean id=“dataSource” class=“org.apache.commons.dbcp.BasicDataSource”>
<property name=“url” value=“${dataSource.url}” />
<property name=“username” value=“${dataSource.username}” />
<property name=“password” value=“${dataSource.password}” />
</bean>
<jdbc:initialize-database data-source=“dataSource”>
<jdbc:script location=“classpath:schema.sql” />
<jdbc:script location=“classpath:test-data.sql” />
</jdbc:initialize-database>
So I think that the answered bean is the org.apache.commons.dbcp.BasicDataSource. Is this assertion true or am I missing something?
所以我认为回答的 bean 是org.apache.commons.dbcp.BasicDataSource。这个断言是真的还是我错过了什么?
What exactly represent the declared configuration tag? It is clear for me what it do but what exactly represent? Is it a special bean declaration or what?
究竟什么代表了声明 配置标签?我很清楚它的作用是什么,但究竟代表了什么?它是一个特殊的bean声明还是什么?
Tnx
田纳西州
回答by luboskrnac
I believe it has to be helper for creating in memory DB for DEV purposes:
我相信它必须是为 DEV 目的在内存数据库中创建的助手:
@Bean
@Profile("dev")
public DataSource devDataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.addScript("classpath:com/bank/config/sql/schema.sql")
.addScript("classpath:com/bank/config/sql/test-data.sql")
.build();
}
XML config:
XML 配置:
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>
Relevant part of Spring docs.
Spring 文档的相关部分。
回答by Marco Schoolenberg
I don't know much about exams and certification questions :-) But here is a valid XML datasource config JDBC MySQL
我不太了解考试和认证问题:-) 但这里有一个有效的 XML 数据源配置 JDBC MySQL
<!-- App's dataSource used by jdbcTemplate,jdbc-user-service and connectController anss so on -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/springappdb" />
<property name="username" value="root" />
<property name="password" value="yourpassword" />
</bean>
<!-- jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>