You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2020/02/27 15:48:45 UTC

[GitHub] [incubator-shardingsphere] bio1984 opened a new issue #4505: 自定义ShardingAlgorithm无法注入spring context

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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-shardingsphere] kimmking commented on issue #4505: 自定义ShardingAlgorithm无法注入spring context

Posted by GitBox <gi...@apache.org>.
kimmking commented on issue #4505: 自定义ShardingAlgorithm无法注入spring context
URL: https://github.com/apache/incubator-shardingsphere/issues/4505#issuecomment-592045055
 
 
   can you show some error log?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-shardingsphere] terrymanu commented on issue #4505: 自定义ShardingAlgorithm无法注入spring context

Posted by GitBox <gi...@apache.org>.
terrymanu commented on issue #4505: 自定义ShardingAlgorithm无法注入spring context
URL: https://github.com/apache/incubator-shardingsphere/issues/4505#issuecomment-592081476
 
 
   **For English only**, other languages will not accept.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-shardingsphere] terrymanu closed issue #4505: 自定义ShardingAlgorithm无法注入spring context

Posted by GitBox <gi...@apache.org>.
terrymanu closed issue #4505: 自定义ShardingAlgorithm无法注入spring context
URL: https://github.com/apache/incubator-shardingsphere/issues/4505
 
 
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services