bio1984 opened a new issue #4505: 自定义ShardingAlgorithm无法注入spring context URL: https://github.com/apache/incubator-shardingsphere/issues/4505 如标题描述,运行环境为Springboot,启动时已经通过@PostConstruct注解进行ApplicationContext的初始化,但初始化不成功 代码如下: `@Configuration public class ShardingJdbcConfig { @Autowired private DataSource dataSource; @Autowired ApplicationContext applicationContext; @PostConstruct public void init() { if (dataSource instanceof ShardingDataSource) { ShardingDataSource sds = (ShardingDataSource) dataSource; ShardingRuntimeContext shardingContext = sds.getRuntimeContext(); ShardingRule shardingRule = shardingContext.getRule(); StandardShardingStrategy strategy = (StandardShardingStrategy) shardingRule.getDefaultTableShardingStrategy(); ReflectionUtils.doWithFields(StandardShardingStrategy.class, field -> { if ("preciseShardingAlgorithm".equals(field.getName())) { field.setAccessible(true); Object o = field.get(strategy); ReflectionUtils.doWithMethods(ShardingAlgorithm.class, method -> { if ("init".equals(method.getName())) { try { method.invoke(o, applicationContext); } catch (Exception e) { e.printStackTrace(); } } }); } }); } } }` `public class ShardingAlgorithm implements PreciseShardingAlgorithm<Long> { ApplicationContext applicationContext; public void init(ApplicationContext ac) { applicationContext = ac; } @Override public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) { TableShardingService tableShardingService = applicationContext.getBean(TableShardingService.class); } }`
---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services