TIL_Java Spring the Modern Way_2

-·2021년 2월 24일
0

spring boot 프로젝트에서 boot 지우고 spring mvc 프로젝트로 변환

pom.xml

  • Spring boot
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter</artifactId>
</dependency>
  • Spring MVC
<!-- spring core bean factory가 정의됨 bean의 기본적인 관리를 담당 -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
</dependency>

<!-- application context 관리 -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
</dependency>

main class

어노테이션
  • Spring boot

@SpringBootApplication

  • Spring MVC

@Configuration - 이게 스프링환경임을 알려줌
@ComponentScan("com.in28min.spring.basics.springin5steps") - scan할 패키지 범위 지정

ApplicationContext
ApplicationContext applicationContext = 
    // spring MVC
    new AnnotationConfigApplicationContext(SpringIn5StepsBasicApplication.class);
	// spring boot
	//SpringApplication.run(SpringIn5StepsBasicApplication.class, args);

applicationcontext.xml

현재는 어노테이션으로 의존성과 종속성을 관리하고있다.

ex) @Component, @Scope, @Autowired

이걸 XML문서로 바꿔보자

  1. resource폴더에 applicationcontext.xml파일을 생성

  2. applicationcontext.xml

    이거 다 외워서 칠수가 없다.

    구글에 spring documentation application context example 이렇게 검색하면 친절하게 예제가 나와있다.

    <?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을 정의 합시다 @Component -->
    	<bean id="xmlJdbcConnection"
    		class="com.in28min.spring.basics.springin5steps.xml.XmlJdbcConnection">
    	</bean>
    
    	<bean id="xmlPersonDAO"
    		class="com.in28min.spring.basics.springin5steps.xml.XmlPersonDAO">
    		<!-- 종속성을 주입합시다 @Autowired -->
    		<property name="xmlJdbcConnection" ref="xmlJdbcConnection"/>
    	</bean>
    </beans>
  3. context 불러오기

    ClassPathXmlApplicationContext applicationContext =
    				 new ClassPathXmlApplicationContext("applicationContext.xml")

어노테이션이 없지만 xml문서로 의존성과 종속성을 구성할수있다.

Try-with-resources

public static void main(String[] args) {
	ClassPathXmlApplicationContext applicationContext =
			 new ClassPathXmlApplicationContext("applicationContext.xml");
		XmlPersonDAO dao = applicationContext.getBean(XmlPersonDAO.class);
		applicationContext.close();
}

close() 는 마지막에 꼭 실행해줘야된다. 중간에 예외처리가 있으면 그거 할때마다 만들어줘야 되고 비효율적이다

public static void main(String[] args) {
	try (ClassPathXmlApplicationContext applicationContext =
			new ClassPathXmlApplicationContext("applicationContext.xml")) {

		XmlPersonDAO dao = applicationContext.getBean(XmlPersonDAO.class);
	}
}

JDK7 부터 추가된 AutoCloseable 인터페이스를 활용해서 처리가 가능하다

public interface AutoCloseable {
    void close() throws Exception;
}

이걸활용해서 try에 전달해두면 try가 책임지고 close를 호출해준다.

try (something1 = new something1(); something2 = new something2()) {
    ...
} catch (...){
    ...
}

이렇게 여러개도 넣어줄 수 있다.

profile
거북이는 오늘도 걷는다

0개의 댓글