spring数据源配置。加注释
我的道的实现spring数据源配置。加注释,spring,jdbc,annotations,datasource,Spring,Jdbc,Annotations,Datasource,我的道的实现 \\here are imports... @Repository public class CompanyDaoImp extends JdbcDaoSupport implements CompanyDao { private static final String INSERTCOMPANY = "INSERT INTO b_company" + "(NAME)VALUES(?)"; 这是我的豆子 <?xml version=".........
\\here are imports...
@Repository
public class CompanyDaoImp extends JdbcDaoSupport implements CompanyDao {
private static final String INSERTCOMPANY = "INSERT INTO b_company"
+ "(NAME)VALUES(?)";
这是我的豆子
<?xml version=".........
<!-- TODO add the component-scan and annotation-config elements -->
<context:annotation-config/>
<context:component-scan base-package="com.some.company"/>
<bean id="companyAppDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/companyapp"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
</beans>
JdbcDaoSupport
没有任何注释来自动注入数据源。您必须像这样重写setDataSource
方法:
<bean name="companyDaoImp" class="...CompanyDaoImp">
<property name="dataSource" ref="companyAppDataSource" />
</bean>
Spring无法自动注入数据源
,因为您可能有多个数据源,在这种情况下,有必要告诉Spring您需要哪一个数据源。我认为setDataSource是最终的,所以我无法覆盖它。第二个溶胶。可以工作,但是有没有办法只通过注释(DataSourceBean除外)@cek您是对的。我没有注意到。我在答案中删除了这个。对不起,没问题。你认为如果我在定义DataSourceBean时出错了吗?你可以定义另一个方法名(setDataSource2
),但这不是很好。您也不能使用JdbcDaoSupport
并为所有DAO(例如AbstractDaoSupport
)定义您自己的通用类和正确的注释。下面的JIRA问题讨论了相同的问题