Spring之 Auto-Wiring All Beans of Compatible Type

Auto-Wiring All Beans of Compatible Type


   @Autowired 注解按类型(type)依赖入住的时候,可以把类型兼容的所有类注入到数组、链表、map等集合数据结构中。如:mybatis中TypeHandler为例:

package com.doctor.practice01;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public interface TypeHandler<T> {

    void setParameter(PreparedStatement ps, int i, T parameter, Object jdbcType) throws SQLException;

    T getResult(ResultSet rs, String columnName) throws SQLException;

    T getResult(ResultSet rs, int columnIndex) throws SQLException;

    T getResult(CallableStatement cs, int columnIndex) throws SQLException;

}
package com.doctor.practice01;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.stereotype.Component;

/**
 * @author sdcuike
 *
 * @time 2016年2月10日 下午9:57:37
 */

@Component("String")
public class StringTypeHandler implements TypeHandler<String> {

    @Override
    public void setParameter(PreparedStatement ps, int i, String parameter, Object jdbcType) throws SQLException {
        // TODO Auto-generated method stub

    }

    @Override
    public String getResult(ResultSet rs, String columnName) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getResult(ResultSet rs, int columnIndex) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getResult(CallableStatement cs, int columnIndex) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

}

package com.doctor.practice01;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import org.springframework.stereotype.Component;

/**
 * @author sdcuike
 *
 * @time 2016年2月10日 下午9:58:23
 */

@Component("Timestamp")
public class SqlTimestampTypeHandler implements TypeHandler<Timestamp> {

    @Override
    public void setParameter(PreparedStatement ps, int i, Timestamp parameter, Object jdbcType) throws SQLException {
        // TODO Auto-generated method stub

    }

    @Override
    public Timestamp getResult(ResultSet rs, String columnName) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Timestamp getResult(ResultSet rs, int columnIndex) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Timestamp getResult(CallableStatement cs, int columnIndex) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }

}

 简单例子,我们要把TypeHandler所有的子类,这里指的是StringTypeHandler、SqlTimestampTypeHandler注入到list和map数据结构中:

package com.doctor.practice01;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author sdcuike
 *
 * @time 2016年2月10日 下午10:00:26
 */

@Component
public class TypeHandlerUtil {

    @SuppressWarnings({ "rawtypes", "unused" })
    private static List<TypeHandler> typeHandlers;

    @SuppressWarnings({ "rawtypes", "unused" })
    private static Map<String, TypeHandler> typeHandlerMap;

    @Autowired
    @SuppressWarnings("rawtypes")
    public void setTypeHandlerMap(Map<String, TypeHandler> typeHandlerMap) {
        TypeHandlerUtil.typeHandlerMap = typeHandlerMap;
    }

    @Autowired
    @SuppressWarnings("rawtypes")
    public void setTypeHandlers(List<TypeHandler> typeHandlers) {
        TypeHandlerUtil.typeHandlers = typeHandlers;
    }

    public static Map<String, TypeHandler> getTypeHandlerMap() {
        return typeHandlerMap;
    }

    public static List<TypeHandler> getTypeHandlers() {
        return typeHandlers;
    }
}


我们测试一下:
package com.doctor.practice01;

import java.util.List;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author sdcuike
 *
 * @time 2016年2月10日 下午9:49:16
 * 
 *       Auto-Wiring All Beans of Compatible Type
 * 
 * 
 */
public class AutoWiringAllBeansOfCompatibleType {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.scan("com.doctor.practice01");
        applicationContext.refresh();
        TypeHandlerUtil typeHandlerUtil = applicationContext.getBean(TypeHandlerUtil.class);
        List<TypeHandler> typeHandlers = typeHandlerUtil.getTypeHandlers();
        typeHandlers.forEach(System.out::println);
        // com.doctor.practice01.SqlTimestampTypeHandler@7a3d45bd
        // com.doctor.practice01.StringTypeHandler@1e7c7811

        typeHandlerUtil.getTypeHandlerMap().forEach((k, v) -> System.out.println("k:" + k + " v:" + v));

        // k:Timestamp v:com.doctor.practice01.SqlTimestampTypeHandler@7a3d45bd
        // k:String v:com.doctor.practice01.StringTypeHandler@1e7c7811

    }
}

注:注入map数据结构的时候,k指的是bean的name。
这样,当我们增加了一个类型的子类的时候,扫描自动注入就带来了便利,而不用我们手动像mybatis一样,配置在一个TypeHandler xml文件中,读取配置的时候再放到一个map结构中,利用spring就带来了便利。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dreamer who

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值