fuzhengwei / door-spring-boot-starter

door-spring-boot-starter
11 stars 7 forks source link

door-spring-boot-starter导入到应用中DoDoor不生效问题 #4

Open shark-ctrl opened 1 year ago

shark-ctrl commented 1 year ago

小傅哥你好,最近在研究你的代码示例时,引入你的door-spring-boot-starter发现DoDoor注解下的DoJoinPoint不生效,经过排查发现你的做法这个DoJoinPoint并不不导入到这个容器中

测试代码如下


 @Autowired
    private ApplicationContext applicationContext;

    @DoDoor(key = "userId", returnJson = "{\"code\":\"1111\",\"info\":\"非白名单可访问用户拦截!\"}")
    @RequestMapping(path = "/user", method = RequestMethod.GET)
    public Map queryUserInfo(@RequestParam String userId) {
     //beansOfType 为空
        Map<String, DoJoinPoint> beansOfType = applicationContext.getBeansOfType(DoJoinPoint.class);
        Map resultMap = new HashMap<>();
        resultMap.put("虫虫:" + userId, "天津市南开区旮旯胡同100号");
        return resultMap;
    }

解决方案:修改StarterAutoConfigure,代码如下


@Configuration
@ConditionalOnClass(StarterService.class)
@EnableConfigurationProperties(StarterServiceProperties.class)
public class StarterAutoConfigure {

    @Autowired
    private StarterServiceProperties properties;

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "itstack.door", value = "enabled", havingValue = "true")
    StarterService starterService() {
        return new StarterService(properties.getUserStr());
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "itstack.door", value = "enabled", havingValue = "true")
    DoJoinPoint doJoinPoint() {
        return new DoJoinPoint();
    }

}

image

fuzhengwei commented 1 year ago

👍🏻 嗯,我记得在issue中也提供了2个方案。

在 2023年1月8日 @.***> 写道:

小傅哥你好,最近在研究你的代码示例时,引入你的door-spring-boot-starter发现DoDoor注解下的DoJoinPoint不生效,经过排查发现你的做法这个DoJoinPoint并不不导入到这个容器中 测试代码如下 @Autowired private ApplicationContext applicationContext; @DoDoor(key = "userId", returnJson = "{\"code\":\"1111\",\"info\":\"非白名单可访问用户拦截!\"}") @RequestMapping(path = "/user", method = RequestMethod.GET) public Map @. String userId) { //beansOfType 为空 Map<String, DoJoinPoint> beansOfType = applicationContext.getBeansOfType(DoJoinPoint.class); Map resultMap = new HashMap<>(); resultMap.put("虫虫:" + userId, "天津市南开区旮旯胡同100号"); return resultMap; } 解决方案:修改StarterAutoConfigure,代码如下 @Configuration @ConditionalOnClass(StarterService.class) @EnableConfigurationProperties(StarterServiceProperties.class) public class StarterAutoConfigure { @Autowired private StarterServiceProperties properties; @Bean @ConditionalOnMissingBean @ConditionalOnProperty(prefix = "itstack.door", value = "enabled", havingValue = "true") StarterService starterService() { return new StarterService(properties.getUserStr()); } @Bean @ConditionalOnMissingBean @ConditionalOnProperty(prefix = "itstack.door", value = "enabled", havingValue = "true") DoJoinPoint doJoinPoint() { return new DoJoinPoint(); } } — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.>