SPRING - 자바를 이용한 DI 설정

지금 까지는 xml파일로 DI를 설정했습니다

이번엔 JAVA로 DI를 설정 해보겠습니다.

먼어 사용되어야 하는 어노테이션이 있습니다

@Configuration 과 @Bean 어노테이션입니다

@Configuration는 클래스 앞에 붙이며

"이 클래스는 스프링 설정에 사용되는 클래습니다"

라고 명시해주는 어노테이션입니다

@Bean은 Bean 객체 생성에 사용되는 어노테이션입니다


다음 3개의 클래스를 생성해줍니다

먼저 Student 빈을 생성하겠습니다


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
package com.spring.ex;
 
import java.util.ArrayList;
 
public class Student {
    
    private String name;
    private int age;
    private ArrayList<String> hobbys;
 
        public Student(String name) {
            this.name = name;
        }
 
        public String getName() {
            return name;
        }
 
        public void setName(String name) {
            this.name = name;
        }
 
        public int getAge() {
            return age;
        }
 
        public void setAge(int age) {
            this.age = age;
        }
 
        public ArrayList<String> getHobbys() {
            return hobbys;
        }
 
        public void setHobbys(ArrayList<String> hobbys) {
            this.hobbys = hobbys;
        }
}
 
cs


다음 ApplicationConfig이름의 클래스 만들어줍니다


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
package com.spring.ex;
 
import java.util.ArrayList;
 
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class ApplicationConfig {
    @Bean
    public Student student1(){
        ArrayList<String> hobbys = new ArrayList<String>();
        hobbys.add("수영");
        hobbys.add("요리");
        
        Student student = new Student("홍길동1");
        student.setAge(10);
        student.setHobbys(hobbys);
        return student;        
    }
    
    @Bean
    public Student student2(){
        ArrayList<String> hobbys = new ArrayList<String>();
        hobbys.add("독서");
        hobbys.add("음악감상");
        
        Student student = new Student("홍길동2");
        student.setAge(5);
        student.setHobbys(hobbys);            
        return student;        
    }
}
 
cs


12line 에서는 student1이라는 이름의

빈객체를 만듭니다 다음 ArrayList를 hobbys라는

이름의 객체를 만들어서 add를 사용하여 취미

데이터를 넣어줍니다. 다음 Student에 매개변수값으로

홍길동1 값을 넣어 생성자 만들어줍니다

다음 set을 사용해서 나이랑 취미 데이터를 넣습니다.

student2도 student1과 같습니다.


마지막으로 메인클래스 만들어줍니다


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
package com.spring.ex;
 
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
public class MainClass {
    
    public static void main(String[] args){
        
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        
        Student student1 = ctx.getBean("student1", Student.class);
        
        System.out.println(student1.getName());
        System.out.println(student1.getAge());
        System.out.println(student1.getHobbys());
        
        
        Student student2 = ctx.getBean("student2", Student.class);
        System.out.println(student2.getName());
        System.out.println(student2.getAge());
        System.out.println(student2.getHobbys());
        
        ctx.close();
    }    
 
}
 
cs


9line 에서는 어떤 클래스를 쓸건지 명시해줍니다

다음 getBean을 사용하여 student1을 뽑아옵니다.

다음 sysout으로 이름 나이 취미를 출력 합니다



실행 시켜보면 위 와같이 콘솔창에

정상적으로 값이 찍히는걸 확인 할 수 있습니다.

'SPRING' 카테고리의 다른 글

SPRING - 스프링MVC  (0) 2018.02.06
SPRING - AOP란?  (0) 2018.02.06
SPRING - DI의 장점  (0) 2018.02.05
SPRING - DI 활용  (0) 2018.02.05
SPRING - DI (Dependency Injection)  (0) 2018.02.05

댓글

Designed by JB FACTORY