jak na sprężynę documentation, etapy użyć Wiosna JdbcTemplate następująco:pomocą wiosna JdbcTemplate - wstrzyknięcie źródła danych vs jdbcTemplate
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Scans within the base package of the application for @Components to configure as beans -->
<context:component-scan base-package="org.springframework.docs.test" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="jdbc.properties"/>
</beans>
a następnie
@Repository
public class JdbcCorporateEventDao implements CorporateEventDao {
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
// JDBC-backed implementations of the methods on the CorporateEventDao follow...
}
Zasadniczo JdbcTemplate tworzony wewnątrz klasy Component za pomocą ustawiacza dla źródła danych.
Czy jest coś złego w zrobieniu tego w ten sposób, aby w aplikacji było dokładnie JEDNO wystąpienie jdbcTemplate?
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="dataSource"
/>
, a następnie wstrzyknięcie samego jdbcTemplate bezpośrednio do składnika
@Repository
public class JdbcCorporateEventDao implements CorporateEventDao {
@Resource("jdbcTemplate")
private JdbcTemplate jdbcTemplate;
// JDBC-backed implementations of the methods on the CorporateEventDao follow...
}
Czy istnieje powód, dlaczego sama jdbcTemplate nie musi być wstrzykiwany do klasy komponentów bezpośrednio?
SGB
może być duplikatem http: // stackoverflo w.com/q/9460507/309399. Ale na to pytanie nie ma odpowiedzi. – SGB