Spring
Spring-FrameWork
Spring-FrameWork์ด๋?
Java FrameWork์ค ํ๋๋ก ํ์ ์์ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋์ด์ง๋ FrameWork์ค ํ๋์ด๋ค.
์คํ๋ง ํ๋ ์์ํฌ(Spring Framework)๋ ์๋ฐ ํ๋ซํผ์ ์ํ ์คํ ์์ค ์ ํ๋ฆฌ์ผ์ด์
ํ๋ ์์ํฌ๋ก์
๊ฐ๋จํ ์คํ๋ง(Spring)์ด๋ผ๊ณ ๋ ํ๋ค.
๋์ ์ธ ์น ์ฌ์ดํธ๋ฅผ ๊ฐ๋ฐํ๊ธฐ ์ํ ์ฌ๋ฌ ๊ฐ์ง ์๋น์ค๋ฅผ ์ ๊ณตํ๊ณ ์๋ค.
Spring-FrameWork ์ค์น
Spring์ ์ค์น๋ฐฉ๋ฒ์ ์ฌ๋ฌ๊ฐ์ง๊ฐ ์๋ค.
- IDE(Eclipse)์์ ์ค์น (maven)
- Spring library ( ์ง์ x )
- STS(?)
Spring library๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ผ๋ก๋ Spring ์์ ์ ๊ณตํ๋
library๋ฅผ ๋ค์ด๋ฐ์ class path๋ฅผ ์ก์ ์ฌ์ฉ ํ๋ ๋ฐฉ๋ฒ์ด๋ค.
ํ์ง๋ง ์ด ๋ฐฉ๋ฒ์ ๋์ด์ ์ง์ํ์ง ์๋ ๊ฑฐ ๊ฐ๋ค.
https://okky.kr/article/231724 ์ฌ์ดํธ๋ฅผ ์ฐธ๊ณ ํ์๋ฉด modul์ด ๋์ฑ ๋ ๋ค์ํด์ ธ
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ฐ๋ก๋ฐ๋ก ๋ฐฐํฌํ๋ ๋ฐฉ๋ฒ์ด ํจ์จ์ ์ด์ง ์์ ๊ทธ๋ฐ๊ฒ ๊ฐ๋ค.
์ง๊ธ์ maven์ ํตํด ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ง์ด ์ฌ์ฉํ๋ค.
Spring Open Source
Spring์ Open Source์ด๋ค.
OpenSource๋ ์๋ ์คํ์์ค ์ํํธ์จ์ด(Open Source Software, OSS)๋ฅผ ๋ปํ๋ ์ฉ์ด์
๋๋ค.
์คํ์์ค ์ํํธ์จ์ด๋ ๊ณต๊ฐ์ ์ผ๋ก ์ก์ธ์คํ ์ ์๊ฒ ์ค๊ณ๋์ด ๋๊ตฌ๋ ์์ ๋กญ๊ฒ ํ์ธ, ์์ ,
๋ฐฐํฌ ํ ์ ์๋ ์ฝ๋์
๋๋ค.
Open Source์ ๊ฐ๋ ์ ์ ์ฉ์์ผ Spring์ ํ์ฅ์ฑ๊ณผ ์ฐ๋์ฑ(?)์ ๊ฐ์ง๊ฒ ๋๋ค.
๋ํ์ ์ธ ์๋ก Spring์ JDBC๋ฅผ ์ง์ํ๋ library๋ณด๋ค ๋ ํ์ฉ๋๊ฐ ๋์ Mubatis๋ก
ํ์ฅํ์ฌ ๋ง์ด ์ฌ์ฉํ๋ค.
BeanContainer
Container๋ ์ธ์คํด์ค์ ์๋ช ์ฃผ๊ธฐ๋ฅผ ๊ด๋ฆฌํ๋ฉฐ, ์์ฑ๋ ์ธ์คํด์ค๋ค์๊ฒ ์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋๋ก ํ๋ ๊ฒ์ด๋ค.
์ฆ Container๋ ๊ฐ๋ฐ์๊ฐ ์์ฑํ ์ฝ๋์ ์ฒ๋ฆฌ๊ณผ์ ์ ์์๋ฐ์ ๋ ๋ฆฝ์ ์ธ ์กด์ฌํ๊ณ ์๊ฐํ๋ฉด ๋๋ค. (ActionServletโฆ.)
์ด๋ Spring Container์์ ์์ฑ๋๋ ๊ฐ์ฒด๋ฅผ Bean์ด๋ผ๊ณ ํ๋ค.
Spring์์ Bean์ ๋ณดํต Singleton์ผ๋ก ์กด์ฌํ๋ค.
- Singleton : ์ด๋ค Class๊ฐ ์ต์ด ํ๋ฒ๋ง ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ๊ณ (Static) ๊ทธ ๋ฉ๋ชจ๋ฆฌ์ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ์ฌ์ฉํ๋ ๋์์ธ ํจํด
- FQCN (Fully Qualified Class Name) : ํจํค์ง๋ช ์ ํฌํจํ ํ ๋ค์
- POJO (Prain Old Java Object) : ์ค๋๋ ๋ฐฉ์์ ์์ํ ์๋ฐ ์ฝ๋ / Java Bean
ActionServlet
public class ActionServlet extends HttpServlet {
///Field
private RequestMapping requestMapping;
///Method
@Override
public void init() throws ServletException {
super.init();
String resources=getServletConfig().getInitParameter("resources");
requestMapping=RequestMapping.getInstance(resources);
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String url = request.getRequestURI();
String contextPath = request.getContextPath();
String reqeustPath = url.substring(contextPath.length());
System.out.println("\nActionServlet.service() RequestURI : "+reqeustPath);
try{
Action action = requestMapping.getAction(reqeustPath);
action.setServletContext(getServletContext());
String resultPage=action.execute(request, response);
String path=resultPage.substring(resultPage.indexOf(":")+1);
if(resultPage.startsWith("forward:")){
HttpUtil.forward(request, response, path);
}else{
HttpUtil.redirect(response, path);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
RequestMapping
public class RequestMapping {
private static RequestMapping requestMapping;
private Map<String, Action> map;
private Properties properties;
private RequestMapping(String resources) {
map = new HashMap<String, Action>();
InputStream in = null;
try{
in = getClass().getClassLoader().getResourceAsStream(resources);
properties = new Properties();
properties.load(in);
System.out.println("properties ์ธ์คํด์ค");
}catch(Exception ex){
System.out.println(ex);
throw new RuntimeException("actionmapping.properties ํ์ผ ๋ก๋ฉ ์คํจ :" + ex);
}finally{
if(in != null){
try{ in.close(); } catch(Exception ex){}
}
}
}
public synchronized static RequestMapping getInstance(String resources){
if(requestMapping == null){
requestMapping = new RequestMapping(resources);
System.out.println("RequestMapping ์ธ์คํด์ค ์์ฑ");
}
return requestMapping;
}
public Action getAction(String path){
System.out.println("\nRequestMapping Start......");
Action action = map.get(path);
if(action == null){
String className = properties.getProperty(path);
System.out.println("prop : " + properties);
System.out.println("path : " + path);
System.out.println("className : " + className);
className = className.trim();
try{
Class c = Class.forName(className);
Object obj = c.newInstance();
System.out.println("RequestMapping ์ธ์คํด์คํ ๋ obj : "+obj);
if(obj instanceof Action){
map.put(path, (Action)obj);
action = (Action)obj;
}else{
throw new ClassCastException("Classํ๋ณํ์ ์ค๋ฅ ๋ฐ์ ");
}
}catch(Exception ex){
System.out.println(ex);
throw new RuntimeException("Action์ ๋ณด๋ฅผ ๊ตฌํ๋ ๋์ค ์ค๋ฅ ๋ฐ์ : " + ex);
}
}
return action;
}
}
์์ ๊ฐ์ด ์๋ฐ์ ๋ชจ๋ ์คํ๊ณผ ํ๋์ ๊ด๋ฆฌํ๋ ActionServlet๊ณผ ๋งตํ์ ํด์ฃผ๋ Request๋ฅผ ์ง์ ์ฝ๋๋ฅผ์์ฑํ์ฌ ๋ง๋ค์๋ค.
ํ์ง๋ง Spring์์๋ ์๋์ ๊ฐ์ด BeanFactory๋ฅผ ์ ๊ณตํด์ค๋ค.
package spring.service.hello.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import spring.service.hello.Hello;
/*
* ใ
Hello Bean ์ฌ์ฉํ๋ Application
* ใ
Hello Bean ๊ณผ dependency Relation
* ใ
Spring Framework ์์ ์ ๊ณตํ๋ API๋ฅผ ์ฌ์ฉ instance ์์ฑ
==> Spring Framework ์ ๋ค์ํ ๋ฉํ๋ฐ์ดํฐ ์ค xml ์ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉํ๋ฉฐ,
instance ์์ฑ, ์ด๊ธฐํ, ๊ฐ์ฒด์ ์ํธ ๊ด๊ณ๋ฅผ xml ์ ์ ์ธ์ ์ผ๋ก ๊ธฐ์ .
<bean id="hello" class="spring.services.hello.Hello"/>
==> Spring Framework :๋ชจ๋ Data Type(Bean/POJO) ์์ฑ๊ด๋ฆฌํ๋ Factory
*/
public class HelloTestApp03UseSpring {
public static void main(String[] args) {
BeanFactory factory =
new XmlBeanFactory( new FileSystemResource
("./src/main/resources/config/hello.xml") );
Hello hello = (Hello)factory.getBean("hello");
hello.printMessage();
}
}//end of class
์์๊ฐ์ด ์ฝ๋๋ฅผ ์ง์ ์์ฑํ์ง ์๋๋ผ๊ณ ํธ๋ฆฌํ๊ฒ ํ๋์ค์ ์ฝ๋๋ง์ผ๋ก ์ด๋ฏธ ๋ง๋ค์ด์ง ํ๋ ์์ํฌ๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
์๋ฐ๋ Mete-data์ ํ์ฅ์๋ก .properties๋ฅผ ์ฌ์ฉํ๋๋ฐ
Spring์ .xml ์ ์ฌ์ฉํ๋ฉฐ getbin์ ํตํด์ ๋งตํ๋ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ ์ฌ ์ ์๋ค.
Leave a comment