Spring之 Auto-Wiring All Beans of Compatible Type
Posted A_Beaver
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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); // [email protected] // [email protected] typeHandlerUtil.getTypeHandlerMap().forEach((k, v) -> System.out.println("k:" + k + " v:" + v)); // k:Timestamp v:[email protected] // k:String v:[email protected] } }
这样,当我们增加了一个类型的子类的时候,扫描自动注入就带来了便利,而不用我们手动像mybatis一样,配置在一个TypeHandler xml文件中,读取配置的时候再放到一个map结构中,利用spring就带来了便利。
以上是关于Spring之 Auto-Wiring All Beans of Compatible Type的主要内容,如果未能解决你的问题,请参考以下文章