-
2022-01-28 스프링 프레임워크카테고리 없음 2022. 1. 28. 15:55
스프링 프레임 워크 설치
Maven – Welcome to Apache Maven
Welcome to Apache Maven Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information. If you
maven.apache.org
2. Download -> 표시된 파일 다운로드
3. 압축 풀기
4. 환경변수 설정
CMD에서 mvn 을 입력했을 때 아래 같은 결과가 나오면 환경변수가 정상적으로 설정됨
-> JAVA_HOME도 있어야 정상 작동함
이클립스 없이 프로젝트 생성하기
위와 같은 폴더들(spring ~ java/resources)을 새로 만든다
이 위치에 pom.xml 파일 추가
pom.xml 파일 내용은 필요에 따라 추가, 변경될 수 있음
현재 내용
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Spring</groupId> <artifactId>Sp01</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.0.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf-8</encoding> </configuration> </plugin> </plugins> </build> </project>
CMD에 아래와 같이 입력하면 (spring 폴더의 Sp01(폴더명이 이게 아니더라도 src폴더와 pom.xml이 있는 폴더)로 이동해서 mvn comile 입력)
쭉쭉 뭔가 다운로드되고
BUILD SUCCESS가 나오면 프로젝트 생성 성공
이클립스에 스프링 프로젝트 만들기
file - new - new MavenProject
이렇게 파일을 생성하고 나면
자바버전이 1.5이며 Maven Dependencies 폴더도 없다
-> pom.xml에
이 부분에
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.0.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf-8</encoding> </configuration> </plugin> </plugins> </build>
얘네들을 붙어고 저장
업데이트 해주면
변경 완료
이클립스에 스프링 프로젝트 import하기
워크 스페이스 변경
file - switch workspace - others - 경로선택
확인을 누르면 이클립스가 다시 시작되며 설정이 모두 리셋된다 .. 필요한 설정을 해주고
file - import
-> 캡쳐를 다시 해서 그런데 해당 폴더(src와 pom.xml이 들어있는) 경로를 지정하면 pom.xml 왼쪽 체크박스에 체크가 되어있고 finish를 누를 수 있다...
finish누르면 바로 import 됨!
스프링프로젝트에서 자바프로그래밍 사용해 보기
1) interface
public interface Calculator { public int cal (int firstNum, int secondNum); }
2) 덧셈, 뺄셈, 곱셈 클래스
public class Sum implements Calculator{ @Override public int cal(int firstNum, int secondNum) { int result = firstNum + secondNum; return result; } }
public class Sub implements Calculator { @Override public int cal(int firstNum, int secondNum) { int result = firstNum - secondNum; return result; } }
public class Multi implements Calculator{ public int cal(int firstNum, int secondNum) { int result = firstNum * secondNum; return result; } }
3) 메인 클래스
public class MainClass { public static void main(String[] args) { System.out.println("스프링 프레임워크 응용 소프트웨어 프로젝트 "); Calculator c = new Sum(); int result = c.cal(45, 78); System.out.println("45+78 => " + result); c = new Sub(); result = c.cal(45, 78); System.out.println("45-78 => " + result); c = new Multi(); result = c.cal(45, 78); System.out.println("45*78 => " + result); } }
4) 결과
스프링 프레임 워크의 특징
- 스프링 프레임워크에서는 클래스의 객체 생성 및 운영이 일반적인 자바 클래스와 다르다.
WalkClass wc = new WalkClass(); wc.move();
자바에서 위와 같이 객체를 생성하고 생성된 객체의 메서드를 사용한다면
스프링 프레임 워크는 필요할 때 new 인스턴스를 통해 객체를 생성하는 것이 아니라 프로그램 시작 시에 미리 생성, 보관하고 있다가 필요할 때 꺼내 쓰는 방법을 사용한다.
= 이때 만들어진 인스턴스의 보관 장소를 스프링 컨테이너 라고 부른다
--스프링 컨테이너의 기본 위치는 src- main - resources 폴더
해당 위치에 스프링 컨테이너 역할을 할 xml 파일을 하나 생성 해줌
파일 안에 작성해야할 내용
<?xml version="1.0" encoding="UTF-8"?> <!-- 현재 폴더가 스프링 컨테이너로 사용됨을 알리는 태그 <beans> --> <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>태그에 등록 --> <bean class="com.sp03.WalkClass" id="cWalk"></bean> </beans>
-> 현재 사용하고자하는 클래스가 com.sp03.WalkClass
...
위에 스프링 컨테이너를 생성했다면 스프링 식으로 객체를 만들기
//스프링 컨테이너에 담겨 있는 객체들을 Bean이라 하며 //bean 을 꺼내 쓰려면 컨테이너 사용 권한을 갖고 있는 객체를 생성하여 사용한다. GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationContext.xml"); WalkClass wc2 = ctx.getBean("cWalk", WalkClass.class); //cWalk : 스프링 컨테이너 내부에 있는 클래스의 id값 //WalkClass.classl 스프링 컨테이너 내부에 있는 해당 클래스의 이름 wc2.move(); ctx.close();
new 인스턴스를 사용하면 할 때마다 새로운 인스턴스가 생성되는 반면 getBean()은 싱글턴 방식처럼 하나의 생성된 객체가 계속 사용된다.
getBean()으로 같은 클래스의 서로 다른 인스턴스 두 개를 사용해야한다면 다른 id 값으로 Bean을 추가 하여 사용한다.
클래스에 생성자가 필요한 경우
- 클래스의 생성자가 다른 클래스를 매개변수로 가지고 있다면 클래스가 다른 클래스에 의존하고 있다고 하며 의존하고 있는 객체를 constructor-arg를 이용하여 채워주는 것을 의존 주입(Dependency Injection), 객체 조립이라고 표현한다.
Spring 데이터베이스에 연동하기
1) 새로운 maven project 생성 후 pom.xml의 <dependencies> 위에 아래 코드 추가
<repositories> <repository> <id>oracle</id> <name>ORACLE JDBC Repository</name> <url>http://maven.jahia.org/maven2</url> </repository> </repositories>
<dependecies> 안에 아래 코드 추가
<dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>12.1.0.2</version> </dependency>
후 저장하면 잠시 우측 하단에 다운로드가 진행되고
ojdbc6.jar가 import된다.
2) 새로 DB 서버 만들기
위 게시글 참고
3) test 용 테이블
create table student( sNum varchar2(20), sId varchar2(20), sPw varchar2(20), sName varchar2(20), sAge number(3), sGender varchar2(20), sMajor varchar2(80) )
테스트 레코드 추가하고 결과 확인~