标签:
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(); } }
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 } }
Spring之 Auto-Wiring All Beans of Compatible Type
标签:
原文地址:http://blog.csdn.net/doctor_who2004/article/details/50650313