1. annotation 사용 안 함
-폴더 구조
: 객체생성에 관한 내용은 app-context.xml파일에 써주고 실행은 MessageMain.java에서 실행한다.
-app-context.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?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 https://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-3.0.xsd"> <description>Example configuration to get you started.</description> <!-- 어떤 패키지에만 환경셋팅 적용할지 --> <context:component-scan base-package="*" /> <bean id="message" class="com.exe.springdi3.MessageKr"/> <!-- 중간지점 클래스를 Main에서 생성하는 객체 --> <!-- IoC(제어의 역전) --> <bean id="serviceConsumer" class="com.exe.springdi4.ServiceConsumer"> <constructor-arg ref="messageService"></constructor-arg> <property name="timeService" ref="timeService"/> <property name="jobService" ref="jobService"/> </bean> <bean id="messageService" class="com.exe.springdi4.MyMessageService"/> <bean id="timeService" class="com.exe.springdi4.MyTimerService"/> <bean id="jobService" class="com.exe.springdi4.MyJobService"></bean> </beans> | cs |
: annotation을 사용하지 않을 경우 app-context.xml에 위의 코드들을 써주어야 한다.
-ServiceConsumer.java
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 46 47 48 49 50 51 52 53 | package com.exe.springdi4; import org.springframework.context.support.GenericXmlApplicationContext; //얘가 중간관리자 public class ServiceConsumer { MessageService ms; //오버로딩된 생성자가 있을 경우 기본생성자가 무조건 있어야하므로 //여기에 명시해줌 public ServiceConsumer() {}; //생성자로 초기화 //생성자로 의존성 주입 public ServiceConsumer(MessageService ms) { this.ms=ms; } //메소드로 의존성 주입1 TimeService ts; //여기는 app-context의 property name의 set뒤에 대문자로 바꾼 이름 public void setTimeService(TimeService ts) { this.ts = ts; } //메소드로 의존성 주입2 JobService js; public void setJobService(JobService js) { this.js=js; } public void consumerService() { //GenericXmlApplicationContext context=new GenericXmlApplicationContext("app-context.xml"); //app-context.xml에서 읽어옴 //MessageService ms=(MessageService)context.getBean("messageService"); //생성자 의존성 주입 String message=ms.getMessage(); System.out.println(message); //메소드 의존성 주입1 String time=ts.getTimeString(); System.out.println(time); //메소드 의존성 주입2 js.getJob(); } } | cs |
: app-context.xml과 ServiceConsumer.java 사이의 관계가 중요하다.
-그 외 파일들
: 여기서 JobService.java와 MyJobService.java가 Interface와 Class로 하나의 짝이고
(MyJobService 클래스가 JobService 인터페이스의 메소드를 오버라이딩해서 구현함)
MessageService.java와 MyMessageService.java, TimeService와 MyTimerService도 마찬가지로 짝이다.
다 같은 구조이므로 예시로 JobService.java와 MyJobService.java만 보여주면
-JobService.java
1 2 3 4 5 6 | package com.exe.springdi4; public interface JobService { public void getJob(); } | cs |
-MyJobService.java
1 2 3 4 5 6 7 8 | package com.exe.springdi4; public class MyJobService implements JobService { public void getJob() { System.out.println("나는 프로그래머 입니다."); } } | cs |
-실행화면
**그런데 여기서!!
**autowire="byName"의 기능
-app-context.xml
1 2 3 4 5 6 7 8 9 | <bean id="serviceConsumer" class="com.exe.springdi4.ServiceConsumer" autowire="byName"> <constructor-arg ref="messageService"></constructor-arg> <!-- <property name="timeService" ref="timeService"/> <property name="jobService" ref="jobService"/> --> </bean> <bean id="messageService" class="com.exe.springdi4.MyMessageService"/> <bean id="timeService" class="com.exe.springdi4.MyTimerService"/> <bean id="jobService" class="com.exe.springdi4.MyJobService"></bean> | cs |
autowire="byName"을 써줄경우 생성자는 제외, 메소드로 의존성주입을 한 경우
밑의 bean id로 (set을 빼고 맨앞글자는 소문자)의 규칙에 맞으면 스프링이 알아서 찾아가게 된다.
실행해보면 위의 property부분을 주석처리해도 잘 실행되는 모습을 볼 수 있다.
2. annotation 사용
-app-context.xml
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 | <?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 https://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-3.0.xsd"> <description>Example configuration to get you started.</description> <!-- 어떤 패키지에만 환경셋팅 적용할지 --> <context:component-scan base-package="*" /> <!-- annotation 사용할 거임 --> <!-- <bean id="message" class="com.exe.springdi3.MessageKr"/> 중간지점 클래스를 Main에서 생성하는 객체 IoC(제어의 역전) <bean id="serviceConsumer" class="com.exe.springdi4.ServiceConsumer" autowire="byName"> <constructor-arg ref="messageService"></constructor-arg> <property name="timeService" ref="timeService"/> <property name="jobService" ref="jobService"/> </bean> <bean id="messageService" class="com.exe.springdi4.MyMessageService"/> <bean id="timeService" class="com.exe.springdi4.MyTimerService"/> <bean id="jobService" class="com.exe.springdi4.MyJobService"></bean> --> </beans> | cs |
: annotation을 안써줬을경우 써야하는 위의 코드를 annotation을 사용함으로써 모두 주석처리해주었다.
- 각 annotation의 역할
: @Component : 자동으로 빈 등록
@Component annotation을 사용해 bean을 각 java파일에서 생성하도록 해주었다.
: @Component로 객체 생성을 해주고 @Autowired(및 @Qualifier)를 이용해 생성자및 메소드의 파라미터를 넘겨준다.
-실행화면
** Annotation을 사용하는 게 훨씬 간단하다.