๐Ÿ’ Spring/Spring ํ•ต์‹ฌ ๊ธฐ์ˆ 

๋นˆ์„ ๋“ฑ๋กํ•˜๊ณ  ์˜์กด์„ฑ ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•

2020. 6. 9. 23:18
๋ฐ˜์‘ํ˜•

๋นˆ ์ด๋ž€?

  • ์Šคํ”„๋ง IoC ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌ ํ•˜๋Š” ๊ฐ์ฒด
  • ์Šคํ”„๋ง IoC ์ปจํ…Œ์ด๋„ˆ๋ž€?  BeanFactory ๋ฅผ ํ™•์žฅํ•˜๋Š” ApplicationContext๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.
  • ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํ•˜๋Š” ์ผ : ๋นˆ ์„ค์ • ์†Œ์Šค๋กœ ๋ถ€ํ„ฐ ๋นˆ ์ •์˜๋ฅผ ์ฝ์–ด๋“ค์ด๊ณ , ๋นˆ์„ ๊ตฌ์„ฑํ•˜๊ณ  ์ œ๊ณตํ•œ๋‹ค.
  • ( ๋นˆ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ, ์˜์กด ๊ด€๊ณ„ ์„ค์ •, ๋นˆ ์ œ๊ณต)

 

  • ์žฅ์         

    1. ์˜์กด์„ฑ ๊ด€๋ฆฌ

    2. ์Šค์ฝ”ํ”„

        - ์‹ฑ๊ธ€ํ†ค (๊ธฐ๋ณธ๊ฐ’)  : ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋งŒ ์‚ฌ์šฉ -> ๋น ๋ฅธ ์„ฑ๋Šฅ

        - ํ”„๋กœํ† ํƒ€์ž… : ๋งค๋ฒˆ ๋‹ค๋ฅธ ๊ฐ์ฒด

    3. ๋ผ์ดํ”„์‚ฌ์ดํด ์ธํ„ฐํŽ˜์ด์Šค

 

 

 

 

 

  • ๋นˆ ๋“ฑ๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ• ๋‘๊ฐ€์ง€

 

1. ๊ณ ์ „์ ์ธ ๋ฐฉ์‹์˜ ๋นˆ ๋“ฑ๋ก ๋ฐฉ๋ฒ• - ApplicationContext (XML)

<Application.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="bookService"
          class="com.example.sspringaplicationcontext.BookService">
        <property name="bookRepository" ref="bookRepository"/>
    </bean>
<!--property name="setter์˜ ์ด๋ฆ„" ref="์ฐธ์กฐํ•  ๋นˆ์˜ id"

์ฃผ์„ ๋‹จ์ถ•ํ‚ค!
1. ์ค„ ๋‹จ์œ„ : ctrl + /
2. ๋ธ”๋Ÿญ ๋‹จ์œ„ : ctrl + shift +
aa-->

    <bean id="bookRepository"
          class="com.example.sspringaplicationcontext.BookRepository">

    </bean>
</beans>

๋‹จ์  : ์ผ์ผํžˆ ํ•˜๋‚˜ํ•˜๋‚˜ ๋นˆ์„ ๋“ฑ๋กํ•ด์•ผ ํ•œ๋‹ค๋Š” ๋ถˆํŽธํ•จ์ด ์žˆ๋‹ค.

 


 

2. ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•œ ๋นˆ ๋“ฑ๋ก ๋ฐฉ๋ฒ• ( Java )

<BookService.class>

package com.example.sspringaplicationcontext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service /*Component๋ฅผ ํ™•์žฅํ•œ ๋นˆ์œผ๋กœ ๋“ฑ๋ก๊ฐ€๋Šฅํ•œ annotation*/
public class BookService {

    @Autowired /*์˜์กด์„ฑ ์ฃผ์ž…์„ ์œ„ํ•œ annotation*/
    BookRepository bookRepository;

    public void setBookRepository(BookRepository bookRepository){
        this.bookRepository = bookRepository;
    }
}

 

๋นˆ ์„ค์ • Annotation

 

@Component

  • @Repository
  • @Service
  • @Controller

 


 

 

์ž๋ฐ” ๋นˆ ์„ค์ • ํŒŒ์ผ - - ApplicationContext (Java)

<Application.class>

package com.example.sspringaplicationcontext;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.awt.print.Book;

@Configuration  //์ด๊ฒƒ์€ ๋นˆ ์„ค์ • ํŒŒ์ผ ์ด๋‹ค~ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด @Configuration์„ ๋“ฑ๋กํ•ด์ค€๋‹ค.
public class ApplicationConfig {

	//๋นˆ ๋“ฑ๋ก
    @Bean
    public BookRepository bookRepository(){
        return new BookRepository();
    }

    @Bean
    public BookService bookService(){
        BookService bookService = new BookService();
        bookService.setBookRepository(bookRepository()); //์˜์กด์„ฑ ์ฃผ์ž… ์ง์ ‘ ํ•ด์ฃผ๊ธฐ
        return bookService;
    }
}

 

์ด๋ ‡๊ฒŒ ์ž๋ฐ”๋กœ ๋งŒ๋“  ํŒŒ์ผ์€ ์–ด๋–ป๊ฒŒ ApplicationContext๋กœ ์‚ฌ์šฉํ•˜๋Š”๊ฐ€?

ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);

<DemoApplication.class>

package com.example.sspringaplicationcontext;

import javafx.application.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;

public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);


        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        System.out.println(Arrays.toString(beanDefinitionNames));
        BookService bookService = (BookService) context.getBean("bookService");
        System.out.println(bookService.bookRepository != null);
    }

}

 

์ž๋ฐ” ๋นˆ ์„ค์ • ํŒŒ์ผ์—์„œ ์˜์กด์„ฑ์„ ์ง์ ‘ ์ฃผ์ž… ํ•ด์ค˜๋„ ๋˜๊ณ , ์•„๋‹ˆ๋ฉด @Autowired๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.

 

์˜์กด์„ฑ ์ฃผ์ž… : @Autowired

<Application.class>

package com.example.sspringaplicationcontext;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.awt.print.Book;

@Configuration  //์ด๊ฒƒ์€ ๋นˆ ์„ค์ • ํŒŒ์ผ ์ด๋‹ค~ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด @Configuration์„ ๋“ฑ๋กํ•ด์ค€๋‹ค.
public class ApplicationConfig {

	//๋นˆ ๋“ฑ๋ก
    @Bean
    public BookRepository bookRepository(){
        return new BookRepository();
    }

    @Bean
    public BookService bookService(){
    	//์ง์ ‘ ์ฃผ์ž…ํ•ด์ค€ ๋ถ€๋ถ„ ์‚ญ์ œํ•˜๊ณ !
        //BookService bookService = new BookService();
        //bookService.setBookRepository(bookRepository()); 
        return bookService;
    }
}
package com.example.sspringaplicationcontext;

import org.springframework.beans.factory.annotation.Autowired;

public class BookService {

    @Autowired //์ด๋ ‡๊ฒŒ ํ•ด์ค˜๋„ ์˜์กด์„ฑ์ด ์ž๋™์œผ๋กœ ์ฃผ์ž…์ด ๋œ๋‹ค.
    BookRepository bookRepository;

    public void setBookRepository(BookRepository bookRepository){
        this.bookRepository = bookRepository;
    }
}

 

 ์•„๋ž˜ ์‚ฌ์ง„์œผ๋กœ ์ž๋™์œผ๋กœ ์˜์กด์„ฑ์ด ์ฃผ์ž…์ด ๋œ๊ฑธ ํ™•์ธ ํ• ์ˆ˜ ์žˆ๋‹ค.

System.out.println(bookService.bookRepository != null); ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๊ฐ’

 


 

์ปดํฌ๋„ŒํŠธ ์Šค์บ” ์„ค์ •

 

1. XML ์„ค์ •

<Application.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 https://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.example.sspringaplicationcontext"/>
<!-- "com.example.sspringaplicationcontext" ์ด ํŒจํ‚ค์ง€๋กœ ๋ถ€ํ„ฐ ๋นˆ์„ ์Šค์บ๋‹์„ ํ•ด์„œ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๊ฒ ๋‹ค.   -->

</beans>

ํŒจํ‚ค์ง€ ์ดํ•˜๋กœ ๋ถ€ํ„ฐ ์–ด๋…ธํ…Œ์ด์…˜์„ ์Šค์บ๋‹ํ•ด์„œ ๋นˆ์œผ๋กœ ๋“ฑ๋ก์„ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

 

 

2. ์ž๋ฐ” ์„ค์ •

<ApplicationConfig.class>

@Configuration
@ComponentScan(basePackageClasses = DemoApplication.class)
//ํด๋ž˜์Šค๊ฐ€ ์œ„์น˜ํ•œ๊ณณ์œผ๋กœ ๋ถ€ํ„ฐ ์ปดํฌ๋„ŒํŠธ ์Šค์บ๋‹์„ ํ•ด๋ผ.
//(๋ชจ๋“  ํด๋ž˜์Šค์— ๋ถ™์–ด์žˆ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์„ ์ฐพ์•„์„œ ๋นˆ์œผ๋กœ ๋“ฑ๋ก์„ํ•ด๋ผ.
public class ApplicationConfig {


}

์ด์ œ ๋นˆ์„ ์ผ์ผํžˆ ๋“ฑ๋กํ•˜๊ธฐ ๊ท€์ฐฎ๋‹ค. ์ž๋™์œผ๋กœ ๋“ฑ๋กํ•ด์ฃผ๋Š” @ComponentScan ์„ ์‚ฌ์šฉํ•ด๋ผ.

 

๋ฐ˜์‘ํ˜•
์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)
'๐Ÿ’ Spring/Spring ํ•ต์‹ฌ ๊ธฐ์ˆ ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • 3. IoC ์ปจํ…Œ์ด๋„ˆ 3๋ถ€ : @Autowired
  • 4. IoC ์ปจํ…Œ์ด๋„ˆ 4๋ถ€ : @Component์™€ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”
  • PSA( Portable Service Abstract )
  • AOP (Aspect-Oriented Programming)
iseunghan
iseunghan
๊พธ์ค€ํ•˜๊ฒŒ ์—ด์‹ฌํžˆ..
iseunghan
iseunghan

๊ณต์ง€์‚ฌํ•ญ

  • ์–ด์ œ๋ณด๋‹ค ๋‚˜์€ ์˜ค๋Š˜์ด ๋˜๊ธฐ ์œ„ํ•ด ๐Ÿ”ฅ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (260)
    • ๐Ÿ’ Spring (14)
      • ๊ฐœ๋… ๋ฐ ์ดํ•ด (2)
      • Spring ํ•ต์‹ฌ ๊ธฐ์ˆ  (24)
      • Spring REST API (8)
      • Spring MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ  (7)
      • Spring Security (23)
      • Spring in Action (1)
    • ๐ŸŒป JAVA (84)
      • ์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ (20)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ž๋ฃŒ๊ตฌ์กฐ (13)
      • ๋””์ž์ธ ํŒจํ„ด (7)
      • ์ •๋ฆฌ์ •๋ฆฌ์ •๋ฆฌ (43)
      • JUnit (1)
    • ๐Ÿ”– Snippets (3)
      • Javascript (3)
    • โš™๏ธ Devops (22)
      • โ› Git (11)
      • ๐Ÿณ Docker (6)
      • ๐Ÿง Linux (3)
      • ๐ŸŒˆ Jenkins (1)
      • ๐Ÿ“ฌ Kafka (1)
    • ๐Ÿ’ฌ ETC.. (4)
      • ๐Ÿ’ป macOS (2)
    • ๐ŸŒง๏ธ ORM (2)
      • JPA (2)
    • ๐Ÿ Python (1)
    • ๐Ÿ“š Databases (15)
      • ์˜ค๋ผํด๋กœ ๋ฐฐ์šฐ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐœ๋ก ๊ณผ ์‹ค์Šต(2ํŒ) (3)
      • RealMySQL 8.0 (8)
    • ๐Ÿ”ฅ Computer Science (5)
      • ๐Ÿ“ก ๋„คํŠธ์›Œํฌ (5)
    • ๐Ÿท๏ธ ํ˜‘์—… (1)
    • ๐Ÿ“œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ (38)
      • BAEKJOON\์ˆ˜ํ•™ 1, ์ˆ˜ํ•™ 2 (8)
      • BAEKJOON\์žฌ๊ท€ (5)
      • BAEKJOON\๋ธŒ๋ฃจํŠธ ํฌ์Šค (3)
      • BAEKJOON\์ •๋ ฌ (1)
      • BAEKJOON\๋ฐฑํŠธ๋ž˜ํ‚น (5)
      • BAEKJOON\BFS, DFS (6)
      • BAEKJOON\์ด๋ถ„ํƒ์ƒ‰ (1)
      • BAEKJOON\๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ (9)
      • BAEKJOON\๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜ (0)
    • โœจ ISEUNGHAN (1)

์ธ๊ธฐ ๊ธ€

์ตœ๊ทผ ๊ธ€

์ „์ฒด
์˜ค๋Š˜
์–ด์ œ
๋ฐ˜์‘ํ˜•
hELLO ยท Designed By ์ •์ƒ์šฐ.
iseunghan
๋นˆ์„ ๋“ฑ๋กํ•˜๊ณ  ์˜์กด์„ฑ ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”

๊ฐœ์ธ์ •๋ณด

  • ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ
  • ํฌ๋Ÿผ
  • ๋กœ๊ทธ์ธ

๋‹จ์ถ•ํ‚ค

๋‚ด ๋ธ”๋กœ๊ทธ

๋‚ด ๋ธ”๋กœ๊ทธ - ๊ด€๋ฆฌ์ž ํ™ˆ ์ „ํ™˜
Q
Q
์ƒˆ ๊ธ€ ์“ฐ๊ธฐ
W
W

๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€

๊ธ€ ์ˆ˜์ • (๊ถŒํ•œ ์žˆ๋Š” ๊ฒฝ์šฐ)
E
E
๋Œ“๊ธ€ ์˜์—ญ์œผ๋กœ ์ด๋™
C
C

๋ชจ๋“  ์˜์—ญ

์ด ํŽ˜์ด์ง€์˜ URL ๋ณต์‚ฌ
S
S
๋งจ ์œ„๋กœ ์ด๋™
T
T
ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ ์ด๋™
H
H
๋‹จ์ถ•ํ‚ค ์•ˆ๋‚ด
Shift + /
โ‡ง + /

* ๋‹จ์ถ•ํ‚ค๋Š” ํ•œ๊ธ€/์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ‹ฐ์Šคํ† ๋ฆฌ ๊ธฐ๋ณธ ๋„๋ฉ”์ธ์—์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.