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
42
43
44
45
자바코드를 이용한 설정
 
xml 의 경우 필요했던 
<context:annotation-config />
태그가 자바 설정 파일에서는 필요 없다
당연한 얘긴가..?
 
 
@Configuration
public class BeanClass {
 
    @Bean
    public Student student() {
        return new Student;
    }
 
    @Bean
    public StudentInfo studentInfo() {
        return new StudentInfo(student());
    }
 
}
 
 
ApplicationContext ctx = 
    new AnnotationConfigApplicationContext(BeanClass.class);
 
StudentInfo studentInfo = 
    ctx.getBean("studentInfo", StudentInfo.class);
 
 
 
자바 설정을 사용한 자동 주입 방식
 
자바 설정을 이용한 @Atutowired 애노테이션의 경우
생성자에 적용하더라도 자동주입이 적용되지 않는다
 
 
@Bean 이 적용된 메소드 파라미터에도 자동주입이 적용된다 
 
@Bean    
public StudentInfo studentInfo(Student student){
    StudentInfo studentInfo = new StudentInfo();
    info.setStudent(student);
}
cs

'프로그래밍 > Spring' 카테고리의 다른 글

설정파일이 두개로 나뉘어진 경우 - xml  (0) 2019.06.29
의존 자동 주입 설정  (0) 2019.06.29

설정

트랙백

댓글