小编Der*_*ble的帖子

在Java中连接字符串的最有效方法

我的印象是StringBuffer是连接字符串的最快方法.但我看到这篇 stackoverflow的帖子说concat是最快的方法.我在Java 1.5,1.6和1.7中尝试了给出的2个例子我从未得到他们得到的结果.我的结果几乎就像这样

  1. 有人可以解释一下我在这里不明白的地方吗?什么是真的
  2. 当多个字符串出现时,方法会对两个字符串有效吗?(如连接4变量S1,S2,S3,S4)

java stringbuffer

4
推荐指数
2
解决办法
9102
查看次数

为什么当Comparator.compare相等时我们需要返回0

我了解在执行Comparator接口的比较方法时,我们需要返回

  • 如果o1> o2,则+1
  • 如果o1 <o2则为-1
  • 如果o1 == o2,则为0

我的问题是为什么我们都必须在相等时返回0?用例是什么?用在什么地方?如果我们考虑在o2大于o1或o2等于o1时进行排序,则不会更改其位置。谁能来解释这个的实际用例?

Java文档说

比较其两个参数的顺序。当第一个参数小于,等于或大于第二个参数时,返回负整数,零或正整数。

这是否意味着返回-1或返回0具有相同的影响?

零或正整数

 @Override
    public int compare(Test f1, Test f2) {
        if (f1.getId() > f2.getId()) {
            return 1;
        } else if (f1.getId() < f2.getId()) {
            return -1;
        } else {
            return 0;
        }

    }
Run Code Online (Sandbox Code Playgroud)

java sorting collections comparator

2
推荐指数
2
解决办法
78
查看次数

WebSecurityConfigurerAdapter 覆盖 EnableResourceServer?

我有使用 spring oAuth2 的完美运行的 Spring Boot 应用程序。当我在主类上使用 @EnableResourceServer 时,它运行良好。但要为我在课下实现的服务启用 CORS

   @Configuration
    public class CorsConfiguration extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {

            http.csrf().disable();
            http.cors();

        }
        @Bean
        public CorsConfigurationSource corsConfigurationSource() {
            final org.springframework.web.cors.CorsConfiguration configuration = new org.springframework.web.cors.CorsConfiguration();
            configuration.setAllowedOrigins(ImmutableList.of("*"));
            configuration.setAllowedMethods(ImmutableList.of("HEAD",
                    "GET", "POST", "PUT", "DELETE", "PATCH"));
            configuration.setAllowCredentials(true);
   configuration.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type"));
            final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", configuration);
            return source;
        }
    }
Run Code Online (Sandbox Code Playgroud)

添加该类后,似乎请求未使用 oAuth 令牌进行验证。即使没有 oAuth 令牌的任何请求也可以访问服务。

我做错了什么?我不能两个都用吗?有什么特别的事情要做吗?有没有更好的方法来启用 CORS。(我试过@CrossOrigin 注释它没有用)

spring oauth spring-security spring-security-oauth2

0
推荐指数
1
解决办法
715
查看次数