这破站 - Java https://www.racecoder.com/category/Java/ Java是世界上最好的语言^_^ Maven设置项目的Java编译版本 https://www.racecoder.com/archives/1022/ 2021-04-19T15:22:15+00:00 在POM文件里加几行代码就能快速搞定[...] 生成随机银行卡号 https://www.racecoder.com/archives/1015/ 2021-04-09T05:38:00+00:00 测试一个功能需要随机的银行卡号,找了找发现没啥好用的,就自己搞了个轮子,勉强能用吧[...] 限流算法和RateLimiter的使用 https://www.racecoder.com/archives/978/ 2021-03-10T07:38:00+00:00 在开发高并发系统时有三把利器用来保护系统:缓存、降级和限流。限流通过对并发访问/请求进行限速,或者对一个时间窗口内的请求进行限速来保护系统,一旦达到限制速率则可以拒绝服务、排队或等待、降级等处理。除此以外,限流还会有其他的原因,比如控制单个服务的流量、管理资源配额、避免费用超额等等。[...] 将BigDecimal格式化为带千分位分隔符的字符串 https://www.racecoder.com/archives/977/ 2021-03-09T06:40:22+00:00 将BigDecimal的金额格式化[...] java.util.Random中的181783497276652981 https://www.racecoder.com/archives/953/ 2021-01-13T05:43:13+00:00 偶然间看到Random类的构造函数中有181783497276652981这么一个Magic Number, 没有定义为常量, 也没有对这个数做什么特别的说明, 感觉很奇怪, 于是好奇心驱使我扒一扒这个数字的由来:// java version: 1.8.0_251 public class Random implements java.io.Serializable { // ... /** * The internal state associated with this pseudorandom number generator. * (The specs for the methods in this class describe the ongoing * computation of this value.) */ private final AtomicLong seed; /** * Creates a new random number generator. This constructor sets * the seed of the random number generator to a value very likely * to be distinct from any other invocation of this constructor. */ public Random() { this(seedUniquifier() ^ System.nanoTime()); } private static long seedUniquifier() { // L'Ecuyer, "Tables of Linear Congruential Generators of // Different Sizes and Good Lattice Structure", 1999 for (;;) { long current = seedUniquifier.get(); long next = current * 181783497276652981L; if (seedUniquifier.compareAndSet(current, next)) return next; } } private static final AtomicLong seedUniquifier = new AtomicLong(8682522807148012L); // ... }[...] 0xCAFEBABE与Hexspeak https://www.racecoder.com/archives/949/ 2021-01-12T06:18:00+00:00 如果将Java源码编译后的class文件以16进制方式打开,会发现前4个字节都是0xCAFEBABE。[...] 判断一个数是否为2的幂 https://www.racecoder.com/archives/946/ 2021-01-11T09:01:58+00:00 写代码无意中看到java.util.Random中的nextInt(int bound)方法, 有这么两行代码让我虎躯一震。int m = bound - 1; if ((bound & m) == 0) // i.e., bound is a power of 2这就能检测一个数是不是2的幂了???[...] Spring Setter注入(Setter injection)和构造注入(Constructor injection) https://www.racecoder.com/archives/943/ 2021-01-07T10:35:00+00:00 平时使用Spring少不了各种注入,大部分时候都是直接在属性上@Autowired就完了,偶尔看到在构造方法上注入的也知道怎么回事。今天忽然想到这俩有什么区别,了解了下使用场景。[...] Java8 Lambda map()和flatMap() https://www.racecoder.com/archives/939/ 2021-01-07T05:20:00+00:00 在使用lambda的map时候看到了flatMap这个方法,之前知道和map不一样,但是一直没管它,这次又想起来,就找了点资料看了下。[...] Spring Boot data-mongodb连接MongoDB数据库失败 https://www.racecoder.com/archives/931/ 2020-11-19T14:41:00+00:00 用了spring-boot-starter-data-mongodb模块后,连接本机的Mongodb一直失败,用户也创建了,密码也设了,就是连不上啊。<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>总是报错:com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='test', source='test', password=<hidden>, mechanismProperties={}} at com.mongodb.connection.SaslAuthenticator.wrapException(SaslAuthenticator.java:162) at com.mongodb.connection.SaslAuthenticator.access$200(SaslAuthenticator.java:39) at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:68) at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:46) at com.mongodb.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:168) at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:46) at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32) at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:122) at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:52) at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) at java.lang.Thread.run(Thread.java:748) Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" } at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:164) at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295) at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255) at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84) at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34) at com.mongodb.connection.SaslAuthenticator.sendSaslContinue(SaslAuthenticator.java:123) at com.mongodb.connection.SaslAuthenticator.access$100(SaslAuthenticator.java:39) at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:65) ... 9 common frames omitted[...]