Spring/Study 2020. 4. 13. 16:05

(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)

 

@Configuration 어노테이션은 JAVA 파일을 스프링 컨테이너로 사용하도록 명시해주는 어노테이션이다. 따라서 @Configuration 어노테이션을 통해 JAVA 파일이 스프링 컨테이너(xml) 역할을 하도록 할 수 있다. JAVA 파일에서는 @Bean 어노테이션을 사용하여 메소드로  빈(Bean) 객체를 생성한다.

 

bean

<?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="studentDao" class="ems.member.dao.StudentDao" ></bean>
 
</beans>
cs

 

@Configuration
public class MemberConfig {
    @Bean
    public StudentDao studentDao() {
        return new StudentDao();
    }
}
cs

 

constructor

<?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="registerService" class="ems.member.service.StudentRegisterService">
        <constructor-arg ref="studentDao" ></constructor-arg>
    </bean>
 
</beans>
cs

 

@Configuration
public class MemberConfig {
    @Bean
    public StudentRegisterService registerService() {
        return new StudentRegisterService(studentDao());
    }
}
cs

 

property

<?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="dataBaseConnectionInfoDev" class="ems.member.DataBaseConnectionInfo">
        <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe" />
        <property name="userId" value="scott" />
        <property name="userPw" value="tiger" />
    </bean>
 
</beans>
cs

 

@Configuration
public class MemberConfig {
    @Bean    
    public DataBaseConnectionInfo dataBaseConnectionInfoDev() {
        DataBaseConnectionInfo infoDev = new DataBaseConnectionInfo();
        infoDev.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:xe");
        infoDev.setUserId("scott");
        infoDev.setUserPw("tiger");
        
        return infoDev;
    }
}
cs

 

list

<?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="informationService" class="ems.member.service.EMSInformationService">
        <property name="developers">
            <list>
                <value>Cheney.</value>
                <value>Eloy.</value>
                <value>Jasper.</value>
                <value>Dillon.</value>
                <value>Kian.</value>
            </list>
        </property>
    </bean>
 
</beans>
cs

 

@Configuration
public class MemberConfig {
    @Bean
    public EMSInformationService informationService() {
        EMSInformationService info = new EMSInformationService();
        
        ArrayList<String> developers = new ArrayList<String>();
        developers.add("Cheney.");
        developers.add("Eloy.");
        developers.add("Jasper.");
        developers.add("Dillon.");
        developers.add("Kian.");
        info.setDevelopers(developers);
        
        return info;
    }
}
cs

 

map

<?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="informationService" class="ems.member.service.EMSInformationService">
        <property name="administrators">
            <map>
                <entry>
                    <key>
                        <value>Cheney</value>
                    </key>
                    <value>cheney@springPjt.org</value>
                </entry>
                <entry>
                    <key>
                        <value>Jasper</value>
                    </key>
                    <value>jasper@springPjt.org</value>
                </entry>
            </map>
        </property>
    </bean>
 
</beans>
cs

 

@Configuration
public class MemberConfig {
    @Bean    
    public EMSInformationService informationService() {
        EMSInformationService info = new EMSInformationService();
        
        Map<StringString> administrators = new HashMap<StringString>();
        administrators.put("Cheney""cheney@springPjt.org");
        administrators.put("Jasper""jasper@springPjt.org");
        info.setAdministrators(administrators);
    
        return info;
    }
}

Colored by Color Scripter

cs

posted by DevMoomin
:
Spring/Study 2020. 4. 13. 15:25

(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)

 

스프링 컨테이너가 생성되면 컨테이터 내부에서 빈(Bean) 객체가 생성되고, 빈(Bean) 객체들은 서로 의존 관계를 가지고 있다. 따라서 스프링 컨테이너와 빈(Bean) 객체의 생명주기는 동일하다.

 

인터페이스(Interface)를 이용하는 방법

BookDao.java

public class BookDao implements InitializingBean, DisposableBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("빈(Bean)객체 생성 단계");
    }
 
    @Override
    public void destroy() throws Exception {
        System.out.println("빈(Bean)객체 소멸 단계");
    }    
}
cs

 

init-method, destroy-method 속성을 이용하는 방법

appCtx.xml

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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 
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context.xsd">
 
    <context:annotation-config />
 
    <bean id="bookRegisterService" class="com.brms.book.service.BookRegisterService" 
    init-method="initMethod" destroy-method="destroyMethod"/>
</beans>
cs

BookRegisterService.java

public class BookRegisterService {
    public void initMethod() {
        System.out.println("BookRegisterService 빈(Bean)객체 생성 단계");
    }
    
    public void destroyMethod() {
        System.out.println("BookRegisterService 빈(Bean)객체 소멸 단계");
    }
}

Colored by Color Scripter

cs

posted by DevMoomin
:
Spring/Study 2020. 4. 12. 20:02

(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)

 

@Qualifier

동일한 객체가 2개 이상인 경우, 스프링 컨테이너는 자동 주입 대상 객체를 판단하지 못해서 Exception을 발생시키는데, @Qualifier 어노테이션을 통해 주입할 객체를 명시함으로써 해결할 수 있다. (변수 이름과 Bean의 ID가 같은 경우 @Qualifier 어노테이션이 없어도 에러가 발생하지 않지만 추천하는 방식은 아니다.)

appCtx.xml

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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 
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context.xsd">
 
    <context:annotation-config />
 
    <bean id="wordDao" class="com.word.dao.WordDao" >
        <qualifier value="usedDao"/>
    </bean>
    <bean id="wordDao2" class="com.word.dao.WordDao" />
    <bean id="wordDao3" class="com.word.dao.WordDao" />
</beans>
cs

WordRegisterService.java

@Autowired
@Qualifier("usedDao")
private WordDao wordDao;
cs

 

의존객체 자동 주입 체크

자동 주입할 객체가 없는 경우 Exception이 발생하는데, required=false 속성을 통해 자동 주입할 객체가 없는 경우에는 주입하지 않도록 함으로써 Exception을 방지할 수 있다.

WordRegisterService.java

@Autowired(required=false)
private WordDao wordDao;
cs

 

@Inject

@Autowired처럼 의존객체를 자동으로 주입을 할 수 있지만, required 속성을 지원하지 않는다. @Qualifier 대신 @Named 어노테이션을 사용한다. (@Inject 보다 @Autowired를 더 많이 사용)

appCtx.xml

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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 
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context.xsd">
 
    <context:annotation-config />
    
    <bean id="wordDao1" class="com.word.dao.WordDao" />
    <bean id="wordDao2" class="com.word.dao.WordDao" />
</beans>
cs

WordRegisterService.java

@Inject
@Named(value="wordDao1")
private WordDao wordDao;

cs

posted by DevMoomin
:
Spring/Study 2020. 4. 10. 18:28

(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)

 

의존객체 자동 주입이란 스프링 설정파일에서 의존객체를 주입할 때 <constructor-arg> 또는 <property> 태그로 대상객체를 명시하지 않아도 스프링 컨테이너가 자동으로 필요한 의존대상 객체를 찾아서 주입해 주는 기능을 말한다.

 

@Autowired

주입하려고 하는 객체의 타입이 일치하는 객체를 자동으로 주입 (생성자, 변수, 메소드에 사용가능)

변수나 메소드에서 사용할 때는 객체가 생성되어야 주입이 가능하므로 Default 생성자 필요

WordRegisterService.java

@Autowired
public WordRegisterServiceUseAutowired(WordDao wordDao) {
    this.wordDao = wordDao;
}
cs

appCtx.xml

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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 
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context.xsd">
 
    <context:annotation-config />
 
    <bean id="wordDao" class="com.word.dao.WordDao" />    
</beans>
cs

 

@Resource

주입하려고 하는 객체의 이름이 일치하는 객체를 자동으로 주입 (변수, 메소드에 사용가능)

변수나 메소드에서 사용할 때는 객체가 생성되어야 주입이 가능하므로 Default 생성자 필요

WordRegisterService.java

@Resource
private WordDao wordDao;
    
public WordRegisterServiceUseResource() {
    
}
    
//@Resource
public WordRegisterServiceUseResource(WordDao wordDao) {
    this.wordDao = wordDao;
}
cs

appCtx.xml

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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 
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context.xsd">
 
    <context:annotation-config />
 
    <bean id="wordDao" class="com.word.dao.WordDao" />
        
</beans>

Colored by Color Scripter

cs

 

posted by DevMoomin
:
Spring/Study 2020. 4. 9. 16:27

(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)

 

싱글톤(Singleton)

스프링 컨테이너에서 생성된 빈(Bean) 객체의 경우 동일한 타입에 대해서는 기본적으로 한 개만 생성이 되며, getBean() 메소드로 호출될 때 동일한 객체가 반환된다. scope 속성을 명시하지 않으면 기본적으로 싱글톤으로 설정된다.

DependencyBean.java

package scope.ex;
 
public class DependencyBean {
    public DependencyBean() {
        System.out.println("DependencyBean : constructor");
    }
}
cs

applicationContext.xml

<?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="dependencyBean" class="scope.ex.DependencyBean" />
    
</beans>
cs

MainClass.java

package scope.ex;
 
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class MainClass {
    public static void main(String[] args) {
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationContext.xml");
        
        DependencyBean dependencyBean1 = ctx.getBean("dependencyBean", DependencyBean.class);
        DependencyBean dependencyBean2 = ctx.getBean("dependencyBean", DependencyBean.class);
        
        if(dependencyBean1.equals(dependencyBean2)) System.out.println("dependencyBean1 == dependencyBean2");
        else System.out.println("dependencyBean1 != dependencyBean2");
        
        ctx.close();
    }
}
cs

실행결과

 

프로토타입(Prototype)

new로 객체를 생성할 때와 마찬가지로 getBean() 메소드로 호출할 때마다 매번 새로운 객체를 만든다. scope속성에서 protype이라고 명시를 해주어야 한다.

DependencyBean.java

package scope.ex;
 
public class DependencyBean {
    public DependencyBean() {
        System.out.println("DependencyBean : constructor");
    }
}
cs

applicationContext.xml

<?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="dependencyBean" class="scope.ex.DependencyBean" scope="prototype" />
    
</beans>
cs

MainClass.java

package scope.ex;
 
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class MainClass {
    public static void main(String[] args) {
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationContext.xml");
        
        DependencyBean dependencyBean1 = ctx.getBean("dependencyBean", DependencyBean.class);
        DependencyBean dependencyBean2 = ctx.getBean("dependencyBean", DependencyBean.class);
        
        if(dependencyBean1.equals(dependencyBean2)) System.out.println("dependencyBean1 == dependencyBean2");
        else System.out.println("dependencyBean1 != dependencyBean2");
        
        ctx.close();
    }
}
cs

실행결과

posted by DevMoomin
:
Spring/Study 2020. 4. 9. 15:26

(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)

 

하나의 XML 파일에 너무 많은 내용이 들어있으면 가독성 등에서 문제가 발생하게 되는데, 기능별로 구분해서 XML 파일을 나누어주어 이러한 문제를 해결할 수 있다.

분리한 스프링 설정 파일은 배열을 이용하여 한번에 가져와서 사용할 수도 있고, 하나의 설정 파일에 여러 설정 파일을 import 해놓고, 해당 설정 파일만을 가져와서 사용할 수도 있다.

 

 

배열로 사용하는 방법

MainClass.java

String[] appCtxs = {"classpath:appCtx1.xml""classpath:appCtx2.xml""classpath:appCtx3.xml"};
 
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(appCtxs);
cs

 

import로 사용하는 방법

applicationContext.xml

<import resource="classpath:appCtx1.xml" />
<import resource="classpath:appCtx2.xml" />
<import resource="classpath:appCtx3.xml" />
cs

MainClass.java

GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationContext.xml");

cs

posted by DevMoomin
:
Spring/Study 2020. 4. 8. 23:01

(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)

 

생성자를 이용한 의존 객체 주입

public StudentRegisterService(StudentDao studentDao) {
    this.studentDao = studentDao;
}
cs

 

<baen id="studentDao" class="ems.member.dao.StudentDao"></bean>
 
<bean id="registerService" class="ems.member.service.StudentRegisterService">
    <constructor-arg ref="studentDao"></constructor-arg>
</bean>
cs

 

setter를 이용한 의존 객체 주입

public void setJdbcUrl(String jdbcUrl) {
    this.jdbcUrl = jdbcUrl;
}
 
public void setUserId(String userId) {
    this.userId = userId;
}
 
public void setUserPw(String userPw) {
    this.userPw = userPw;
}
cs

 

<bean id="dataBaseConnectionInfoDev" class="ems.member.DataBaseConnectionInfo">
    <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe" />
    <property name="userId" value="moomini" />
    <property name="userPw" value="1234" />
</bean>
cs

 

List타입 의존 객체 주입

public void setDevelopers(List<String> developers) {
    this.developers = developsers;
}
cs

 

<property name="developers">
    <list>
        <value>Cheney.</value>
        <value>Eloy.</value>
        <value>Jasper.</value>
        <value>Dillon.</value>
        <value>Kian.</value>
    </list>
</property>
cs

 

Map타입 의존 객체 주입

public void setAdministrators(Map<StringString> administrators) {
    this.administrators = administrators;
}
cs

 

<property name="administrators">

    <map>

        <entry>

            <key>

                <value>Cheney</value>

            </key>

            <value>cheney@springPjt.org</value

        </entry>

        <entry>

            <key>

                <value>Jasper</value>

            </key>

            <value>jasper@springPjt.org</value

        </entry>

    </map>

</property>

Colored by Color Scripter

cs

posted by DevMoomin
:
Spring/Study 2020. 4. 8. 22:35

(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)

 

DI(Dependency Injection)란 의존성 주입이라고도 하며, 객체를 직접 생성하는 것이 아니라 외부에서 객체를 생성한 후 주입하는 방식을 말한다. DI를 이용하면 프로그램을 수정할 때 설정 파일만 바꿔주면 되기 때문에 확장성이 뛰어나고, 유지보수에 용이하다.

 

방법1) 객체 직접 생성

Hello.java

1
2
3
4
5
6
7
package helloSpring;
 
public class Hello {
    public void print() {
        System.out.println("Hello World!");
    }
}
cs

- Line 4 : print() 메소드 선언

- Line 5 : Hello World! 출력

MainClass.java

1
2
3
4
5
6
7
8
package helloSpring;
 
public class MainClass {
    public static void main(String[] args) {
        Hello hello = new Hello();
        hello.print();
    }
}
cs

- Line 5 : new를 이용하여 hello 객체 생성

- Line 6 : print() 메소드 호출

 

방법2) 외부에서 객체를 생성한 후 주입

Hello.java

1
2
3
4
5
6
7
8
package helloSpring;
 
public class Hello {
    public void print() {
        System.out.println("Hello World!");
    }
}
 
cs

- Line 4 : print() 메소드 선언

- Line 5 : Hello World! 출력

applicationContext.java

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="hello" class="helloSpring.Hello" />
 
</beans>
cs

- Line 7 : Hello 클래스를 hello라는 ID의 bean으로 등록

MainClass.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package helloSpring;
 
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class MainClass {
    public static void main(String[] args) {
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationContext.xml");
        
        Hello hello = ctx.getBean("hello", Hello.class);
        hello.print();
        
        ctx.close();
    }
}
 
cs

- Line 7 : 스프링 컨테이너가 생성되고, 내부에 bean 객체들이 생성 (XML로부터 객체 설정 정보를 가져옴)

- Line 9 : hello라는 객체를 생성하고, XML에서 ID가 hello인 bean을 가져와서 넣어줌

- Line 10 : hello 객체의 print() 메소드 호출

- Line 12 : 스프링 컨테이너와 bean 객체들이 소멸

'Spring > Study' 카테고리의 다른 글

스프링(Spring) 설정 파일 분리  (0) 2020.04.09
스프링(Spring) 다양한 의존 객체 주입  (0) 2020.04.08
스프링(Spring) Hello World!  (0) 2020.04.07
스프링(Spring) 프로젝트 생성  (0) 2020.04.07
스프링(Spring) 개요  (0) 2020.04.06
posted by DevMoomin
:
Spring/Study 2020. 4. 7. 22:21

(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)

 

스프링(Spring)에서는 자바처럼 new를 이용하여 객체를 직접 생성하지 않고, 스프링 설정파일(XML)을 이용한다. 

 

스프링(Spring) 프로젝트 생성 : https://devmoomini.tistory.com/46

 

Hello.java

1
2
3
4
5
6
7
8
package helloSpring;
 
public class Hello {
    public void print() {
        System.out.println("Hello World!");
    }
}
 
cs

- Line 4 : print() 메소드 선언

- Line 5 : Hello World! 출력

 

applicationContext.xml

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="hello" class="helloSpring.Hello" />
 
</beans>
cs

- Line 7 : Hello 클래스를 hello라는 ID의 bean으로 등록

 

MainClass.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package helloSpring;
 
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class MainClass {
    public static void main(String[] args) {
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationContext.xml");
        
        Hello hello = ctx.getBean("hello", Hello.class);
        hello.print();
        
        ctx.close();
    }
}
 
cs

- Line 7 : 스프링 컨테이너가 생성되고, 내부에 bean 객체들이 생성 (XML로부터 객체 설정 정보를 가져옴)

- Line 9 : hello라는 객체를 생성하고, XML에서 ID가 hello인 bean을 가져와서 넣어줌

- Line 10 : hello 객체의 print() 메소드 호출

- Line 12 : 스프링 컨테이너와 bean 객체들이 소멸

 

실행결과

posted by DevMoomin
:
Spring/Study 2020. 4. 7. 20:03

(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)

 

스프링(Spring) 프로젝트를 생성하기 위해서는 JDK와 이클립스(Eclipse)가 필요하다. 만약 설치가 되어있지 않다면, 아래의 URL을 참고해서 먼저 설치 후 진행하도록 하자.

 

JDK 설치 : https://devmoomini.tistory.com/39

JAVA 환경변수 설정 : https://devmoomini.tistory.com/40

이클립스(Eclipse) 설치 : https://devmoomini.tistory.com/41

 

1. Eclipse 실행 후 Project Explorer 영역에서 마우스 우클릭 - New - Project 클릭

2. maven을 검색해서 Maven Project 선택 후 Next 클릭

※ Maven : 프로젝트 생성, 테스트 빌드, 배포 등의 작업을 위한 빌드 도구

3. Create a simple project (skip archetype selection) 체크 후 Next 클릭

4. Group IdArtifact Id를 입력하고, Finish 클릭

※ Group Id : 프로젝트의 그룹명(ex. 지하철 노선 개발 프로젝트)

    Artifact Id : Group Id에 속하는 프로젝트 이름(ex. 1호선, 2호선)

5. pom.xml에 아래 그림과 같이 입력 후 저장한다.

※ pom.xml : Maven 설정 파일로, 프로젝트에 import 되는 jar 파일의 정보와 버전 정보를 가지는 파일

    dependencies : 의존성을 정의하는 부분으로, 라이브러리를 불러옴

    build : 빌드할 때 사용할 플러그인 목록을 저장

6. Eclipse 하단부의 Markers을 보면 에러가 발생한 것을 확인할 수 있는데, 프로젝트의 JRE 라이브러리 버전이 메이븐 설정 파일의 버전과 일치하지 않아서 발생하는 에러

7. 프로젝트 폴더 마우스 우클릭 - Maven - Update Project...를 클릭

8. OK를 클릭하여 프로젝트 업데이트

9. 에러 없이 정상적으로 동작하고, pom.xml에서 입력한 라이브러리가 Maven Dependencies에 자동으로 다운받아진 것 확인

※ src/main/java : .java 파일을 관리하는 곳

    src/main/resources : 스프링 설정 파일(XML), 프로퍼티 파일 등 자원 파일을 관리하는 곳

'Spring > Study' 카테고리의 다른 글

스프링(Spring) 설정 파일 분리  (0) 2020.04.09
스프링(Spring) 다양한 의존 객체 주입  (0) 2020.04.08
스프링(Spring) DI(Dependency Injection)  (0) 2020.04.08
스프링(Spring) Hello World!  (0) 2020.04.07
스프링(Spring) 개요  (0) 2020.04.06
posted by DevMoomin
: