๋กœ์ผ“๐Ÿพ
article thumbnail

 

@Autowired๋ฅผ ์ด์šฉํ•˜๋ฉด xml์—์„œ ๋นˆ์„ ์ง์ ‘ ์„ค์ •ํ•  ํ•„์š”์—†์ด, ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์•Œ์•„์„œ ์›ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์ฃผ์ž…์‹œ์ผœ์ค๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๋ฐ”๋กœ ์˜ˆ์‹œ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

 


l ์˜ˆ์‹œ

 

package com.spring.ex02;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller("loginController")
@RequestMapping(value="/test")
public class LoginController {
	@Autowired//=> ์ฃผ๋ชฉ
	LoginVO loginVO;
	
	@RequestMapping(value="/login.do", method = {RequestMethod.GET, RequestMethod.POST})
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response) throws Exception{
		request.setCharacterEncoding("utf-8");
		ModelAndView mv = new ModelAndView();
		
		mv.setViewName("result");
		loginVO.setId("๊น€๋ณ‘์ค€");
		System.out.println(loginVO.getId());
		return mv;
	}
}

 

์ปจํŠธ๋กค๋Ÿฌ ์•ˆ์— @Autowired๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ LoginVO ํด๋ž˜์Šค๋ฅผ ์ฃผ์ž…์‹œ์ผœ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์• ๋„ˆํ…Œ์ด์…˜์„ ์‚ฌ์šฉ์•ˆํ•  ๊ฒฝ์šฐ xml์—์„œ ๋นˆ ์„ค์ •์„ ํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ ํ”„๋กœ์ ํŠธ๊ฐ€ ๋ณต์žกํ•ด์ง€๋ฉด ์ƒ๋‹นํžˆ ์–ด๋ ค์›Œ์ง‘๋‹ˆ๋‹ค.

 

package com.spring.ex02;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

@Component("LoginVO")
public class LoginVO {
	private String id;
	private String pw;
	
	public void setId(String id) {
		this.id = id;
	}
	
	public String getId() {
		return this.id;
	}
	
	public void setPw(String pw) {
		this.pw = pw;
	}
	
	public String getPw() {
		return this.pw;
	}
}

 

๊ผญ LoginVO์—์„œ๋„ @Component๋ฅผ ์ด์šฉํ•˜์—ฌ ๋นˆ์œผ๋กœ ์„ค์ •ํ•ด์ฃผ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ์ปจํŠธ๋กค๋Ÿฌ๋ฉด @Controller ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋“ฑ๋กํ•˜๊ณ , ์„œ๋น„์Šค์ด๋ฉด @Service๋ฅผ ์ด์šฉํ•˜์—ฌ ๋“ฑ๋กํ•˜๊ณ , DAO์ด๋ฉด @Repository๋ฅผ ์ด์šฉํ•˜์—ฌ ๋“ฑ๋กํ•˜๊ณ , ๋‹จ์ˆœ ๋นˆ์„ ๋“ฑ๋กํ•˜๋ ค๋ฉด @Component๋ฅผ ์ด์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์ถœ๋ ฅ๊ฒฐ๊ณผ

import๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฃผ์ž…ํ•˜์ง€๋„ ์•Š์•˜๊ณ , xml์— ์ง์ ‘ ์„ค์ •๋„ ์•ˆํ•ด์ฃผ์—ˆ์ง€๋งŒ ๋ฌธ์ œ์—†์ด LoginVO ํด๋ž˜์Šค๋ฅผ ์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค์— ์ฃผ์ž…์‹œ์ผœ ์ œ๋Œ€๋กœ๋œ ๊ฐ’์ด ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ •ํ™•ํžˆ ๋งํ•˜์ž๋ฉด xml์— ์„ค์ •๋˜์–ด์žˆ์ง€๋งŒ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ž๋™์œผ๋กœ ํ•ด์ค€ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ๋ฐ”๋กœ ์Šคํ”„๋ง์˜ ํŠน์„ฑ์ธ IoC์ž…๋‹ˆ๋‹ค. 

 

 

[Spring] ์Šคํ”„๋ง DI, IoC ์ดํ•ดํ•˜๊ธฐ

์ž๋™์ฐจ์—์„œ ๋งŒ์•ฝ ์—”์ง„์ด ์˜ค๋ž˜๋˜์„œ ์—”์ง„์„ ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค๋ฉด, ์ž๋™์ž ์ „์ฒด๊ฐ€ ์•„๋‹Œ ์—”์ง„๋งŒ ๋ฝ‘์•„์„œ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ๋„ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•˜๋ฉด ๋‹ค๋ฅธ ํด๋ž˜์Šค์— ์˜ํ–ฅ

kingofbackend.tistory.com

 

 

์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

์งˆ๋ฌธ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.

 

"๋‚œ ๋ฐ˜๋“œ์‹œ ๋ฐฑ์—”๋“œ ์™•์ด ๋ ๊ฑฐ์•ผ"

profile on loading

Loading...