[英]Inject URL() Spring Autowired
I wish to inject the Java URL
class into my Spring project, however URL takes an argument (of the URL you wish to open).我希望将 Java URL
类注入到我的 Spring 项目中,但是 URL 需要一个参数(您希望打开的 URL)。
How can I go about doing this?我该怎么做呢?
I currently have我目前有
private HttpURLConnection getConnectionHandler(String urlToPing) throws MalformedURLException, IOException, ProtocolException {
URL url = new URL(urlToPing);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
return connection;
}
Spring already converts a String
into an URL
it does this out-of-the-box. Spring 已经将String
转换为URL
它是开箱即用的。
Simple change the method signature to use an URL
instead of a String
.简单地更改方法签名以使用URL
而不是String
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.