티스토리 뷰

@Configuration

java config(스프링 설정 클래스)라는 의미를 가짐

java config로 설정을 할 클래스 위에는 @Configuration이 붙어 있어야 한다

 

Example01: java config 내부에서 빈 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Configuration
public class ApplicationConfig {
    @Bean
    public Car car(Engine e) {
        Car c = new Car();
        c.setEngine(e);
        return c;
    }
    
    @Bean
    public Engine engine() {
        return new Engine();
    }
}
 
public class Exam {
 
    public static void main(String[] args) {
        ApplicationContext ac = new AnnotationConfigApplicationContext(ApplicationConfig.class);
           
        Car car = (Car)ac.getBean("car");
        car.run();
        
    }
}
cs

 

line 16: ApplicationContext #스프링 컨테이너

= IoC/Di 컨테이너

 

line 16: AnnotationConfigApplicationContext

ApplicationContext 중 어노테이션 정보를 읽어오는 공장

설정파일(ApplicationConfig)을 읽어서 @Bean이 붙어 있는 메소드들을 자동으로 실행한다.

그 결과로 리턴하는 객체들을 싱글턴으로 관리한다.

AnnotationConfigApplicationContext가 JavaConfig클래스를 읽어들여 IoC와 DI를 적용한다

 

ApplicationContext 활용)

#DataSource Test

#DAO Test

 

Example02: ComponentScan 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@Configuration
@ComponentScan("mypackage")
public class ApplicationConfig {
}
 
@Component
public class Engine {
    public Engine() {
        System.out.println("Engine 생성자");
    }
    
    public void exec() {
        System.out.println("엔진이 동작합니다.");
    }
}
 
@Component
public class Car {
    @Autowired
    private Engine v8;
    
    public Car() {
        System.out.println("Car 생성자");
    }
    
    public void run() {
        System.out.println("엔진을 이용하여 달립니다.");
        v8.exec();
    }
}
 
public class Exam {
 
    public static void main(String[] args) {
        ApplicationContext ac = new AnnotationConfigApplicationContext(ApplicationConfig.class);
           
        Car car = ac.getBean(Car.class);
        car.run();
        
    }
}
cs

 

line 2: @ComponentScan

파라미터로 들어온 패키지 이하에서 @Controller, @Service, @Repository, @Component 어노테이션이 붙어 있는 클래스를 찾아 메모리에 몽땅 올려 DI를 주입한다. 이러한 어노테이션이 붙어 있지 않은 객체는 @Bean어노테이션을 이용하여 직접 생성해주는 방식으로 클래스들을 관리하면 편리하다.

 

line 6: 기존의 @Bean 대신 @Component 사용

 

line 19: @Autowired

주입하려고 하는 객체(bean)와 동일한 타입의 객체를 찾아 자동으로 주입한다.

ex) Dao dao = new Dao(); 대신에

    @Autowired

    Dao dao; 사용

동일한 객체가 2개 이상인 경우, 스프링 컨테이너는 자동주입대상 객체를 판단하지 못해 Exception 발생

 

빈(Bean) 객체의 생명주기는 스프링 컨테이너의 생명주기와 같이 한다.

line 35: 컨테이너 생성

line 37: getBean()을 이용한 빈 객체 이용

(ac.close() : 컨테이너 종료, 빈도 소멸)

 

 

[실행 결과]

Engine 생성자

Car 생성자

엔진을 이용하여 달립니다.

엔진이 동작합니다.

 

 


[ApplicationConfig.java]

1
2
3
4
5
6
@Configuration
@ComponentScan(basePackages = {"mypackage.dao""mypackage.service"})
@Import(DBConfig.class)
public class ApplicationConfig {
 
}
cs

 

line 3: @Import

설정파일을 여러 개로 나눠서 작성할 수 있다

 

line 3: 데이터베이스에 대한 설정은 #DBConfig

'JAVA > Spring' 카테고리의 다른 글

[Spring] Connection Pool / Spring JDBC  (0) 2019.10.10
[Spring] Controller(Handler)  (0) 2019.10.09
[Spring] [Config] WebMvcContextConfiguration  (0) 2019.10.08
[Spring] Spring MVC / Layered Architecture  (0) 2019.10.06
[Spring] [Config] DBConfig  (0) 2019.10.06
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함