JAVA μΉ νλ‘μ νΈμμ Properties νμΌ νμ©νκΈ°
JAVA μΉ νλ‘μ νΈμμ Properties νμΌ νμ©νκΈ°
μΉ μ΄ν리μΌμ΄μ μ΄ μμνμλ§μ κ°μ ΈμμΌνλ μ 보μ κ°μ κ²λ€μ νλ‘νΌν° νμΌμ μμ±ν΄μ κ΄λ¦¬ν μ μλ€. μΉ νλ‘μ νΈμμ νλ‘νΌν° νμΌμ μμ±ν΄μ κΊΌλ΄ μ¬μ©νλ λ°©λ²μ λν΄ μμ보μ.
νλ‘νΌν° νμΌ μμ±νκΈ°
νλ‘νΌν° νμΌμ ν λΌμΈμ key=value νμμΌλ‘ μμ±νκ³ .properties νμ₯μλ₯Ό λΆμ¬ μ μ₯νλ€.
μλ₯Ό λ€μ΄ λ€μκ³Ό κ°μ΄ μμ±ν μ μλ€.
νλ‘νΌν° νμΌ λ‘λ© λ° λ΄μ© κ°μ Έμ€κΈ°(μ½κΈ°)
java.util.Properties ν΄λμ€λ₯Ό μ¬μ©ν΄μ νλ‘νΌν° νμΌμ λ€λ£° μ μλ€. Propertiesλ key=value νμμ ν μ€νΈ νμΌμ λ€λ£° λ μ¬μ©νλ ν΄λμ€μ΄λ€.
μλμ κ°μ΄ νλ‘νΌν° νμΌμ λ‘λνμ¬ λ΄μ©μ κ°μ Έμ¬ μ μλ€.
1
2
3
4
5
6
|
Properties properties = new Properties();
properties.load(new FileReader("C:\\workspace\\intellij-project\\JavaWebProgramming\\out\\artifacts\\Member_MiniMVCFramework_war_exploded\\WEB-INF\\application-context.properties"));
for (Object object: properties.keySet()) {
System.out.println(object + " = " + properties.get(object));
}
|
cs |
μ€ν κ²°κ³Ό
/student/list.do = com.atoz_develop.spms.controls.StudentListController
/student/delete.do = com.atoz_develop.spms.controls.StudentDeleteController
studentDao = com.atoz_develop.spms.dao.MySqlStudentDao
/student/update.do = com.atoz_develop.spms.controls.StudentUpdateController
jndi.dataSource = java:comp/env/jdbc/knou
/student/add.do = com.atoz_develop.spms.controls.StudentAddController
/auth/login.do = com.atoz_develop.spms.controls.LogInController
/auth/logout.do = com.atoz_develop.spms.controls.LogOutController
Propertiesμ load() λ©μλλ FileReaderλ₯Ό ν΅ν΄ μ½μ΄λ€μΈ νλ‘νΌν° λ΄μ©μ ν€-κ° ννλ‘ λ΄λΆ λ§΅μ 보κ΄νλ€.
web.xmlμ νλ‘νΌν° νμΌ κ²½λ‘ μ€μ
νλ‘νΌν° νμΌμ κ²½λ‘λ₯Ό νλμ½λ©νμ§ μκ³ web.xmlλ‘λΆν° μ½μ΄μ€κ² μ²λ¦¬ν΄λ³΄μ.
web.xmlμ 컨ν μ€νΈ νλΌλ―Έν°λ₯Ό μΆκ°νκ³ νλΌλ―Έν° κ°μ νλ‘νΌν° νμΌμ κ²½λ‘λ₯Ό μμ±νλ€.
web.xml
1
2
3
4
|
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.properties</param-value>
</context-param>
|
cs |
νλ‘νΌν° νμΌμ λ‘λνλ μ½λλ₯Ό λ€μκ³Ό κ°μ΄ λ³κ²½νλ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class MyServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext sc = req.getServletContext();
Properties properties = new Properties();
properties.load(new FileReader(sc.getRealPath(sc.getInitParameter("contextConfigLocation"))));
for (Object object: properties.keySet()) {
System.out.println(object + " = " + properties.get(object));
}
}
}
|
cs |
컨ν μ€νΈ νλΌλ―Έν°λ ServletContext κ°μ²΄μ getInitParameter() λ©μλλ‘ κ°μ κΊΌλΌ μ μλ€.
web.xmlμ μ€μ ν νλΌλ―Έν° μ΄λ¦μ λ겨 νλ‘νΌν° νμΌμ κ²½λ‘ μ 보λ₯Ό κ°μ Έμ¨λ€.
μ€ν κ²°κ³Όλ μ΄μ κ³Ό λμΌνλ€.