0

The dependencies of some of the beans in the application context form a cycle:

┌─────┐
|  encode defined in class path resource [com/extenso/bi/backend/crm/config/BaseConversion.class]
↑     ↓
|  decode defined in class path resource [com/extenso/bi/backend/crm/config/BaseConversion.class]
└─────┘


    @Configuration
    public class BaseConversion {
        private static final String allowedString "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ0123456789";
        private final char[] allowedCharacters = allowedString.toCharArray();
        private final int base = allowedCharacters.length;
        
    
        @Bean
        @Primary
        @Qualifier("encode")
        public String encode( long id) {
            var encodedString = new StringBuilder();
            if (id == 0) {
                return String.valueOf(allowedCharacters[0]);
            }
            while (id > 0) {
                encodedString.append(allowedCharacters[(int) (id % base)]);
                id = id / base;
            }
            return encodedString.reverse().toString();
        }
    
        @Bean
        @Qualifier("getCheckSum")
        public String getCheckSum(long num) {
            int checksumNum = (int) ((num + 5) % (base * base));
            String checksum = encode(checksumNum);
            if (checksum.length() == 1) {
                checksum = encode((checksumNum + 5) % base) + checksum;
            }
            return checksum;
        }
    
        @Bean
        @Qualifier("checkShortUrl")
        public boolean isShortUrlValid( String shortUrl) {
            String checkSum = shortUrl.substring(0, 2);
            long decodedBaseUrl = decode(shortUrl.substring(2));
            return (checkSum.equals(getCheckSum(decodedBaseUrl)));
        }
    
    
        @Bean
        @Qualifier("decode")
        public long decode( String baseInput) {
            var characters = baseInput.toCharArray();
            var length = characters.length;
            var decoded = 0;
            var counter = 1;
            for (char character : characters) {
                decoded += allowedString.indexOf(character) * Math.pow(base, length - counter);
                counter++;
            }
            return decoded;
        }
    }

Andy Wilkinson
  • 85,432
  • 19
  • 215
  • 204
  • 3
    You don't seem to grasp the purpose of dependency injection. It almost never makes sense to declare a simple data value like a `String` or `boolean` as a bean; in fact, these look like just pure functions that should be `static`. – chrylis -cautiouslyoptimistic- May 27 '21 at 05:35

1 Answers1

0

In this case, you can use a util class. There is a good explanation about what is a util class here https://stackoverflow.com/a/25223691/12592670 . For the beans, you can check what are spring beans and how they are configured from the documentation here https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02s02.html

Görhem
  • 16