我正在使用模板接口转换为所需的DAO,但是我需要将它放在bean xml文件中,以便我可以在所需的实现类中注入数据源。
package com.dao;
import java.util.List;
import com.connection.impl.QuerySpecifierWrapperList;
public interface SimpleInterfaceDao<T> {
public List<String> loadColumnNames(String query);
public List<T> loadAll(String query);
}
package com.dao.impl;
import java.util.List;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import com.common.CommonConstants;
import com.connection.impl.QueryExecutorImpl;
import com.connection.impl.QuerySpecifierWrapperList;
import com.dao.ClusterInfo;
import com.dao.SimpleInterfaceDao;
public class ClusterInfoDaoImpl <T> implements SimpleInterfaceDao <T> {
@Autowired
DataSource GlobalcashmanmdataSource;
static Logger log = Logger.getLogger(ClusterInfoDaoImpl.class.getName());
public List<T> loadAll(String query){
List<ClusterInfo> listClusterinfo = null;
log.info("Executing query loadAll:" + query);
//JdbcTemplate jdbcTemplate = (JdbcTemplate) GlobalcashmanmdataSource;
JdbcTemplate jdbcTemplate = new JdbcTemplate(GlobalcashmanmdataSource);
listClusterinfo = jdbcTemplate.query(query,new BeanPropertyRowMapper<ClusterInfo>(ClusterInfo.class));
return (List <T>) listClusterinfo;
}
public List<String> loadColumnNames(String query){
List<String> columnNames = null;
//TODO to be removed or commented once the testing is done.
//callContextToSetDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(GlobalcashmanmdataSource);
columnNames = jdbcTemplate.queryForList(query,String.class);
return columnNames;
}
public void insert(ClusterInfo customer){
}
}
public class QueryExecutorImpl {
public QueryExecutionResults executeQueryWrapperByQueryID(String queryID){
......
......
SimpleInterfaceDao <ClusterInfo> daoInterface = new ClusterInfoDaoImpl<ClusterInfo>();
queryColumnNames = daoInterface.loadColumnNames(querySpecifier.getColumnQuery());
queryRowColResult = (List) daoInterface.loadAll(querySpecifier.getColumnQuery());
......
}
<bean id="queryexecutorimplbean" class="com.connection.impl.QueryExecutorImpl">
</bean>
答案 0 :(得分:1)
问题不明确,但你可以尝试这种方式
如果ClusterInfoDaoImpl
bean已经创建,那么它很简单,直接将其注入QueryExecutorImpl
(有一个实例ClusterInfoDaoImpl<?> daoInterface; and it's setter property
)
<bean id="queryexecutorimplbean" class="com.connection.impl.QueryExecutorImpl">
<property name="ClusterInfoDao" ref="ClusterInfoDaoImpl" />
</bean>
我认为这里与模板无关。