在spring bean xml文件中注入模板类接口

时间:2016-01-25 09:29:55

标签: spring

我正在使用模板接口转换为所需的DAO,但是我需要将它放在bean xml文件中,以便我可以在所需的实现类中注入数据源。

SimpleInterfaceDao.java

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);   

}

ClusterInfoDaoImpl.java

    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.xml

  <bean id="queryexecutorimplbean" class="com.connection.impl.QueryExecutorImpl">
  </bean>

1 个答案:

答案 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>

我认为这里与模板无关。