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์„ ํ†ตํ•ด์„œ ๋งตํ•‘๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

Categories:

Updated:

Leave a comment