티스토리 뷰

Framework/Spring

[Spring & ibatis] 수업 1일

gray.yoon 2013. 4. 22. 20:37

강사 : 서정우 강사님 (cafe : cafe.naver.com/kostaspring)

 

spring-lib.zip

night-spring.pdf

 

http://www.eclipse.org // eclipse 설치

 

수업 진행 방향

java SE -> JDBC -> servlet/jsp(model2) -> Ajax,JQuery -> Framework   (EE계열) + (*)tiles

-----------------

java SE -> JDBC -> Android, Hybrid app(통계), Jquery Mobile (ME계열)

 

Spring DI(IoC)-제어의 역행(2일) -> AOP(2일) -> ibatis(1일) -> spring MVC(5일) + Ajax, Tiles

처음에 XML 후에 anotation으로 리뉴얼할 예정

 

주요 개념

-의존성 주입(Dependency Injection)

-관점 지향 프로그래밍(Aspect-Oriented Programming)

 

**spring lesson ** 목차

DI -> AOP -> DI AOP Annotation -> ibatis framework -> spring mvc -> ajax -> tiles

 

개발 방법

DL (Dependency Lookup) - ex)Connection pool

DI (Dependency injection) -객체 주입 방식 (new를 해서 생성하지 않음) - 결합도 낮아짐(유지보수 편리)

 

 

View 

 Controller  Model

html
javascript -> jquery
jsp(jstl)

 Servlet

비즈니스 - Service
영속성 - DAO(ibatis, hid...)

view + controller = struts 1,2

controller + Model = spring

 

Spring 환경설정

 

1. spring lib download

http://www.springsource.org/download/community
3.0.2.RELEASE

spring-framework-3.0.2.RELEASE-dependencies.zip (sha1) 155.7 MB
spring-framework-3.0.2.RELEASE-with-docs.zip (sha1) 45.0 MB
spring-framework-3.0.2.RELEASE.zip (sha1) 21.4 MB

세가지를 모두 다운로드 받는다.

---------------------------------------------------------

2. Eclipse Spring plug-in 설치 : 이클립스상의 Spring 개발 도구 제공

eclipse menu 의 help-install new software? update site :
http://dist.springframework.org/release/IDE
core 체크 후 설치


-----------------------------------------------------------------
********** tomcat WAS 설정 및 Eclipse 연동 설정*****************
1.http://tomcat.apache.org/
톰켓 6.0 zip 파일 다운 받는다.
2.C:\tomcat\conf 의 web.xml 과 server.xml 의 설정을 변경
server.xml port 와 한글처리를 다음과 같이 변경한다.
69 Line - port="8080" 을 "8888" 로 변경 (port 변경-오라클서버도 8080 이용)
- URIEncoding="utf-8" 추가 (한글처리)
<Connector port="8888" URIEncoding="utf-8" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
3. C:\tomcat\conf\context.xml 에서 자동 리로딩 설정 - 코드 변경시 서버 자동 리로딩!!
19 Line
<Context reloadable="true">으로 변경

4.
Eclipse 에서 web 개발 설정하기

1) 기본 에디터 설정
File - Switch workspace - other 에서 C:\eclipse\workspace\web
로 workspace를 다시 생성한다.
다시 실행되면 font 설정 및 jdk 설정을 한다.
window - preference - General - Appearance - Colors and font - basic
줄번호
window - preference - Editors - Text Editors 의 show line number
jdk 확인
window - preference - java - installed JREs - JDK1.x 확인
window - preference - java - installed JREs - Execution Environment - jdk1.x 확인
2) 한글 인코딩 변경 (MS949에서 utf-8로 변경 설정)
windows-preferences-general-workspace 에서 Text file encoding 에서 other 선택후 utf-8 로 setting
windows-preferences-general-editors-Text Editors-Spelling
windows-preferences-general-content types에서 css,html,java properties file,java source file,jsp를 utf-8로 설정
windows-preferences-web-jsp,css,html 을 utf-8로 설정 

 

DI 설명

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 test;
 
import model.Car;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 
public class TestDI {
 
    /**
     * Spring DI : 계층간의 결합도를 낮추기 위해 설계시 적용
     *                     -> 유지보수성 및 테스트 용이성
     * how? spring Container 에서 객체 생성하여
     *             사용하는 클라이언트 프로그램측은 추상화된 인터페이스 타입으로
     *             주입받아 사용
     *            -> 구체적인 객체의 변화가 있더라도 인터페이스의 변화가 없으면
     *                코드 수정이 필요없다. 
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //Car car = new SportsCar();
//        Car car = new Truck();
//        car.go();
        // DI로 변경
        //spring config xml 을 로딩
        ApplicationContext context = new ClassPathXmlApplicationContext("di.xml");
        //spring container가 객체를 생성하여 반환한다.
        Car car = (Car)context.getBean("car");
        car.go();
    }
 
}
 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package model;
 
public interface Car {
    public void go();
}
 
package model;
 
public class SportsCar implements Car{
    public void go(){
        System.out.println("SportsCar go");
    }
}
 
package model;
 
public class Truck implements Car{
    public void go(){
        System.out.println("truck go");
    }
}
 

 

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="car" class="model.Truck"/>
        
</beans>
 

OO (Object - Oriented)

why? 보안

Encapsalation

"public interface, private implementation"    인터페이스는 보이고, 완성(실행)은 숨긴다. 

 

Object
Component Object가 모인것
FrameWork : 반 완전한 app (Components + pattern)

 

 

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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <!-- 생성자를 통한 주입 연습 -->
    <bean id = "car" class="model.Car">
        <constructor-arg>
            <value>k7</value>
        </constructor-arg>
    </bean>
    <bean id = "friend" class="model.Friend">
        <constructor-arg>
            <value>Yoon</value>
        </constructor-arg>
        <constructor-arg>
            <value>27</value>
        </constructor-arg>
    </bean>
    <bean id = "owner" class="model.Owner">
        <constructor-arg>
            <value>Yoon</value>
        </constructor-arg>
        <constructor-arg>
            <ref bean="car"/>
        </constructor-arg>
    </bean>
</beans>

 

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package model;
 
public class Car {
    private String model;
 
    public Car(String model) {
        super();
        this.model = model;
    }
 
    public String getModel() {
        return model;
    }
 
    public void setModel(String model) {
        this.model = model;
    }
 
    @Override
    public String toString() {
        return "Car [model=" + model + "]";
    }
}
 
package model;
 
public class Friend {
    private String name;
    private int age;
    
    public Friend(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
 
    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;
    }
 
    @Override
    public String toString() {
        return "Friend [name=" + name + ", age=" + age + "]";
    }
    
}
 
package model;
 
public class Owner {
    private String name;
    private Car car;
    
    public Owner(String name, Car car) {
        super();
        this.name = name;
        this.car = car;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Car getCar() {
        return car;
    }
 
    public void setCar(Car car) {
        this.car = car;
    }
 
    @Override
    public String toString() {
        return "Owner [name=" + name + ", car=" + car + "]";
    }
    
    
}
 

 

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
package test;
 
import model.Car;
import model.Friend;
import model.Owner;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class TestDI {
 
    /**
     * 생성자를 통한 injection 연습 예제
     */
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "di.xml");
        Car car = (Car)context.getBean("car");
        System.out.println(car.toString());
        System.out.println("===================================");
        Friend friend = (Friend)context.getBean("friend");
        System.out.println(friend.toString());
        System.out.println("===================================");
        Owner owner = (Owner)context.getBean("owner");
        System.out.println(owner.toString());
        
    }
 
}
 

Car [model=k7]
===================================
Friend [name=Yoon, age=27]
===================================
Owner [name=Yoon, car=Car [model=k7]]

3) WAS(tomcat) 설정
java EE perspective 로 이동 - 아래 server tab 선택 - 오른쪽 마우스 클릭
new - server 선택 - Apache - tomcat 6.0 선택
tomcat installation directory - C:\tomcat
JRE - jdk1.* - 완료 후
Server 탭에서 생성된 tomcat을 더블클릭
Server Locations 에서
두번째 tomcat installation 클릭
deploy path 를 C:\tomcat\webapps 로설정 후 저장
Server 탭에서 서버를 클릭후 오른쪽 마우스를 이용해 server start
메뉴 bar 에서 지구그림을 클릭(웹화면) 실행

********** Oracle 및 Eclipse 연동 설정*****************
administrator : sys or system --> 패스워드 : oracle

*오라클 설치후 제어판-관리도구-서비스- Oracle 관련 수동으로 설정 ,
시작 - 오라클 - 데이터베이스로 시작으로 오라클을 실행
*오라클 데이터베이스에서 자신의 계정 만들기
**계정생성
시작- Run SQL Commend Line실행
connect
ID는 system, 비번은 위에 설치할때 입력한 비번
create user [ID입력] identified by [패스워드]
grant connect, resource to [ID입력]
-- connect 테이블 생성 및 조회 권한 , resource -- pl sql 사용가능
drop user sadqueen; //sadqueen을 삭제했습니다.

-- 실제 예

SQL*Plus: Release 11.2.0.2.0 Production on 목 8월 9 20:36:35 2012

Copyright (c) 1982, 2010, Oracle. All rights reserved.

SQL> connect;
Enter user-name: system
Enter password:
Connected.

SQL> create user scott identified by tiger;

User created.

SQL> grant connect,resource to scott;

Grant succeeded.



*오라클 jdbc 드라이버를 java 실행 환경에 설정하기
C:\oraclexe\app\oracle\product\1*\server\jdbc\lib\ojdbc14.jar 를 copy,
C:\Program Files\Java\jdk1.*\jre\lib\ext\ 밑에 paste
*오라클을 이클립스에서 사용하기 위한 연동 설정하기
oracle 과 eclipse 연동을 위한 tip
java EE perspective 로 이동 - Data Source explorer 하단 탭을 이동
오른쪽 마우스 클릭 - new - new Connection Profiles - Oracle 선택 - 이름 oracle로
입력 - Driver 선택 창 - 오른쪽 상단부 New Driver Definition을 클릭 -
Name/Type 탭에서 Oracle Thin Driver 10 을 선택 후 - Jar list 탭에서
Add Jar/zip 선택후 - C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext\ojdbc14.jar를
선택 - Properties 탭의 Properties 의 connection Url 을
jdbc:oracle:thin:@127.0.0.1:1521:xe 로 입력 , Database Name을 xe로 입력
User ID 를 scott, Password를 tiger로 입력한다.
설정 마치고 Test Connection으로 테스트 후 성공하면 연결이 OK!!
.sql 파일로 이동하여 type:Oracle_10 , Name:oracle , Database : xe 를 설정하고
query 실행해본다. --> query 실행시 영역 선택하고
Execute Selected Text로 주로 사용
-- 테이블 생성하기
create table member(
id varchar2(50) primary key, -- primary key : 유일키 unique+not null 속성
password varchar2(50) not null, -- not null 제약 조건 : null 값은 허용 안됨.
name varchar2(50) not null,
address varchar2(50)
)


insert into member values('java','1234','보아','종로');

insert into member values('spring','4321','한효주','강남');

select * from member;

 

DI 개발시 환경 설정
-> spring library 를 class path 에 추가한다.(spring-lib 에 위치)
-> spring container는 common.logging library 를 필요로 하므로
1) 의 dependencies 안의 관련 library 두가지를 추가로 패스 설정한다.
com.springsource.org.apache.commons.logging-1.1.1.jar
  com.springsource.org.apache.log4j-1.2.15.jar

 

 

1일차
spring di 환경설정 -> log4j
spring library, eclipes에서 spring plug in 설치
DI(의존관계 주입) 개념 : IOC(제어의 역행)
결합도를 낮춘다 -> spring container에서 객체를 주입

2일차
OOP Key concept
Encapsulation, Inheritance, Polymorphism
DI -> 생성자 및 setter method를 이용한 주입
Log4j
AOP

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
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
글 보관함