접근 제한된 생성자, 필드에 접근하는 방법
1
2
3
4
5
6
7
8
public static void initConfiguration() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Constructor<ServerConfiguration> constructor =
ServerConfiguration.class.getDeclaredConstructor(int.class, String.class); // 특정 클래스에서 인자로 받은 타입을 인자로 받는 생성자를 찾아서 반환한다.
constructor.setAccessible(true); // 접근 제한을 전부 허용한다.
constructor.newInstance(8080, "Good Day!"); // 새로운 인스턴스를 생성한다.
}
실행 코드 : HttpServer 를 구성하고 요청을 보낸 후 응답을 받는다.
1
2
3
4
5
public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, IOException {
initConfiguration();
WebServer webServer = new WebServer();
webServer.startServer();
}
구성 클래스
WebServer class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class WebServer {
public void startServer() throws IOException {
HttpServer httpServer = HttpServer.create(ServerConfiguration.getInstance().getServerAddress(), 0);
httpServer.createContext("/greeting").setHandler(exchange -> { // /greeting 경로의 핸들러생성
String responseMessage = ServerConfiguration.getInstance().getGreetingMessage();
exchange.sendResponseHeaders(200, responseMessage.length());
OutputStream responseBody = exchange.getResponseBody();
responseBody.write(responseMessage.getBytes());
responseBody.flush();
responseBody.close();
});
System.out.println(String.format("Starting server on address %s:%d",
ServerConfiguration.getInstance().getServerAddress().getHostName(),
ServerConfiguration.getInstance().getServerAddress().getPort()));
httpServer.start();
}
}
ServerConfiguration class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class ServerConfiguration {
private static ServerConfiguration serverConfigurationInstance;
private final InetSocketAddress serverAddress;
private final String greetingMessage;
private ServerConfiguration(int port, String greetingMessage) {
this.greetingMessage = greetingMessage;
this.serverAddress = new InetSocketAddress(port);
if (serverConfigurationInstance == null) {
serverConfigurationInstance = this;
}
}
public static ServerConfiguration getInstance() {
return serverConfigurationInstance;
}
public InetSocketAddress getServerAddress() {
return this.serverAddress;
}
public String getGreetingMessage() {
return this.greetingMessage;
}
}
실행 코드
1
Starting server on address 0.0.0.0:8080