[Spring 3.0] Spring 3.0 설치 및 환경Setting
1. Spring 3.0 버전 이클립스 다운
다운로드 링크 : https://spring.io/tools
메인 화면 맨 밑의 하단 TOOLS > Spring Tools 3 for Eclipse>아래 이미지에 표시해놓은 버튼눌러서 다운
(Spring 3.9.8버전 다운로드)
- 다운 받아서 원하는 경로에 폴더 설정해준다 (웹서버인 톰캣과, 프로젝트 생성폴더인 work폴더 셋팅)
- server.xml 에 GET방식으로 받을때 한글인코딩해주기위해서 저 부분 써준다.
(경로: D:\sts-bundle\apache-tomcat-7.0.92\conf\server.xml)
-window>preperences에서 인코딩방식 바꿔준다.
-기존에 깔려있던 pivotal-tc-server 서버는 안쓸거기 때문에 삭제해준다.
-window>preperences>Server>Runtime Environment 가서도 삭제
**앞으로의 진도 일정
1. Pojo 문법
2. jdbc->mybatis->Spring JDBC
3. Web
4. jdbc->mybatis->SPring JD
2. 프로젝트 생성 (New Legacy Project)
-next
**프로젝트에 오류뜰때 없애는 방법 2가지
1. repository 폴더 내용 삭제
이 경로의 repository에 있는 폴더를 다 지우고
다시 이클립스 재부팅하면 이클립스에서 다시 라이브러리를 받는다.
또 다른 방법은
2. Maven>update project
눌러주면 이클립스에서 필요한 Maven 라이브러리 다운 받는다.
3. 간단한 날짜 예제
-TimeService.java
1 2 3 4 5 6 7 | package com.exe.springdi4; public interface TimeService { public String getTimeString(); } | cs |
-MyTimerService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.exe.springdi4; import java.text.SimpleDateFormat; import java.util.Date; import javax.management.timer.TimerMBean; public class MyTimerService implements TimeService { public String getTimeString() { //날짜를 지정해주는 형식을 지정해줌 SimpleDateFormat sdf= (SimpleDateFormat)SimpleDateFormat.getDateTimeInstance( SimpleDateFormat.LONG,SimpleDateFormat.LONG); String now=sdf.format(new Date()); return now; } } | cs |
-SerciveConsumer.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 | package com.exe.springdi4; import org.springframework.context.support.GenericXmlApplicationContext; //얘가 중간관리자 public class ServiceConsumer { MessageService ms; //오버로딩된 생성자가 있을 경우 기본생성자가 무조건 있어야하므로 //여기에 명시해줌 public ServiceConsumer() {}; //생성자로 초기화 //생성자로 의존성 주입 public ServiceConsumer(MessageService ms) { this.ms=ms; } //메소드로 의존성 주입 TimeService ts; public void setTimeService(TimeService ts) { this.ts = ts; } 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); //메소드 의존성 주입 String time=ts.getTimeString(); System.out.println(time); } } | cs |
4. autowire="byName"과 annotation
** autowire="byName"
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"이라고 써주면
property에 따로 써주지않아도 bean의 id와 메소드의 이름을보고 스프링이 알아서 찾아간다.
**@Component
@Component와 같은 역할을 하는 부분
1 2 | <bean id="serviceConsumer" class="com.exe.springdi4.ServiceConsumer" autowire="byName"> | cs |
**@Autowired
@Autowired와 같은 역할을 하는 부분
1 | <constructor-arg ref="messageService"></constructor-arg> | cs |