<DI>
Dependency Injection 의 약자.
Student 라는 클래스와, 그런 Student 의 정보를 출력해주는 StudentInfo 가 있다.
이 StudentInfo 객체를 초기화 해주는 방법은 다음과 같이 Xml 파일을 이용하는 방법과,
1 2 3 4 5 | <bean id="studentinfo" class="com.javalec.ex.StudentInfo"> <construct-arg> <ref bean ="student1"/> </construct-arg> </bean> | cs |
다음과 같이 Java 파일에서 초기화 해주는 방법이 있다.
1 2 3 4 | //앞부분 생략 Student student2 = ctx.getBean("student",Student.class); studentinfo.setStudent(student2); studentinfo.getStudentInfo(); | cs |
이런 경우 StudentInfo 객체는 Student 객체에 의존하고 있는 상태라고 볼 수 있다.
DI 사용에 따른 장점
작은 규모의 홈페이지를 만들 때에는 더 무겁고 불편할 수가 있다.
하지만 규모가 커지고, 추후 유지보수 업무가 발생할 때에는 DI 를 이용한 개발의 장점을 느낄 수 있다.
인터페이스의 장점과 비슷하다.
Java 파일의 수정이 없이 스프링 설정 파일만을 수정해 부품을 생성/조립 가능하다.
1 2 3 4 5 | AbstractApplicationContext ctx = new GenericXmlApplicationContext("class path:applicationCTX.xml"); Pencil pencil = ctx.getBean("pencil", Pencil.class); pencil.use(); ctx.close(); | cs |
이러한 자바 파일 중 연필의 굵기를 다르게 하고 싶다고 가정해본다.
그럴 경우 xml 파일만 수정해주면 자바 파일의 내용을 바꾸지 않고도 간편하게 수정 가능하다
1 | <bean id="pencil" class="com.javalec.ex.Pencil4B"/> | cs |
만약 16B 로 바꾸고 싶다면?
1 | <bean id="pencil" class="com.javalec.ex.Pencil16B"/> | cs |
DI 의 설정방법은 xml 로 설정해주는 방식
applicationCTX.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <bean id="student1" class="com.javalec.ex.Student"> <!--constructor setting--> <constructor-arg value="gildong"/> <constructor-arg value="10"/> <!--constructor setting (object) --> <constructor-arg> <list> <value>swim</value> <value>cooking</value> </list> </constructor-arg> <!--setter()--> <property name="height"> <value>187</value> </property> <property name="weight" value="84"/> </bean> | cs |
과,
ApplicationConfig.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @Configuration //이 클래스는 스프링 설정에 사용되는 클래스입니다. 라는 의미 public class ApplicationConfig { } @Bean // 객체 생성 public Student student1(){ ArrayList<String> hobbys = new ArrayList<String>(); hobbys.add("수영"); hobbys.add("요리"); Student student = new Student("홍길동", 20, hobbys); //생성자에 설정 student.setHeight(180); //프로퍼티에 설정 student.setWeight(80); return student; } | cs |
다음처럼 자바파일로 DI 를 설정해줄 수도 있다. 다만 자바 파일로 된 Config 파일은 Main Class 에서 context 초기화를 할 때
1 | AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfig.class); | cs |
와 같이 해주어야 한다. 그 외에는 xml 파일과 동일하게 사용하면 된다.
두 개 다 쓰고 싶은 경우
1. xml 파일에 java 파일 포함시키기
java 파일에 위와같이 Bean 을 생성시킨 후, xml 파일에 다음과 같은 구문을 추가시킨다.
applicationCTX.xml
1 2 | <context:annotation-config /> <bean class="com.javalec.ex.ApplicationConfig"/> //Config 파일로 쓰이는 자바 파일의 bean 을 하나 만들음 | cs |
그리고, mainclass 에서 bean 을 가져올 때는, xml 형식으로 context를 초기화 시켜주면 된다.
1 | AbstractApplicationContext ctx = new ~ ("applicationCTX.xml"); | cs |
2. java 파일에 xml 파일 포함시키기
java config 파일에 다음과 같은 Annotation 을 포함한다.
1 | @ImportResource("classpath:applicationCTX.xml"); | cs |
그리고 mainclass 에서는 java 형식으로(앞서 말했던 AnnotationConfigApplicationContext 객체로) context 를 초기화 시켜주면 된다.
'JSP > 정리' 카테고리의 다른 글
[jsp/spring] 외부파일 (0) | 2017.02.03 |
---|---|
[jsp] Forwarding(포워딩) (0) | 2017.01.20 |
[jsp/개발방법론] FrontController 패턴 (0) | 2017.01.20 |
[jsp] JSTL (0) | 2017.01.20 |
[jsp] EL(Expression Language) (0) | 2017.01.19 |
댓글