當我們使用spring jdbc時,首先定義一個dataSource bean並在創建jdbcTemplate對象時注入它。我想知道的是,我們是否需要在原型範圍內定義這個數據源。除非整個應用程序只有一個dataSource對象。我認爲這會影響應用程序的性能。數據源是否需要在spring的原型範圍內jdbc
這裏是我如何在spring配置文件中定義dataSouce。
<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/testdb" />
<property name="username" value="root" />
<property name="password" value="123" />
</bean>
在我的DAO類中,我有如下所示的自動裝配的dataSOurce。
@Repository
public class RecordDAOImpl {
JdbcTemplate jdbcTemplate = null;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
}
讓我知道什麼是爲spring mvc web應用程序定義dataSource的最佳方法。