2016-04-24 87 views
2

我希望将Java URL类注入到我的Spring项目中,但URL需要一个参数(您希望打开的URL)。Inject URL()Spring Autowired

我该如何去做这件事?

我现在有

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; 
    } 
+0

为什么不注入属性值呢? – Leon

+0

我无法控制这个类,它内置的URL std Java class @Leon –

+0

只需将'String urlToPing'更改为'URL urlToPing' ... –

回答

0

春天已经是String转换成URL它这样做外的开箱。

简单地将方法签名更改为使用URL而不是String

+0

我正在讨论将整个URL类注入到我的类中,所以我不再需要调用新的URL –

+0

正如前面提到的spring已经这样做了......只需将String从String类型更改为URL ... Spring将注入一个URL的字符串。你不需要为此做任何事情。 –