小编Mik*_*kov的帖子

为什么String.strip()比String.trim()快5倍于空字符串在Java 11中

我遇到了一个有趣的场景.由于某种原因strip(),空格字符串(仅包含空格)明显快trim()于Java 11.

基准

public class Test {

    public static final String TEST_STRING = "   "; // 3 whitespaces

    @Benchmark
    @Warmup(iterations = 10, time = 200, timeUnit = MILLISECONDS)
    @Measurement(iterations = 20, time = 500, timeUnit = MILLISECONDS)
    @BenchmarkMode(Mode.Throughput)
    public void testTrim() {
        TEST_STRING.trim();
    }

    @Benchmark
    @Warmup(iterations = 10, time = 200, timeUnit = MILLISECONDS)
    @Measurement(iterations = 20, time = 500, timeUnit = MILLISECONDS)
    @BenchmarkMode(Mode.Throughput)
    public void testStrip() {
        TEST_STRING.strip();
    }

    public static void main(String[] args) throws …
Run Code Online (Sandbox Code Playgroud)

java string performance microbenchmark java-11

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

Spring Boot绑定@Value到Enum不区分大小写

枚举

public enum Property {
    A,
    AB,
    ABC;
}
Run Code Online (Sandbox Code Playgroud)

领域

@Value("${custom.property}")
protected Property property;
Run Code Online (Sandbox Code Playgroud)

application.properties(小写)

custom.property=abc
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序时,我有一个错误:

无法将[java.lang.String]类型的值转换为必需的类型[com.xxx.Property]:找不到匹配的编辑器或转换策略.

鉴于(大写):

custom.property=ABC
Run Code Online (Sandbox Code Playgroud)

工作良好.

有没有办法绑定值不区分大小写的值?像ABC,Abc,AbC,abc任何模式都应该有效.

注:我看到这个问题- 春季3.0 MVC结合枚举大小写敏感的,但在我的情况我有超过10枚举/值(并期望有更多)班,并实施10层不同的自定义属性的粘合剂将是痛苦的,我需要一些通用的解决方案.

java enums spring spring-boot spring-properties

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

最好的方法是在spring boot中定义重要的凭据

使用Spring Boot应用程序时,我们application.properties根据不同的环境使用不同的文件.

我们提供了重要的凭据,如:数据库配置,服务器IP,管理员用户名/密码等.

我担心如果有人获得我们的应用程序属性并获得所有重要细节会发生什么.

有没有什么好的方法可以将重要的凭据放在某处并在基于环境的Spring Boot应用程序中获取它们?

java security password-protection spring-boot

13
推荐指数
1
解决办法
3029
查看次数

Java相当于SecureString

我正在寻找Java相当于.NET的SecureString.aspx.是否有这样的实施在2018年?

OWASP实现并不完全相同,因为它只是一个普通的char数组.虽然.NET等价物提供了额外的功能,例如从/向非托管内存获取实例的能力以及加密.

我知道普通的Java模式传递密码,char[]Arrays.fill()在使用后用零做.但它需要在char[]所有时间内构建一个简单的实用程序类.

java securestring java-security

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

使用匿名参数化类类型推断的javac NPE时,JDK 11.0.2编译失败

代码(spring-web 5.1.2)

public static void main(String[] args) {
    RestTemplate restTemplate = new RestTemplate();

    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.AUTHORIZATION, "token");
    HttpEntity<Object> requestEntity = new HttpEntity<>(headers);

    ResponseEntity<Object> test = restTemplate.exchange(
            "https://example.com",
            HttpMethod.GET,
            new HttpEntity<>(headers),
            new ParameterizedTypeReference<>() { // fails here
            });
}
Run Code Online (Sandbox Code Playgroud)

OracleJDK 1.8(预期输出)

无法推断org.springframework.core.ParameterizedTypeReference的类型参数

原因:不能对匿名内部类使用“ <>”

OracleJDK 11.0.2(非预期输出)

编译器消息文件损坏:key = compiler.misc.msg.bug arguments = 11.0.2,{1},{2},{3},{4},{5},{6},{7} java.lang jdk.compiler / com.sun.tools.javac.comp.Flow $ FlowAnalyzer.visitApply(Flow.java:1235)处的.NullPointerException,jdk.compiler / com.sun.tools.javac.tree.JCTree $ JCMethodInvocation.accept( JcTree.java:1634),位于jdk.compiler / com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49),位于jdk.compiler / com.sun.tools.javac.comp.Flow $ BaseAnalyzer。在jdk.compiler / com.sun.tools.javac.comp.Flow $ FlowAnalyzer.visitVarDef(Flow.java:989)处扫描(Flow.java:398)
...

如果我将菱形运算符更改为显式类型 …

java type-inference javac compiler-bug java-11

12
推荐指数
4
解决办法
2777
查看次数

如何禁用AlertDialog内的按钮?

我想AlertDialog用3个按钮写一个.如果不满足某个条件,我希望禁用中间的中性按钮.

int playerint = settings.getPlayerInt();
int monsterint = settings.getMonsterInt();



        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
        alertbox.setMessage("You have Encountered a Monster");

        alertbox.setPositiveButton("Fight!",
                new DialogInterface.OnClickListener() {

                    // do something when the button is clicked
                    public void onClick(DialogInterface arg0, int arg1) {
                        createMonster();
                        fight();

                    }
                });

        alertbox.setNeutralButton("Try to Outwit",
                new DialogInterface.OnClickListener() {

                    // do something when the button is clicked
                    public void onClick(DialogInterface arg0, int arg1) {
                        // This should not be static
//                      createTrivia();
                        trivia();

                    }
                });

        // Return to …
Run Code Online (Sandbox Code Playgroud)

java android classcastexception android-alertdialog

10
推荐指数
3
解决办法
1万
查看次数

Spring Test MockMvc对外部URL执行请求

我正在尝试在当前上下文之外的URL上执行POST请求,看起来Spring看起来无法理解它.

测试代码:

        String content = mvc
            .perform(post("http://some-external-url.com:8080/somepath)
                    .header("Authorization", authorization)
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                    .param("username", username)
                    .param("password", password)
            .andExpect(status().isOk())
            .andReturn().getResponse().getContentAsString();
Run Code Online (Sandbox Code Playgroud)

就当前背景下的魅力而言.但是根本无法达到远程服务.似乎" http://some-external-url.com:8080 "部分被忽略了.

模拟创建代码:

mvc = MockMvcBuilders.webAppContextSetup(context).build();
Run Code Online (Sandbox Code Playgroud)

有没有办法使它工作?因为使用标准的Java HttpUrlConnection类是一个巨大的痛苦.

java testing spring spring-mvc spring-test

8
推荐指数
1
解决办法
5638
查看次数

POSTMAN 在 XML 请求正文中设置变量

我正在尝试使用 postman 使用 xml 发出 SOAP 请求,但我想要发送的一些参数必须是我已经存储在其他请求中的环境变量。

请求正文是这样的:

<soapenv:Envelope xmlns:soapenv="blablabla" xmlns:XXX="http:blablabla/">
    <soapenv:Header/>
    <soapenv:Body>
        <XXX:Function>
            <CODE>878734ijHgs</CODE>
            <DISTRIBUTOR>myDistributor</DISTRIBUTOR>
            <MAGICNUMBER>21</MAGICNUMBER>
        </XXX:Function>
    </soapenv:Body>
</soapenv:Envelope>
Run Code Online (Sandbox Code Playgroud)

关键是,作为MAGICNUMBER,我想发送一个之前存储为环境变量的值,但语法 {{variable}} 不适用于 XML

请建议。

xml variables soap request postman

8
推荐指数
1
解决办法
2万
查看次数

如何在JVM内部发生java对象锁定和监视器创建

假设我有以下代码片段,其中两个线程访问具有两个关键部分(同步语句)的相同方法.这些同步语句中的每一个都被赋予不同的锁定对象.代码如下:

public class MyWorker {
private Random random = new Random();

private Object lock1 = new Object();
private Object lock2 = new Object();

private List<Integer> list1 = new ArrayList<>();
private List<Integer> list2 = new ArrayList<>();

private void stageOne() {

    synchronized (lock1) {
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        list1.add(random.nextInt(100));
    }

}

private void stageTwo() {

    synchronized (lock2) {
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        list2.add(random.nextInt(100));
    }

}

private void process() {
    for …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading jvm-hotspot

8
推荐指数
1
解决办法
867
查看次数

如何修复:不推荐使用"Sensio\Bundle\FrameworkExtraBundle\Configuration\Route"

Symfony版本:4.1

当我使用PHPUnit运行测试时,我有以下弃用消息:

从版本5.2开始,不推荐使用注释"Sensio\Bundle\FrameworkExtraBundle\Configuration\Route".请改用"Symfony\Component\Routing\Annotation\Route".

我想澄清我放入我的framework.yaml:

sensio_framework_extra:
     router:
         annotations: false
Run Code Online (Sandbox Code Playgroud)

我还想澄清use Sensio \ Bundle \ FrameworkExtraBundle \ Configuration \ Route我的控制器中没有.

我使用FOSRestBundle,我得到的印象是问题来自那里,但我试图修复FOSREST文档中提供的配置.

你有这种类型的错误和/或你知道我应该在哪里看吗?

php symfony

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