인터페이스 디폴트 오류

Objective_chicorita·2022년 9월 22일
0

오류처리

목록 보기
2/2

ContextLoaderListener.java

package com.bitcamp.board.listener;

import java.sql.Connection;
import java.sql.DriverManager;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import com.bitcamp.board.dao.MariaDBBoardDao;
import com.bitcamp.board.dao.MariaDBMemberDao;

// 웹 애플리케이션이 시작되었을 때 공유할 자원을 준비시키거나 해체하는 일을 한다.                                         
//
@WebListener
public class ContextLoaderListener implements ServletContextListener{
  
  public void ContextLoaderListener(ServletContextEvent sce) {
  
    System.out.println("공유 자원을 준비중!");
    try { 
      Class.forName("org.mariadb.jdbc.Driver");

      Connection con = DriverManager.getConnection(
          "jdbc:mariadb://localhost:3306/studydb","study","1111");

      ServletContext ctx = sce.getServletContext();
      ctx.setAttribute("boardDao", new MariaDBBoardDao(con));
      ctx.setAttribute("memberDao", new MariaDBMemberDao(con));
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

}

ServletContextListener 인터페이스를 구현하는 ContextLoaderListener 클래스를 생성.

  • 생성자를 만듬.
  • 오류는 발생하지 않았음. 그러나 웹에서는 작동하지 않음.

ServletContextListener(인터페이스)


package javax.servlet;

import java.util.EventListener;

/** 
 * Interface for receiving notification events about ServletContext
 * lifecycle changes.
 *
 */
public interface ServletContextListener extends EventListener {

    /**
     * Receives notification that the web application initialization
     * process is starting.
     * @implSpec
     * The default implementation takes no action.
     */
    default public void contextInitialized(ServletContextEvent sce) {}

    /**
     * Receives notification that the ServletContext is about to be
     * shut down.
     *
     * @implSpec
     * The default implementation takes no action.
     */
    default public void contextDestroyed(ServletContextEvent sce) {}
}

인터페이스 특징 )

강제성 - 인터페이스로 클래스를 구현하면 클래스 안에는 인터페이스에 있는 모든 메서드를 구현해야 한다! (강제성)

ServletContextListener 인터페이스 에서는 메서드에 default 처리가 되어 있음!
-> default : 꼭 강제로 선언할 필요가 없는 메서드
-> 선언해야할 메서드가 너무 많을 때, 인터페이스 안에 필요한 메서드만 구현하고 싶을 때 사용!

" 인터페이스의 가장 큰 특징인 인터페이스의 강제성을 제외하면 되겠는가?! " -개발자 A
" 에이~ 그래도 만약에 메서드가 엄청 많으면 그 긴 코드를 다 선언하라고? 너무 불편해~
default는 꼭 필요해! "- 개발자 B
" 만약 인테페이스 구현하다가 메서드가 하나도 없는 클래스가 구현되면 어떻게!??! " -개발자A
" 에이~ 설마 개발자가 그걸 헷갈리겠어? "-개발자B

... 맞다.... 그게 나다...

ContextLoaderListener.java 수정!

package com.bitcamp.board.listener;

import java.sql.Connection;
import java.sql.DriverManager;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import com.bitcamp.board.dao.MariaDBBoardDao;
import com.bitcamp.board.dao.MariaDBMemberDao;

// 웹 애플리케이션이 시작되었을 때 공유할 자원을 준비시키거나 해체하는 일을 한다.                                         
//
@WebListener
public class ContextLoaderListener implements ServletContextListener{
  // ServletContextListener 메서드가 default처리가 되어 있어 다른 인터페이스와 달리 
  // 원하는 것만 오버라이딩
  @Override
  public void contextInitialized(ServletContextEvent sce) {


    System.out.println("공유 자원을 준비중!");
    try { 
      Class.forName("org.mariadb.jdbc.Driver");

      Connection con = DriverManager.getConnection(
          "jdbc:mariadb://localhost:3306/studydb","study","1111");

      ServletContext ctx = sce.getServletContext();
      ctx.setAttribute("boardDao", new MariaDBBoardDao(con));
      ctx.setAttribute("memberDao", new MariaDBMemberDao(con));
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

}
profile
객체지향 치코리타와 함께하는 dev_travel

0개의 댓글