Spring: introduce static member (System.in) through constructor

I wrote some console client for a simple application. To be more flexible, I thought it would be nice to depend only on java.io.Input-/OutputStreaminstead of direct access to System.in/out.

I renamed the class ConsoleClientto StreamClient, added setters, and made sure that System.in/outinstance fields were used instead of fields .

Currently, my client code is as follows:

ApplicationContext appCtx = new ClassPathXmlApplicationContext("...");
StreamClient cc = (StreamClient) appCtx.getBean("streamClient");
cc.setInputStream(System.in);
cc.setOutputStream(System.out);
cc.run();   // start client

Question:

Is there a way to move lines 3 and 4 into the Spring configuration (preferably, embedding the constructor)?

Thank you for your time.

+3
source share
2 answers

<util:constant ... />:

<util:constant id = "out" static-field="java.lang.System.out" />
+5

, bean System.out (, , ). bean, / factory ( System.out)

<bean id="streamOut" class="examples.StreamFactory"
      factory-method="getSystemOut"/>
0

Source: https://habr.com/ru/post/1737810/


All Articles