小编Bha*_*ddy的帖子

使用Java Regex Pattern解析字符串?

我有以下格式的以下java字符串.

String s = "City: [name:NYK][distance:1100] [name:CLT][distance:2300] [name:KTY][distance:3540] Price:"
Run Code Online (Sandbox Code Playgroud)

使用java.util.regex包问题和模式类,我必须得到以下格式的输出字符串:

Output: [NYK:1100][CLT:2300][KTY:3540]
Run Code Online (Sandbox Code Playgroud)

你能建议一个可以帮助我获得上述输出格式的RegEx模式吗?

java regex string pattern-matching

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

如何构建可执行 jar 以将字符串返回给 shell 脚本

我必须从 shell 脚本运行可执行 jar 文件来获取字符串值。可执行 jar 无法返回值,因为 main 返回 void。我不能使用 System.exit(int) 因为 jar 必须返回 String 类型的值。

请建议。

java shell return-type return-value executable-jar

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

Autowired ApplicationContext 没有方法 getBean

我在 RestController 类中自动装配了 ApplicationContext,因为我需要为收到的每个请求创建一个原型 bean。

为了创建 bean,我尝试了 context.getBean(xx) 但 context 没有列出 getBean() 方法。有没有办法可以在 RestController 类中获取原型类的 bean。我将此应用程序作为 Spring boot 运行。

示例代码在这里:

@RestController
@RequestMapping("/Restcompare")
public class CompareService {


    @Autowired
    private ApplicationContext context;

    private Comparator comparator;

    @RequestMapping("/compare")
    public String vcompare(@RequestParam(value="pre", defaultValue="") 
    String pre, @RequestParam(value="post", defaultValue="") String post){


        comparator = context.getBean(Comparator.class);  //Error here
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:

解决方案: IDE 不知何故导入了 Spring 框架之外的不同 ApplicationContext。更正导入以org.springframework.context.ApplicationContext解决问题。

spring spring-ioc spring-boot spring-restcontroller spring-rest

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