반응형
스프링 시작하기
위에 Spring Framework가 핵심이다.
Spring Framework 설치법
pom.xml에서 java 버전을 1.8로, Spring을 5.0.8로 바꿔준다
<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>org.springframework.samples</groupId>
<artifactId>SpringEx02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- Generic properties -->
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring -->
<spring-framework.version>5.0.8.RELEASE</spring-framework.version>
<!-- Hibernate / JPA -->
<hibernate.version>4.2.1.Final</hibernate.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!-- Test -->
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
SpringEx01.spring1.HelloSpring
package spring1;
public class HelloSpring {
public void sayHello(String name) {
System.out.println(name + "님 안녕하세요");
}
}
SpringEx01.spring1.HelloSpringMain
package spring1;
public class HelloSpringMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 객체의 생성 코드로 작성
HelloSpring helloSpring = new HelloSpring();
helloSpring.sayHello("홍길동");
}
}
SpringEx01.spring2.context
<?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">
<!-- HelloSpring helloSpring1 = new HelloSpring() -->
<bean name="helloSpring1" class="spring2.HelloSpring" />
</beans>
SpringEx01.Spring2.HelloSpring
package spring2;
public class HelloSpring {
public HelloSpring() {
System.out.println("생성자 호출");
}
public void sayHello(String name) {
// TODO Auto-generated method stub
System.out.println(name + "님 안녕하세요");
}
}
SpringEx01.Spring2.HelloSpringMain
package spring2;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class HelloSpringMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
Resource resource
= new ClassPathResource("spring2/context.xml");
BeanFactory beanFactory
= new XmlBeanFactory(resource);
HelloSpring helloSpring
= (HelloSpring)beanFactory.getBean("helloSpring1");
helloSpring.sayHello("홍길동");
}
}
반응형
'Web & Mobile > Spring' 카테고리의 다른 글
Lecture 76 - Spring(6) Model2 게시판을 Spring으로, 전자정부프레임워크 설정법, 전자정부프레임워크 모바일 (0) | 2019.07.05 |
---|---|
Lecture 75 - Spring(5) Spring MVC 게시판(2), Maven (0) | 2019.07.04 |
Lecture 74 - Spring(4) AOP, Spring MVC 게시판 (0) | 2019.07.02 |
Lecture 73 - Spring(3) 스프링 게시판 (0) | 2019.07.01 |
Lecture 72 - Spring(2) 스프링 프레임워크를 이용한 게시판 (0) | 2019.06.28 |
댓글