3 ๋ถ„ ์†Œ์š”

๐Ÿ“š SPRING


๐Ÿ“š Dependency Injection ( ์˜์กด์„ฑ ์ฃผ์ž… )

Bean

Spring Bean์€ ๊ธฐ๋ณธ์ ์œผ๋กœ Singleton ์œผ๋กœ ๋งŒ๋“ค์–ด์ง„๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“  ๋นˆ์˜ instance๋Š” ๋™์ผํ•˜๋‹ค.
์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํ•ญ์ƒ ์ƒˆ๋กœ์šด instance๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ scope๋ฅผ prototype ์œผ๋กœ ์„ค์ •ํ•ด์•ผํ•œ๋‹ค.

@Service("memberService")
@Scope("singleton")
public class MemberServiceImpl implements MemberService{

    @Override
    public int registerMember(MemberDto memberDto){
        return 0;
    }
}

xml bean ์„ค์ • ( prototype )

<bean id="memberService" class="com.test.hello.service2.MemberServiceImpl" scope="prototype"/>


Bean์˜ ์ƒ์„ฑ ๋ฒ”์œ„ ์ง€์ •

  • singleton
    • spring container ๋‹น ํ•˜๋‚˜์˜ instance bean๋งŒ ์ƒ์„ฑ (default)
  • prototype
    • container์— bean์„ ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด instance ์ƒ์„ฑ
  • request
    • HTTP Request๋ณ„๋กœ ์ƒˆ๋กœ์šด instance ์ƒ์„ฑ
  • session
    • HTTP Session๋ณ„๋กœ ์ƒˆ๋กœ์šด isntance ์ƒ์„ฑ


bean ๋ฉ”ํƒ€ ์ •๋ณด์˜ ํ‘œํ˜„ ๋ฐฉ์‹์—๋Š” XML Document, Annotation, Java Code ๊ฐ€ ์žˆ๋Š”๋ฐ ์ด๊ฒƒ๋“ค์„ ํ†ตํ•ด BeanDefinition meta data๊ฐ€ ๋˜์–ด IoC Container์— ์ „์†ก๋œ๋‹ค.

Spring Bean ์„ค์ •

XML

  • XML๋ฌธ์„œ ํ˜•ํƒœ๋กœ ๋นˆ์˜ ์„ค์ • ๋ฉ”ํƒ€ ์ •๋ณด๋ฅผ ๊ธฐ์ˆ 
  • ํƒœ๊ทธ๋ฅผ ํ†ตํ•ด์„œ ์„ธ๋ฐ€ํ•œ ์ œ์–ด ๊ฐ€๋Šฅ

servlet-context.xml

<bean id="memberDao" class="com.test.hello.dao.MemberDaoImpl" />

<bean id="memberService" class="com.test.hello.service2.MemberServiceImpl" scope="prototype" >
    <property name="memberDao" ref="memberDao" />
</bean>

Annotation

  • ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์ง€๊ณ  ๋นˆ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์•„์งˆ ์ˆ˜๋ก XML ํŒŒ์ผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋ฒˆ๊ฑฐ๋กญ๋‹ค.
  • ํŠน๋ณ„ํ•œ annotation์„ ๋ถ€์—ฌํ•ด ์ฃผ๋ฉด ์ž๋™์œผ๋กœ ๋นˆ ๋“ฑ๋ก์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • โ€œ์˜ค๋ธŒ์ ํŠธ ๋นˆ ์Šค์บ๋„ˆโ€ ๋กœ โ€œ๋นˆ ์Šค์บ๋‹โ€์„ ํ†ตํ•ด ์ž๋™ ๋“ฑ๋กํ•œ๋‹ค.
    • ๋นˆ ์Šค์บ๋„ˆ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํด๋ž˜์Šค ์ด๋ฆ„์„ ๋นˆ์˜ ์•„์ด๋””๋กœ ์‚ฌ์šฉ
    • ์ •ํ™•ํžˆ๋Š” ํด๋ž˜์Šค ์ด๋ฆ„์˜ ์ฒซ ๊ธ€์ž๋งŒ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พผ ๊ฒƒ์„ ์ด์šฉ
@Service
public class MemberServiceImpl implements MemberService{

    // annotation์„ ์‚ฌ์šฉํ•œ ๋นˆ ๋“ฑ๋ก ( Autowired )
    @Autowired
    private MemberDao memberDao;

    @Override
    public int registerMember(MemberDto memberDto){
        return memberDao.registerMember(memberDto);
    }
}

Annotation์œผ๋กœ ๋นˆ์„ ์„ค์ •ํ•  ๊ฒฝ์šฐ ๋ฐ˜๋“œ์‹œ component-scan์„ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.
servlet-context.xml

<context:component-scan base-package="com.test.hello.*" />


  • Stereotype annotation ์ข…๋ฅ˜
    • ๋นˆ ์ž๋™๋“ฑ๋ก์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” annotation
    • ๋นˆ ์ž๋™์ธ์‹์„ ์œ„ํ•œ annotation์ด ์—ฌ๋Ÿฌ๊ฐ€์ง€์ธ ์ด์œ 
      • ๊ณ„์ธต๋ณ„๋กœ ๋นˆ์˜ ํŠน์„ฑ์ด๋‚˜ ์ข…๋ฅ˜๋ฅผ ๊ตฌ๋ถ„
      • AOP Pointcut ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ํŠน์ • annotation์ด ๋‹ฌ๋ฆฐ ํด๋ž˜์Šค๋งŒ ์„ค์ • ๊ฐ€๋Šฅ
      • ํŠน์ • ๊ณ„์ธต์˜ ๋นˆ์— ๋ถ€๊ฐ€ ๊ธฐ๋Šฅ ๋ถ€์—ฌ
Stereotype ์ ์šฉ ๋Œ€์ƒ
@Repository Data Access Layer์˜ DAO ๋˜๋Š” Repository ํด๋ž˜์Šค์— ์‚ฌ์šฉ
DataAccessException ์ž๋™๋ณ€ํ™˜๊ณผ ๊ฐ™์€ AOP์˜ ์ ์šฉ ๋Œ€์ƒ์„ ์„ ์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
@Service Service Layer์˜ ํด๋ž˜์Šค์— ์‚ฌ์šฉ
@Controller Presentation Layer์˜ MVC Controller์— ์‚ฌ์šฉ
์Šคํ”„๋ง ์›น ์„œ๋ธ”๋ฆฟ์— ์˜ํ•ด ์›น ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ ๋นˆ์œผ๋กœ ์„ ์ •
@Component ์œ„์˜ Layer ๊ตฌ๋ถ„์„ ์ ์šฉํ•˜๊ธฐ ์–ด๋ ค์šด ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ์— ์„ค์ •

Spring Bean์˜ ์ƒ๋ช… ์ฃผ๊ธฐ


DI ( Dependency Injection )

DI ( Dependency Injection ) spring ์„ค์ •

Spring ์„ค์ • - xml

xml ๋ฌธ์„œ ์ด์šฉ

  • Application์—์„œ ์‚ฌ์šฉํ•  Spring ์ž์›๋“ค์„ ์„ค์ •ํ•˜๋Š” ํŒŒ์ผ
  • Spring Container๋Š” ์„ค์ •ํŒŒ์ผ์— ์„ค์ •๋œ ๋‚ด์šฉ์„ ์ฝ์–ด Application์—์„œ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณต
  • Root tag๋Š”
  • ํŒŒ์ผ๋ช…์€ ์ƒ๊ด€ ์—†๋‹ค.


applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

๊ธฐ๋ณธ ์„ค์ • - bean ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ์ฃผ์ž…

  • ์ฃผ์ž… ํ•  ๊ฐ์ฒด๋ฅผ ์„ค์ • ํŒŒ์ผ์— ์„ค์ •
    • : ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•  Bean๊ฐ์ฒด๋ฅผ ์„ค์ •
  • ๊ธฐ๋ณธ ์†์„ฑ
    • name : ์ฃผ์ž… ๋ฐ›์„ ๊ณณ์—์„œ ํ˜ธ์ถœ ํ•  ์ด๋ฆ„ ์„ค์ •
    • id : ์ฃผ์ž… ๋ฐ›์„ ๊ณณ์—์„œ ํ˜ธ์ถœ ํ•  ์ด๋ฆ„ ์„ค์ • ( ์œ ์ผ ๊ฐ’ )
    • class : ์ฃผ์ž… ํ•  ๊ฐ์ฒด์˜ ํด๋ž˜์Šค
    • factory-method : Singleton ํŒจํ„ด์œผ๋กœ ์ž‘์„ฑ๋œ ๊ฐ์ฒด์˜ factory ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
    • ref : ์ฐธ์กฐ๋ฐ›์„ ํด๋ž˜์Šค์˜ id๋ช…


applicationContext.xml

<bean id="memberDao" class="com.test.hello.dao.MemberDaoImpl" />

<bean id="memberService" class="com.test.hello.service2.MemberServiceImpl" scope="prototype" >
    <property name="memberDao" ref="memberDao" />
</bean>

๊ธฐ๋ณธ ์„ค์ • - bean ๊ฐ์ฒด ์–ป๊ธฐ

  • ์„ค์ • ํŒŒ์ผ์— ์„ค์ •ํ•œ bean์„ Container๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ฃผ์ž…๊ธฐ ์—ญํ• ์˜ api๋ฅผ ํ†ตํ•ด ์ฃผ์ž… ๋ฐ›๋Š”๋‹ค.


java file

ApplicationContext context = new ClassPathXmlApplicationContext("com/test/hello/controller4/applicationContext.xml");
CommonService memberService = context.getBean("memberService", MemberService.class);
CommonService adminService = context.getBean("adminService", AdminService.class);

Spring ์„ค์ • - annotation

Annotation : ๋ฉค๋ฒ„๋ณ€์ˆ˜์— ์ง์ ‘ ์ •์˜ํ•˜๋Š” ๊ฒฝ์šฐ setter method๋ฅผ ๋งŒ๋“ค์ง€ ์•Š์•„๋„ ๋จ
@Resource, @Autowired, @Inject๊ฐ€ ์žˆ๋Š”๋ฐ @Autowired๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
@Autowired๋Š” Spring Framework์—์„œ ์ง€์›ํ•˜๋Š” Dependency ์ •์˜ ์šฉ๋„์˜ Annotation์œผ๋กœ, Spring Framework์— ์ข…์†์ ์ด๊ธด ํ•˜์ง€๋งŒ ์ •๋ฐ€ํ•œ Dependency Injection์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์— ์œ ์šฉํ•˜๋‹ค.

์‚ฌ์šฉ๋ฒ•์€ ์ƒ์„ฑ์ž์— ๋“ฑ๋ก ํ•˜๊ณ  ๋™์ผํ•œ ํƒ€์ž…์˜ bean์ด ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ์—๋Š” @Qualifier(โ€œnameโ€) ์œผ๋กœ ์‹๋ณ„ํ•œ๋‹ค.

public class MemberServiceImpl implements MemberService{

    @Autowired
    @Qualifier("mdao")   // ๋™์ผ ํƒ€์ž…์ด ์—ฌ๋Ÿฌ๊ฐœ ์ผ ๊ฒฝ์šฐ
    MemberDao memberDao;

    ...
}