简体   繁体   中英

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).

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.

Simple change the method signature to use an URL instead of a String .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM