Spring
02. Spring- 스프링코어기본 - 세터주입(Maven Project)
코골면서 딩가딩가
2024. 4. 17. 10:29
1. 메이븐 프로젝트 만들기
가. Other -> New -> Maven Project
나. Build Path -> Configure Build Path
다. pom.xml 코드 수정
<?xml version="1.0" encoding="UTF-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>com.khinfo.spring</groupId>
<artifactId>springcore</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcore</name>
<url>http://maven.apache.org</url>
<properties>
<springframework.version>4.3.6.RELEASE</springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
라. test파일 Delete -> Update Project 후 Maven Dependencies 새로운 .jar 확인
2. 자바빈 생성
2.1 DI에 대한 3단계
가. POJO(Plain Old Java Object) 클래스 만들기
Employee.java 코드
package com.khinfo.spring.springcore;
public class Employee {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + "]";
}
}
나. 설정파일 만들기
config.xml 코드
<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" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean name="emp" class="com.khinfo.spring.springcore.Employee">
<property name="id">
<value>25</value>
</property>
<property name="name">
<value>JMJ</value>
</property>
</bean>
</beans>
다. 테스트 클래스 만들기
Test.java 코드
package com.khinfo.spring.springcore;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext(
"classpath:config.xml");
// 빈 객체 얻어옴
Employee employee = (Employee)ctx.getBean("emp");
System.out.println(employee);
ctx.close();
}
}
+ 디렉토리 경로구성
라. 결과